189 8069 5689

java计算器布局代码 基于java的计算器设计

java计算器的源代码

import java.awt.*;

成都创新互联公司专注于企业成都营销网站建设、网站重做改版、富宁网站定制设计、自适应品牌网站建设、HTML5商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为富宁等各大城市提供网站开发制作服务。

import java.awt.event.*;

import java.lang.*;

import javax.swing.*;

public class Counter extends Frame

{

//声明三个面板的布局

GridLayout gl1,gl2,gl3;

Panel p0,p1,p2,p3;

JTextField tf1;

TextField tf2;

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;

StringBuffer str;//显示屏所显示的字符串

double x,y;//x和y都是运算数

int z;//Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"

static double m;//记忆的数字

public Counter()

{

gl1=new GridLayout(1,4,10,0);//实例化三个面板的布局

gl2=new GridLayout(4,1,0,15);

gl3=new GridLayout(4,5,10,15);

tf1=new JTextField(27);//显示屏

tf1.setHorizontalAlignment(JTextField.RIGHT);

tf1.setEnabled(false);

tf1.setText("0");

tf2=new TextField(10);//显示记忆的索引值

tf2.setEditable(false);

//实例化所有按钮、设置其前景色并注册监听器

b0=new Button("Backspace");

b0.setForeground(Color.red);

b0.addActionListener(new Bt());

b1=new Button("CE");

b1.setForeground(Color.red);

b1.addActionListener(new Bt());

b2=new Button("C");

b2.setForeground(Color.red);

b2.addActionListener(new Bt());

b3=new Button("MC");

b3.setForeground(Color.red);

b3.addActionListener(new Bt());

b4=new Button("MR");

b4.setForeground(Color.red);

b4.addActionListener(new Bt());

b5=new Button("MS");

b5.setForeground(Color.red);

b5.addActionListener(new Bt());

b6=new Button("M+");

b6.setForeground(Color.red);

b6.addActionListener(new Bt());

b7=new Button("7");

b7.setForeground(Color.blue);

b7.addActionListener(new Bt());

b8=new Button("8");

b8.setForeground(Color.blue);

b8.addActionListener(new Bt());

b9=new Button("9");

b9.setForeground(Color.blue);

b9.addActionListener(new Bt());

b10=new Button("/");

b10.setForeground(Color.red);

b10.addActionListener(new Bt());

b11=new Button("sqrt");

b11.setForeground(Color.blue);

b11.addActionListener(new Bt());

b12=new Button("4");

b12.setForeground(Color.blue);

b12.addActionListener(new Bt());

b13=new Button("5");

b13.setForeground(Color.blue);

b13.addActionListener(new Bt());

b14=new Button("6");

b14.setForeground(Color.blue);

b14.addActionListener(new Bt());

b15=new Button("*");

b15.setForeground(Color.red);

b15.addActionListener(new Bt());

b16=new Button("%");

b16.setForeground(Color.blue);

b16.addActionListener(new Bt());

b17=new Button("1");

b17.setForeground(Color.blue);

b17.addActionListener(new Bt());

b18=new Button("2");

b18.setForeground(Color.blue);

b18.addActionListener(new Bt());

b19=new Button("3");

b19.setForeground(Color.blue);

b19.addActionListener(new Bt());

b20=new Button("-");

b20.setForeground(Color.red);

b20.addActionListener(new Bt());

b21=new Button("1/X");

b21.setForeground(Color.blue);

b21.addActionListener(new Bt());

b22=new Button("0");

b22.setForeground(Color.blue);

b22.addActionListener(new Bt());

b23=new Button("+/-");

b23.setForeground(Color.blue);

b23.addActionListener(new Bt());

b24=new Button(".");

b24.setForeground(Color.blue);

b24.addActionListener(new Bt());

b25=new Button("+");

b25.setForeground(Color.red);

b25.addActionListener(new Bt());

b26=new Button("=");

b26.setForeground(Color.red);

b26.addActionListener(new Bt());

//实例化四个面板

p0=new Panel();

p1=new Panel();

p2=new Panel();

p3=new Panel();

//创建一个空字符串缓冲区

str=new StringBuffer();

//添加面板p0中的组件和设置其在框架中的位置和大小

p0.add(tf1);

p0.setBounds(10,25,300,40);

//添加面板p1中的组件和设置其在框架中的位置和大小

p1.setLayout(gl1);

p1.add(tf2);

p1.add(b0);

p1.add(b1);

p1.add(b2);

p1.setBounds(10,65,300,25);

//添加面板p2中的组件并设置其的框架中的位置和大小

p2.setLayout(gl2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

p2.add(b6);

p2.setBounds(10,110,40,150);

//添加面板p3中的组件并设置其在框架中的位置和大小

p3.setLayout(gl3);//设置p3的布局

p3.add(b7);

p3.add(b8);

p3.add(b9);

p3.add(b10);

p3.add(b11);

p3.add(b12);

p3.add(b13);

p3.add(b14);

p3.add(b15);

p3.add(b16);

p3.add(b17);

p3.add(b18);

p3.add(b19);

p3.add(b20);

p3.add(b21);

p3.add(b22);

p3.add(b23);

p3.add(b24);

p3.add(b25);

p3.add(b26);

p3.setBounds(60,110,250,150);

//设置框架中的布局为空布局并添加4个面板

setLayout(null);

add(p0);

add(p1);

add(p2);

add(p3);

setResizable(false);//禁止调整框架的大小

//匿名类关闭窗口

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e1)

{

System.exit(0);

}

});

