做的不好的地方,大家说说,因为我刚开始学
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame
implements ActionListener
{
int z;
Double x;
Double y;
Double result;
Panel p = new Panel();
Panel p1 = new Panel();
TextField tx = new TextField();
Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button bt = new Button(".");
Button t1 = new Button("+");
Button t2 = new Button("-");
Button t3 = new Button("*");
Button t4 = new Button("/");
Button g = new Button("=");
public Calculator()
{
this.setTitle("我的计算器");
this.setLayout(new BorderLayout());
this.add(p,BorderLayout.NORTH);
p.setBounds(100,10,300,30);
this.add(p1);
p1.setLayout(new GridLayout(4,4));
p.add(tx);
tx.setColumns(20);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(t1);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(t2);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(t3);
p1.add(b0);
p1.add(bt);
p1.add(g);
p1.add(t4);
this.setBounds(300,200,200,200);
this.setResizable(false);
tx.addActionListener(this);
b0.addActionListener(this);
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);
bt.addActionListener(this);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
t4.addActionListener(this);
g.addActionListener(this);
this.addWindowListener(new MyAp());
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
{
tx.setText(tx.getText()+"0");
}else if(e.getSource()==b1)
{
tx.setText(tx.getText()+"1");
}else if(e.getSource()==b2)
{
tx.setText(tx.getText()+"2");
}else if(e.getSource()==b3)
{
tx.setText(tx.getText()+"3");
}else if(e.getSource()==b4)
{
tx.setText(tx.getText()+"4");
}else if(e.getSource()==b5)
{
tx.setText(tx.getText()+"5");
}else if(e.getSource()==b6)
{
tx.setText(tx.getText()+"6");
}else if(e.getSource()==b7)
{
tx.setText(tx.getText()+"7");
}else if(e.getSource()==b8)
{
tx.setText(tx.getText()+"8");
}else if(e.getSource()==b9)
{
tx.setText(tx.getText()+"9");
}else if(e.getSource()==bt)
{
tx.setText(tx.getText()+".");
}else if(tx.getText().length()<=0)
{
return;
}
else if(e.getSource()==t1)
{
z=0;
x=Double.parseDouble(tx.getText());
tx.setText("");
}else if(e.getSource()==t2)
{
z=1;
x=Double.parseDouble(tx.getText());
tx.setText("");
}else if(e.getSource()==t3)
{
z=2;
x=Double.parseDouble(tx.getText());
tx.setText("");
}else if(e.getSource()==t4)
{
z=3;
x=Double.parseDouble(tx.getText());
tx.setText("");
}
else if(e.getSource()==g)
{
y=Double.parseDouble(tx.getText());
switch(z)
{
case 0:result=x+y;break;
case 1:result=x-y;break;
case 2:result=x*y;break;
case 3:result=x/y;break;
}
tx.setText(result+"");
}
}
public static void main(String args[])
{
new Calculator();
}
}
class MyAp extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}