好不容易做完了 几经调试呀 呵呵
import java.awt.*;
import java.awt.event.*;
public class jisuanqi extends WindowAdapter
{
static double a=0,c;
static String str,s;
public static void main(String []args)
{
(new jisuanqi()).chuangkou();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
TextField t=new TextField(30);
Button b=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("关闭");
Button b10=new Button("清除");
Button b11=new Button("9");
Button b12=new Button("0");
Button b0=new Button("=");
Button b13=new Button("+");
Button b14=new Button("-");
Button b15=new Button("*");
Button b16=new Button("/");
Button b17=new Button("说明");
public void chuangkou()
{
Frame f=new Frame("你好 AWT 窗口");
f.addWindowListener(this);
f.setLocation(300,200);
f.setBackground(Color.green);
f.setSize(300,300);
f.setLayout(new GridLayout(5,3,10,10));
b9.setBackground(Color.yellow);
//b0.setBackground(Color.blue);
b9.addActionListener(new close());
b.addActionListener(new wang());
b2.addActionListener(new wang());
b3.addActionListener(new wang());
b4.addActionListener(new wang());
b5.addActionListener(new wang());
b6.addActionListener(new wang());
b7.addActionListener(new wang());
b8.addActionListener(new wang());
b10.addActionListener(new wang());
b11.addActionListener(new wang());
b12.addActionListener(new wang());
b0.addActionListener(new wang());
b13.addActionListener(new wang());
b14.addActionListener(new wang());
b15.addActionListener(new wang());
b16.addActionListener(new wang());
b17.addActionListener(new wang());
f.add(t);
f.add(b);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b11);
f.add(b12);
f.add(b13);
f.add(b14);
f.add(b15);
f.add(b16);
f.add(b17);
f.add(b9);
f.add(b10);
f.add(b0);
f.setVisible(true);
}
class wang implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button tt=(Button)e.getSource();
if (tt.getLabel()=="=")
{
jisuan();
s=String.valueOf(a);
t.setText(s);
str="";
}
else if(tt.getLabel()=="+")
{
jisuan();
t.setText("");
str="+";
}
else if(tt.getLabel()=="-")
{
jisuan();
t.setText("");
str="-";
}
else if(tt.getLabel()=="*")
{
jisuan();
t.setText("");
str="*";
}
else if(tt.getLabel()=="/")
{
jisuan();
t.setText("");
str="/";
}
else
t.setText(t.getText()+tt.getLabel());
if(tt.getLabel()=="说明")
t.setText("!先清空再使用!");
if(tt.getLabel()=="清除")
t.setText("");
}
public void jisuan()
{
if(str=="+")
{
a+=Double.valueOf(t.getText());
}
else if(str=="-")
{
a-=Double.valueOf(t.getText());
}
else if(str=="*")
{
a*=Double.valueOf(t.getText());
}
else if(str=="/")
{
a/=Double.valueOf(t.getText());
}
else
{
a=Double.valueOf(t.getText());
}
}
}
}
class close implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}