setBackground(Color.lightGray);

setBounds(100,100,320,280);

setVisible(true);

}

//构造监听器

class Bt implements ActionListener

{

public void actionPerformed(ActionEvent e2)

{

try{

if(e2.getSource()==b1)//选择"CE"清零

{

tf1.setText("0");//把显示屏清零

str.setLength(0);//清空字符串缓冲区以准备接收新的输入运算数

}

else if(e2.getSource()==b2)//选择"C"清零

{

tf1.setText("0");//把显示屏清零

str.setLength(0);

}

else if(e2.getSource()==b23)//单击"+/-"选择输入的运算数是正数还是负数

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText(""+(-x));

}

else if(e2.getSource()==b25)//单击加号按钮获得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);//清空缓冲区以便接收新的另一个运算数

y=0d;

z=0;

}

else if(e2.getSource()==b20)//单击减号按钮获得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=1;

}

else if(e2.getSource()==b15)//单击乘号按钮获得x的值和z的值并清空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=2;

}

else if(e2.getSource()==b10)//单击除号按钮获得x的值和z的值并空y的值

{

x=Double.parseDouble(tf1.getText().trim());

str.setLength(0);

y=0d;

z=3;

}

else if(e2.getSource()==b26)//单击等号按钮输出计算结果

{

str.setLength(0);

switch(z)

{

case 0 : tf1.setText(""+(x+y));break;

case 1 : tf1.setText(""+(x-y));break;

case 2 : tf1.setText(""+(x*y));break;

case 3 : tf1.setText(""+(x/y));break;

}

}

else if(e2.getSource()==b24)//单击"."按钮输入小数

{

if(tf1.getText().trim().indexOf(′.′)!=-1)//判断字符串中是否已经包含了小数点

{

}

else//如果没数点有小

{

if(tf1.getText().trim().equals("0"))//如果初时显示为0

{

str.setLength(0);

tf1.setText((str.append("0"+e2.getActionCommand())).toString());

}

else if(tf1.getText().trim().equals(""))//如果初时显示为空则不做任何操作

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

}

}

y=0d;

}

else if(e2.getSource()==b11)//求平方根

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText("数字格式异常");

if(x0)

tf1.setText("负数没有平方根");

else

tf1.setText(""+Math.sqrt(x));

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b16)//单击了"%"按钮

{

x=Double.parseDouble(tf1.getText().trim());

tf1.setText(""+(0.01*x));

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b21)//单击了"1/X"按钮

{

x=Double.parseDouble(tf1.getText().trim());

if(x==0)

{

tf1.setText("除数不能为零");

}

else

{

tf1.setText(""+(1/x));

}

str.setLength(0);

y=0d;

}

