import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Computer extends JFrame
{
JTextField vi;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;
JPanel jp;
boolean bool=true;
double result;
String s,strnum,strbut;
boolean bb1=false;boolean bb2=false;boolean bb3=false;boolean bb4=false;
public Computer()
{
Container c=getContentPane();
c.setLayout(new BorderLayout());
result=0.0;
strnum="";strbut="=";
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
vi=new JTextField("0");
vi.setEditable(false);
vi.setHorizontalAlignment(JTextField.RIGHT);
jp=new JPanel();
jp.setLayout(new GridLayout(4,4,2,2));
b0=new JButton("0");b1=new JButton("1");b2=new JButton("2");b3=new JButton("3");
b4=new JButton("4");b5=new JButton("5");b6=new JButton("6");b7=new JButton("7");
b8=new JButton("8");b9=new JButton("9");b10=new JButton("+");b11=new JButton("-");
b12=new JButton("*");b13=new JButton("/");b14=new JButton("=");b15=new JButton("Space");
jp.add(b0);jp.add(b1);jp.add(b2);jp.add(b3);jp.add(b4);jp.add(b5);jp.add(b6);jp.add(b7);
jp.add(b8);jp.add(b9);jp.add(b10);jp.add(b11);jp.add(b12);jp.add(b13);jp.add(b14);jp.add(b15);
c.add(jp,BorderLayout.CENTER);
c.add(vi,BorderLayout.NORTH);
b0.addActionListener(insert);b1.addActionListener(insert);b2.addActionListener(insert);b3.addActionListener(insert);
b4.addActionListener(insert);b5.addActionListener(insert);b6.addActionListener(insert);b7.addActionListener(insert);
b8.addActionListener(insert);b9.addActionListener(insert);b10.addActionListener(command);b11.addActionListener(command);
b12.addActionListener(command);b13.addActionListener(command);b14.addActionListener(command);b15.addActionListener(command);
setResizable(false); //窗体不能改变大小
setLocation(400,400);
setSize(300,200);
show();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
}
public static void main(String args[])
{
new Computer();
}
private class InsertAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input = e.getActionCommand();
if(bool==true)
{
vi.setText("");
bool=false;
}
if(bool==false)
{
vi.setText(vi.getText()+input);
}
}
};
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
s=evt.getActionCommand();
if(bool==true)//二次计算
{
strbut=s;System.out.println("bool=true:"+strbut);
}
if(bool==false)
{
calculate(Double.parseDouble(vi.getText()));
strbut=s;System.out.println("bool=false:"+strbut);
bool=true;
}
if(strbut.equals("Space"))
{
vi.setText("0.0");
bool=true;
}
}
}
public void calculate(double x) {
if (strbut.equals("+")) result+= x;
else if (strbut.equals("-")) result-=x;
else if (strbut.equals("*")) {result*=x;System.out.println("result:"+result);}
else if (strbut.equals("/")) result/=x;
else if (strbut.equals("=")) result=x;
vi.setText(""+ result);
}
};
这个计算器的结果倒是出来了,可是有些地方没怎么弄清楚, 就是,乘法,除法计算的时候,为什么if (strbut.equals("*")) {result*=x;System.out.println("result:"+result);}result不是0呢?因为我是一下一下的输入,而且没有保存上次输入的数据.....请详细指教.....