import java.awt.*;//计算器实例
import java.awt.event.*;
public class calculator
{
public static void main(String args[])
{
MyWindow my=new MyWindow("计算器");
}
}
class MyWindow extends Frame implements ActionListener
{ StringBuffer m=new StringBuffer();
int p;
TextField tex;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,jia,jian,cheng,chu,deng,dian,qingling,kaifang;
MyWindow(String s)
{
super(s);
//StringBuffer s2=new StringBuffer();
//String s;
tex=new TextField(18);
b0=new Button(" 0 ");
b1=new Button(" 1 ");
b2=new Button(" 2 ");
indexof空格算不算 b3=new Button(" 3 ");
b4=new Button(" 4 ");
b5=new Button(" 5 ");
b6=new Button(" 6 ");
b7=new Button(" 7 ");
b8=new Button(" 8 ");
b9=new Button(" 9 ");
dian=new Button(" . ");
jia=new Button(" + ");
jian=new Button(" - ");
cheng=new Button(" × ");
chu=new Button(" / ");
deng=new Button(" = ");
qingling=new Button(" 清零 ");
kaifang=new Button(" √ ");
setLayout(new FlowLayout());
add(tex);
add(b0);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(dian);
add(jia);
add(jian);
add(cheng);
add(chu);
add(kaifang);
add(qingling);
add(deng);
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);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
dian.addActionListener(this);
deng.addActionListener(this);
qingling.addActionListener(this);
kaifang.addActionListener(this);
setBounds(200,200,160,280);
setResizable(false);//不可改变大小
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent ee)
{ it(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
Source()==b0)
{
m=m.append("0");
tex.setText(String.valueOf(m));
}
Source()==b1)
{
m=m.append("1"); tex.setText(String.valueOf(m));
}
Source()==b2)
{
m=m.append("2"); tex.setText(String.valueOf(m));
}
Source()==b3)
{
m=m.append("3"); tex.setText(String.valueOf(m));
}
Source()==b4)
{
m=m.append("4"); tex.setText(String.valueOf(m));
}
Source()==b5)
{
m=m.append("5"); tex.setText(String.valueOf(m));
}
Source()==b6)
{
m=m.append("6"); tex.setText(String.valueOf(m));
}
Source()==b7)
{
m=m.append("7"); tex.setText(String.valueOf(m));
}
Source()==b8)
{
m=m.append("8"); tex.setText(String.valueOf(m));
}
Source()==b9)
{
m=m.append("9"); tex.setText(String.valueOf(m));
}
Source()==jia)
{
m=m.append("+"); tex.setText(String.valueOf(m));
}
Source()==jian)
{
m=m.append("-"); tex.setText(String.valueOf(m));
}
Source()==cheng)
{
m=m.append("*"); tex.setText(String.valueOf(m));
}
Source()==chu)
{
m=m.append("/"); tex.setText(String.valueOf(m));
}
Source()==dian)
{
m=m.append("."); tex.setText(String.valueOf(m));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论