else if(e2.getSource()==b3)//MC为清除内存

{

m=0d;

tf2.setText("");

str.setLength(0);

}

else if(e2.getSource()==b4)//MR为重新调用存储的数据

{

if(tf2.getText().trim()!="")//有记忆数字

{

tf1.setText(""+m);

}

}

else if(e2.getSource()==b5)//MS为存储显示的数据

{

m=Double.parseDouble(tf1.getText().trim());

tf2.setText("M");

tf1.setText("0");

str.setLength(0);

}

else if(e2.getSource()==b6)//M+为将显示的数字与已经存储的数据相加要查看新的数字单击MR

{

m=m+Double.parseDouble(tf1.getText().trim());

}

else//选择的是其他的按钮

{

if(e2.getSource()==b22)//如果选择的是"0"这个数字键

{

if(tf1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作

{

}

else

{

tf1.setText(str.append(e2.getActionCommand()).toString());

y=Double.parseDouble(tf1.getText().trim());

}

}

else if(e2.getSource()==b0)//选择的是“BackSpace”按钮

{

if(!tf1.getText().trim().equals("0"))//如果显示屏显示的不是零

{

if(str.length()!=1)

{

tf1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常

}

else

{

tf1.setText("0");

str.setLength(0);

}

}

y=Double.parseDouble(tf1.getText().trim());

}

else//其他的数字键

{

tf1.setText(str.append(e2.getActionCommand()).toString());

y=Double.parseDouble(tf1.getText().trim());

}

}

}

catch(NumberFormatException e){

tf1.setText("数字格式异常");

}

catch(StringIndexOutOfBoundsException e){

tf1.setText("字符串索引越界");

}

}

}

public static void main(String args[])

