日期:2014-05-20  浏览次数:20827 次

请各位帮帮忙看看自己编写的一个计算器的JAVA小程序
package whh;
import java.awt.*;
import java.awt.event.*;

class EventTest extends Frame implements ActionListener,ItemListener
{
Button btn1[]=new Button[16];
Button btn2[]=new Button[16];
TextField text;
String a1="",a2="",b1="",b2="",b3="",x;
CheckboxGroup cbg=new CheckboxGroup();
Checkbox c1,c2,c3;
EventTest()
  {
setTitle("计算器");
setLayout(null);
setSize(500,250);
setLocation(400,200);
this.setResizable(false);
text=new TextField("0",6);
text.setEditable(false);
Panel pnl=new Panel(new GridLayout(4,4,7,7));
Panel pnl2=new Panel(new GridLayout(1,4,7,7));
Panel pnl3=new Panel(new GridLayout(1,4,7,7));
Panel pnl4=new Panel(new GridLayout(1,4,7,7));
Panel pnl5=new Panel(new GridLayout(1,4,7,7));
Panel pnl6=new Panel(new GridLayout(4,4,7,7));
for(int i=0;i<=9;i++)
{
btn1[i]=new Button(String.valueOf(i));
}
 
btn1[10]=new Button("+");
  btn1[10].setForeground(Color.red);
btn1[11]=new Button("-");
btn1[11].setForeground(Color.red);
btn1[12]=new Button("*");
btn1[12].setForeground(Color.red);
btn1[13]=new Button("/");
btn1[13].setForeground(Color.red);
btn1[14]=new Button("C");
btn1[14].setForeground(Color.red);
btn1[15]=new Button("=");
btn1[15].setForeground(Color.red);
btn2[0]=new Button("Sin");
btn2[1]=new Button("And");
btn2[2]=new Button("(");
btn2[3]=new Button(")");
btn2[4]=new Button("Cos");
btn2[5]=new Button("Or");
btn2[6]=new Button("Exp");
btn2[7]=new Button("Ln");
btn2[8]=new Button("Tan");
btn2[9]=new Button("Xo");
btn2[10]=new Button("x*y");
btn2[11]=new Button("Log");
btn2[12]=new Button("e");
btn2[13]=new Button("Not");
btn2[14]=new Button("x*3");
btn2[15]=new Button("n!");
for(int i=0;i<=15;i++)
{
btn2[i].disable();
}

c1=new Checkbox("十进制",cbg,true);
c2=new Checkbox("八进制",cbg,false);
c3=new Checkbox("二进制",cbg,false);
this.setLayout(null);
pnl.setBounds(260,90,240,160);
pnl2.setBounds(0, 70, 240, 20);
pnl3.setBounds(260, 70, 240, 20);
pnl4.setBounds(0, 30, 500, 20);
pnl5.setBounds(50, 50, 400, 20);
pnl6.setBounds(0,90,240,160);
 
for (int i=0;i<=15;i++)
{
pnl.add(btn1[i]);
}

pnl2.add(c1);
pnl2.add(c2);
pnl2.add(c3);
pnl3.add(new Checkbox("Inv"));
pnl3.add(new Checkbox("Hyp"));
pnl5.add(text);
  for(int i=0;i<=15;i++)
  {
  pnl6.add(btn2[i]);
  }

this.add(pnl);
this.add(pnl2);
this.add(pnl3);
this.add(pnl4);
this.add(pnl5);
this.add(pnl6);

for(int i=0;i<=15;i++)
{
btn1[i].addActionListener(this);// 注册监听器
}

c1.addItemListener(this); 
c2.addItemListener(this); 
c3.addItemListener(this); 
 
//关闭窗口
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
  }
public void itemStateChanged(ItemEvent e)
{
if(c1.getState()==true)  
{
for(int i=0;i<=9;i++)