{

new Counter();

求Java计算器标准的布局代码

是用awt和swing做的,很好看比单单用AWT做好看

import java.awt.*;

import javax.swing.*;

public class Conputer extends JFrame {

protected Container con = getContentPane();// 指向内容面板

protected JMenuBar a = new JMenuBar();// 菜单条

protected JMenu a1 = new JMenu("编辑(E)");// 菜单1

protected JMenu a2 = new JMenu("查看(V)");// 菜单2

protected JMenu a3 = new JMenu("帮助(H)");// 菜单3

protected JMenuItem a11 = new JMenuItem("复制(C)", 'C');// 菜单1的菜单项

protected JMenuItem a12 = new JMenuItem("粘贴(P)", 'P');// 菜单1的菜单项

protected JMenuItem a21 = new JMenuItem("标准型(T)", 'T');// 菜单2的菜单项

protected JMenuItem a22 = new JMenuItem("科学型(S)", 'S');// 菜单2的菜单项

protected JMenuItem a31 = new JMenuItem("帮助主题(H)", 'H');// 菜单3的菜单项

protected JTextField jtf = new JTextField(30);// 文本框

protected JButton[] jb = new JButton[27];

protected String[] arr = { "Backspace", "CE", "C", "MC", "7", "8", "9", "/", "sqrt",

"MR", "4", "5", "6", "*", "%", "MS", "1", "2", "3", "-", "1/x",

"M+", "0", "+/-", ".", "+", "=" };

protected JPanel jp = new JPanel();// 主面板

protected JPanel m = new JPanel();// 次面板

protected JPanel m1 = new JPanel();// 次次面板

protected JPanel m2 = new JPanel();// 次次面板

protected JPanel m3 = new JPanel();// 次次面板

protected JPanel m4 = new JPanel();// 次次面板

protected JPanel m5 = new JPanel();// 次次面板

protected JPanel m6 = new JPanel();// 次次面板

protected GridLayout glo = new GridLayout(6, 1, 3, 3);// 主网格布局

protected GridLayout glo1 = new GridLayout(1, 3, 3, 3);// 次网格布局

protected GridLayout glo2 = new GridLayout(1, 6, 3, 3);// 次网格布局

protected Conputer(String s) {

super(s);

a1.setMnemonic('E');

a1.add(a11);

a1.add(a12);

a2.setMnemonic('V');

a2.add(a21);

a2.add(a22);

a3.setMnemonic('H');

a3.add(a31);

a.add(a1);

a.add(a2);

a.add(a3);

this.setJMenuBar(a);// 菜单条完成

for (int i = 0; i 27; i++) {

jb[i] = new JButton(arr[i]);

}

jp.setLayout(glo);

jp.add(m1);

m1.add(jtf);

jtf.setEditable(false); jtf.setText("0."); jtf.setForeground(Color.BLUE); jtf.setBackground(Color.WHITE);

jtf.setHorizontalAlignment(JTextField.RIGHT);//文本显示在右边

jp.add(m2);

m2.setLayout(glo1);

m2.add(jb[0]);

m2.add(jb[1]);

m2.add(jb[2]);

jp.add(m3);

m3.setLayout(glo2);

m3.add(jb[3]);

m3.add(jb[4]);

m3.add(jb[5]);

m3.add(jb[6]);

m3.add(jb[7]);

m3.add(jb[8]);

jp.add(m4);

m4.setLayout(glo2);

m4.add(jb[9]);

m4.add(jb[10]);

m4.add(jb[11]);

m4.add(jb[12]);

m4.add(jb[13]);

m4.add(jb[14]);

jp.add(m5);

m5.setLayout(glo2);

m5.add(jb[15]);

m5.add(jb[16]);

m5.add(jb[17]);

m5.add(jb[18]);

m5.add(jb[19]);

m5.add(jb[20]);

jp.add(m6);

m6.setLayout(glo2);

m6.add(jb[21]);

m6.add(jb[22]);

m6.add(jb[23]);

m6.add(jb[24]);

m6.add(jb[25]);

m6.add(jb[26]);

m.add(jp);

con.add(m);

this.setResizable(false);// 不能用鼠标拉伸窗体

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//可以关闭窗体

this.setSize(380, 275);

this.setVisible(true);

}

protected Conputer() {

this("计算器");

}

public static void main(String[] args) {

new Conputer("计算器");

}

}

求用JAVA实现计算器的代码(可实用的,没语法错误的)

import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; //导包 public class Jisuanqi extends JFrame implements ActionListener { //继承JFrame 实现事件监听 private JTextField reasult; private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnAC, btnAdd, btnSub, btnReasult, btnD, btnAbout, btnCancel; private boolean add, sub, end, s, c; private String str; private double num1, num2; public Jisuanqi() { //构造属性 JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); TitledBorder tb = new TitledBorder("输出"); tb.setTitleColor(Color.BLUE); //标题边框底端线 设置颜色 btnAbout = new JButton(" 关于 "); btnCancel = new JButton("Cancel"); //两个按钮 btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { System.exit(0); } }); //给Cancel添加事件监听 当鼠标点击时 程序结束 btnAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ee) { JOptionPane.showMessageDialog(null, "黄盖!!", "消息", JOptionPane.INFORMATION_MESSAGE); } //给“关于”添加事件监听 当鼠标点击时 弹出对话框 显示黄盖 }); p3.add(btnAbout); p3.add(btnCancel); // JPanel p4=new JPanel(); // JPanel p5=new JPanel(); // reasult.setBorder(tb); reasult = new JTextField("0", 20); reasult.setEditable(false); //设置不能修改 reasult.setHorizontalAlignment(JTextField.RIGHT); // 设置文本的水平对齐方式。 reasult.setForeground(Color.BLUE); //颜色 p1.setBorder(tb); p1.add(reasult); btn0 = new JButton("0"); btn0.addActionListener(this); btn1 = new JButton("1"); btn1.addActionListener(this); btn2 = new JButton("2"); btn2.addActionListener(this); btn3 = new JButton("3"); btn3.addActionListener(this); btn4 = new JButton("4"); btn4.addActionListener(this); btn5 = new JButton("5"); btn5.addActionListener(this); btn6 = new JButton("6"); btn6.addActionListener(this); btn7 = new JButton("7"); btn7.addActionListener(this); btn8 = new JButton("8"); btn8.addActionListener(this); btn9 = new JButton("9"); btn9.addActionListener(this); btnD = new JButton("."); btnD.addActionListener(this); btnD.setForeground(Color.RED); btnAC = new JButton("AC"); btnAC.addActionListener(this); btnAC.setBackground(Color.PINK); btnAdd = new JButton("+"); btnAdd.addActionListener(this); btnAdd.setForeground(Color.BLUE); btnSub = new JButton("—"); btnSub.addActionListener(this); btnSub.setForeground(Color.BLUE); btnReasult = new JButton("="); btnReasult.addActionListener(this); btnReasult.setForeground(Color.RED); //事件监听 + 颜色 p2.add(btn1); p2.add(btn2); p2.add(btn3); p2.add(btn4); p2.add(btn5); p2.add(btn6); p2.add(btn7); p2.add(btn8); p2.add(btn9); p2.add(btn0); p2.add(btnD); p2.add(btnAC); p2.add(btnAdd); p2.add(btnSub); p2.add(btnReasult); //面板上添加按钮 p2.setLayout(new GridLayout(5, 3)); //面板上设置对齐方式 add(p1, BorderLayout.NORTH); add(p2, BorderLayout.CENTER); add(p3, BorderLayout.SOUTH); //将p1 p2 p3 面板对象添加到JFrame } public void num(int i) { String s = null; s = String.valueOf(i); if (end) { // 如果数字输入结束,则将文本框置零,重新输入 reasult.setText("0"); end = false; } if ((reasult.getText()).equals("0")) { // 如果文本框的内容为零,则覆盖文本框的内容 reasult.setText(s); } else { // 如果文本框的内容不为零,则在内容后面添加数字 str = reasult.getText() + s; reasult.setText(str); } }/* * * String s=null; * * s=String.valueOf(i); * * str=reasult.getText()+s; * * reasult.setText(str); * * } */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) num(1); else if (e.getSource() == btn2) num(2); else if (e.getSource() == btn3) num(3); else if (e.getSource() == btn4) num(4); else if (e.getSource() == btn5) num(5); else if (e.getSource() == btn6) num(6); else if (e.getSource() == btn7) num(7); else if (e.getSource() == btn8) num(8); else if (e.getSource() == btn9) num(9); else if (e.getSource() == btn0) num(0); else if (e.getSource() == btnAdd) { sign(1); btnD.setEnabled(true); } else if (e.getSource() == btnSub) { sign(2); btnD.setEnabled(true); } else if (e.getSource() == btnAC) { btnD.setEnabled(true); reasult.setText("0"); } else if (e.getSource() == btnD) { str = reasult.getText(); str += "."; reasult.setText(str); btnD.setEnabled(false); } else if (e.getSource() == btnReasult) { btnD.setEnabled(true); num2 = Double.parseDouble(reasult.getText()); if (add) { num1 = num1 + num2; } else if (sub) { num1 = num1 - num2; } reasult.setText(String.valueOf(num1)); end = true; } } public void sign(int s) { if (s == 1) { add = true; sub = false; } else if (s == 2) { add = false; sub = true; } num1 = Double.parseDouble(reasult.getText()); end = true; } //设计计算的过程 public static void main(String[] args) { Jisuanqi j = new Jisuanqi(); j.setTitle("+/-简易计算器"); j.setLocation(500, 280); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //默认关闭可以关闭程序 j.setResizable(false); j.pack(); j.setVisible(true); } } 这个计算机,绝对让你满意

求JAVA语言写的计算器的代码。用GridLayout布局。

package com.citi.Util;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

/**

* @author yc80549

*

*/

public class Calculator extends JFrame implements ActionListener {

/**

*

*

*/

private static final long serialVersionUID = 1L;

private JTextField expression = new JTextField();

private JPanel buttonContainer = new JPanel();

private JPanel operators = new JPanel();

private JButton b1 = new JButton("1");

private JButton b2 = new JButton("2");

private JButton b3 = new JButton("3");

private JButton b4 = new JButton("4");

private JButton b5 = new JButton("5");

private JButton b6 = new JButton("6");

private JButton b7 = new JButton("7");

private JButton b8 = new JButton("8");

private JButton b9 = new JButton("9");

private JButton b0 = new JButton("0");

private JButton bpoint = new JButton(".");

private JButton bracket1 = new JButton("(");

private JButton bracket2 = new JButton(")");

private JButton plus = new JButton("+");

private JButton minus = new JButton("-");

private JButton multiply = new JButton("*");

private JButton divide = new JButton("/");

private JButton getResult = new JButton("=");

private JButton backSpace = new JButton("Backspace");

private JButton clear = new JButton("C");

private JButton OFF = new JButton("OFF");

private StringCalculate2 cacu = new StringCalculate2();

public Calculator() {

super();

setVisible(true);

this.setLayout(new BorderLayout());

this.setSize(400, 220);

Toolkit tk = Toolkit.getDefaultToolkit();

int width = (int) tk.getScreenSize().getWidth();

int height = (int) tk.getScreenSize().getHeight();

this.setLocation((int) ((width - getSize().getWidth()) / 2),

(int) ((height - getSize().getHeight()) / 2));

this.locate();

this.addAllListener();

}

public void locate() {

buttonContainer.setLayout(new GridLayout(4, 4));

buttonContainer.add(b1);

buttonContainer.add(b2);

buttonContainer.add(b3);

buttonContainer.add(plus);

buttonContainer.add(b4);

buttonContainer.add(b5);

buttonContainer.add(b6);

buttonContainer.add(minus);

buttonContainer.add(b7);

buttonContainer.add(b8);

buttonContainer.add(b9);

buttonContainer.add(multiply);

buttonContainer.add(b0);

buttonContainer.add(bpoint);

buttonContainer.add(getResult);

buttonContainer.add(divide);

operators.add(bracket1);

operators.add(bracket2);

operators.add(backSpace);

operators.add(clear);

operators.add(OFF);

this.add(expression, BorderLayout.NORTH);

this.add(buttonContainer, BorderLayout.CENTER);

this.add(operators, BorderLayout.SOUTH);

}

public void addAllListener() {

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

plus.addActionListener(this);

multiply.addActionListener(this);

divide.addActionListener(this);

minus.addActionListener(this);

bpoint.addActionListener(this);

bracket1.addActionListener(this);

bracket2.addActionListener(this);

getResult.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

double result = cacu.parse(expression.getText());

expression.setText("" + result);

} catch (Exception e1) {

System.err

.println("invaild expression or RuntimeException");

}

}

});

clear.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

expression.setText("");

}

});

OFF.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(1);

}

});

backSpace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

expression.setText(expression.getText().substring(0,

expression.getText().length() - 1));

}

});

}

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) e.getSource();

expression.setText(expression.getText() + source.getText());

}

public static void main(String[] args) {

Calculator cal = new Calculator();

cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

package com.citi.Util;

import java.util.Stack;

import javax.swing.JOptionPane;

public class StringCalculate2 {

private StackDouble numbers = new StackDouble();

private StackCharacter chs = new StackCharacter();

private boolean compare(char str) {

if (chs.empty()) {

return true;

}

char last = (char) chs.lastElement();

switch (str) {

case '*': {

if (last == '+' || last == '-')

return true;

else

return false;

}

case '/': {

if (last == '+' || last == '-')

return true;

else

return false;

}

case '+':

return false;

case '-':

return false;

}

return true;

}

public Double caculate(String str) {

StringBuffer sb = new StringBuffer(str);

StringBuffer num = new StringBuffer();

String tem = null;

char next;

while (sb.length() 0) {

tem = sb.substring(0, 1);

sb.delete(0, 1);

if (isNum(tem.trim())) {

num.append(tem);

} else {

if (num.length() 0 !"".equals(num.toString().trim())) {

Double d = new Double(num.toString().trim());

numbers.push(d);

num.delete(0, num.length());

}

if (!chs.isEmpty()) {

while (!compare(tem.charAt(0))) {

caculate();

}

}

if (numbers.isEmpty()) {

num.append(tem);

} else {

chs.push(new Character(tem.charAt(0)));

}

next = sb.charAt(0);

if (next == '-') {

num.append(next);

sb.delete(0, 1);

}

}

}

Double d = new Double(num.toString().trim());

numbers.push(d);

while (!chs.isEmpty()) {

caculate();

}

return numbers.pop();

}

public void caculate() {

Double b = numbers.pop();

Double a = null;

a = numbers.pop();

char ope = chs.pop();

Double result = null;

switch (ope) {

case '+':

result = a + b;

numbers.push(result);

break;

case '-':

result = a - b;

numbers.push(result);

break;

case '*':

result = a * b;

numbers.push(result);

break;

case '/':

result = a / b;

numbers.push(result);

break;

}

}

private boolean isNum(String num) {

return num.matches("[0-9.]");

}

public Double parse(String str) {

int start = 0;

StringBuffer sts = new StringBuffer(str);

int end = -1;

while ((end = sts.indexOf(")")) 0) {

String s = sts.substring(start, end + 1);

int first = s.lastIndexOf("(");

Double value = caculate(sts.substring(first + 1, end));

sts.replace(first, end + 1, value.toString());

}

return caculate(sts.toString());

}

public static void main(String[] args) {

String exp = JOptionPane

.showInputDialog("please input expression you want to caculate!");

if (exp.trim().equals("")) {

System.out.println("you have input nothing");

return;

}

StringCalculate2 sc = new StringCalculate2();

JOptionPane.showMessageDialog(null, "the result is: " + sc.parse(exp));

}

}

急求!!!如何用java程序代码实现计算器界面

package jisuanqi_new;

import java.awt.*;

import java.awt.event.*;

public class JiSuanQi_new implements ActionListener

{

Panel p1;//声明面板p1

TextField t1;//声明文本行t1

String[] label = {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//声明标签数组label1存放按钮上的标签

Button[] b;//声明按钮数组存放16个按钮

private int i;//声明i以备后用

private String op1 = "0";//运算数备用

private String operator = "+";//运算符备用

private boolean append = false;//备用

public JiSuanQi_new()//构造方法

{

t1=new TextField();//初始化文本行t1

b = new Button[label.length];//初始化按钮数组b

p1=new Panel();//初始化面板p1

p1.setLayout(new GridLayout(4,4,4,4));//使面板选择网格布局管理器以备储存16个按钮(4行4列)

for(int i=0;ib.length;i++)//利用for循环把标签放在按钮上,使每个按钮添加事件监听器,在面板p1上添加上16个按钮

{

b[i] = new Button(label[i]);//把标签依次放在16个按钮上

b[i].addActionListener(this);//使每个按钮添加动作事件监听器

p1.add(b[i]); //分别将按钮添加到面板p1上

}

Frame f=new Frame("计算器1.0");//初始化窗口f,起名字计算器1.0

f.setLayout(new BorderLayout());//为窗口选择边界布局管理器

f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部

f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中间

f.addWindowListener(new WindowAdapter(){//给窗口f添加窗口事件监听器

public void windowClosing(WindowEvent eve){//运行窗口关闭方法

System.exit(0);//退出程序

}

});

f.setSize(250, 250);//设置窗口大小

f.setLocation(200,200);

f.setVisible(true);//显示窗口

}

public static void main(String args[])

{

new JiSuanQi_new(); //调用构造方法

}

public void actionPerformed(ActionEvent ae)

{//按钮被操作发生

String comm = ae.getActionCommand();//返回与此动作相关的命令字符串,即:使用者第一次点击的按钮是什么。

if("0123456789".indexOf(comm)!=-1)//如果相关命令字符串为0~9之间的数字则执行

{

if(append){

String temp = t1.getText();//新数字

t1.setText(temp+comm);

}else{                         //因为此时append为false执行这个

t1.setText(comm); //将文本行t1设置为相关命令字符串(你按中的按钮代码)

append =  true;//此时append=true若继续按按钮若继续按数字的话1.第一次的按话不会改变2.非第一次按得话会覆盖之前按得数字(即缺点:只能进行单个数字的计算)

}

}

else if(("+-*/".indexOf(comm)!=-1))//如果相关命令字符串为+-*/之间的数字则执行

{

//保存

//t1.setText(comm);

op1 = t1.getText();//把第一个数赋值给op1

operator = comm;//把命令字符串赋值给operator

append = false;//此时append为false即若继续按按钮若按数字的话将重复上面的动作,按符号的话将覆盖之前的符号

}

else if("=".equals(comm))//如果按的是=号,则按条件进行下面的运算

{

String op2 = t1.getText();//op2第二个数

double d1 = Double.parseDouble(op1);

double d2 = Double.parseDouble(op2);

if(operator.equals("+")){

d1 = d1 + d2 ;

}else if(operator.equals("-")){

d1 = d1 - d2;

}else if(operator.equals("*")){

d1 = d1 * d2;

}else {

d1 = d1 / d2;

}

t1.setText(d1+"");//显示计算结果

append = false;

}

else if(".".equals(comm))//若是.号继续按

{

String temp = t1.getText();

if(temp.indexOf(".")==-1){

t1.setText(temp+".");

append = true;

}

}

}

}

JAVA计算器相关代码求大神{详解}

我给你找找

package com.bj.calcultor;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calcultor extends Frame implements ActionListener {

public static void main(String[] args) {//定义主方

new Calcultor();//创建匿名对象,并调用test()方法;

}

//定义按钮名称

String[] arr={"1","2","3","4","5","6","7","8","9","0","+","-","*","/","=","."};

JButton [] button=new JButton[arr.length];

JButton reset = new JButton("CE");

JTextField display = new JTextField(20);

//创建窗口,定义组件

//执行窗口事件:关闭窗口

private class WindowCloser extends WindowAdapter {

public void windowClosing(WindowEvent we) {

System.exit(0);

}

}

public Calcultor(){

super("计算器");//定义标题

//定义面板容器,并布局

JPanel jpanel=new JPanel(new GridLayout(4,4));

//添加按钮,并给按钮添加名称

for (int i = 0; i arr.length; i++) {

button[i]= new JButton(arr[i]);

jpanel.add(button[i]);

}

JPanel panel2 = new JPanel();

panel2.add("Northr", display);

display.setEnabled(false);

panel2.add("East", reset);

this.add("North", panel2);

this.add("Center", jpanel);

for (int i = 0; i arr.length; i++){

addWindowListener(new WindowCloser());

setVisible(true);

setSize(400,400);

pack();

button[i].addActionListener(this);

reset.addActionListener(this);

display.addActionListener(this);

}

}

@Override

public void actionPerformed(ActionEvent e) {//定义事件

// TODO Auto-generated method stub

Object target=e.getSource();

String lable=e.getActionCommand();

if(target==reset){

handleReset();

}else if("0123456789.".indexOf(lable)0){

handleNumber(lable);

}else{

hadleOperator(lable);

}

}

boolean isFirstDigit=true;

private void hadleOperator(String key) {

if(operator.equals("+")){

number += Double.valueOf(display.getText());

}else if (operator.equals("-")){

number -= Double.valueOf(display.getText());

}else if (operator.equals("*")){

number *= Double.valueOf(display.getText());

}else if (operator.equals("/")){

number /= Double.valueOf(display.getText());

}else if(operator.equals("=")){

number =Double.valueOf(display.getText());

}

display.setText(String.valueOf(number));

operator=key;

isFirstDigit=true;

}

private void handleNumber(String key) {

if(true){

display.setText(key);//把键值设置为文本框内容

}else if(key.equals(".") display.getText().indexOf(".")0){

display.setText(display.getText()+".");//把文本框内容设置:display.getText()+"."

}else if(!key.equals(".")){

display.setText(display.getText() + key);//把文本框内容设置:display.getText()+key

isFirstDigit=false;

}

}

private void handleReset() {

display.setText("0");

isFirstDigit=true;

operator="=";

}

String operator="=";

Double number=0.0;

}


网页名称:java计算器布局代码 基于java的计算器设计
文章位置:http://jkwzsj.com/article/hjdsgi.html

其他资讯