初学乍练,请各位多多指教.指出不足.
import java.awt.*;
import java.awt.event.*;
class ae
{
public static void main(String args[])
{
frame fr=new frame();
}
}
class frame extends Frame implements ActionListener {
Button button[]=new Button[18];
TextField tx=new TextField(20);
int m=0;String str1="";String str2="",strsum;double d1=0,d2,dsum; //str1为第一个操作字符串,d1是str1对应的数,
//str2为第二个,d2是str2对应的数,dsum为结果
//m为判断是何种运算
String buttontext[]={"0","1","2","3","4","5","6","7","8","9","+","-","*","/","%","=","C","."};
public frame()
{
super("计算器");
this.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
this.add(tx);
boolean b=false;
tx.setEditable(b);
for(int i=0;i<=17;i++)
{
button[i]=new Button(buttontext[i]);
this.add(button[i]);
button[i].addActionListener(this);
}
this.addWindowListener(new WindowAdapter()//窗口事件监听器
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
this.pack();setSize(193,200);this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button[0]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"0";tx.setText(str1);}
else if(!tx.getText().equals("0")&&(m!=0)){str2=str2+"0";tx.setText(str2);}}
else if(e.getSource() == button[1]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"1";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"1";tx.setText(str2);}}
else if(e.getSource() == button[2]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"2";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"2";tx.setText(str2);}}
else if(e.getSource() == button[3]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"3";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"3";tx.setText(str2);}}
else if(e.getSource() == button[4]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"4";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"4";tx.setText(str2);}}
else if(e.getSource() == button[5]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"5";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"5";tx.setText(str2);}}
else if(e.getSource() == button[6]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"6";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"6";tx.setText(str2);}}
else if(e.getSource() == button[7]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"7";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"7";tx.setText(str2);}}
else if(e.getSource() == button[8]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"8";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"8";tx.setText(str2);}}
else if(e.getSource() == button[9]){
if(!tx.getText().equals("0")&&m==0){str1=str1+"9";tx.setText(str1);}
else if(!tx.getText().equals("0")&&m!=0){str2=str2+"9";tx.setText(str2);}}
else if(e.getSource() == button[17]&&(!tx.getText().equals(""))){if(tx.getText().indexOf(".")==-1) //小数点
{if(m==0){str1=str1+".";tx.setText(str1);}
else if(m!=0){str2=str2+".";tx.setText(str2);}}}
else if((e.getSource() == button[10])&&(!tx.getText().equals(""))&&(!tx.getText().equals("0"))){str1=tx.getText
();d1=Double.parseDouble(str1);m=1;}//加
else if(e.getSource() == button[11]){
if(!tx.getText().equals("")){str1=tx.getText();d1=Double.parseDouble(str1);m=2;} //减,可做负号用.
else {str1=str1+"-";tx.setText(str1);}}
else if(e.getSource() == button[12]&&(!tx.getText().equals(""))){str1=tx.getText();d1=Double.parseDouble(str1);m=3;} //乘
else if(e.getSource() == button[13]&&(!tx.getText().equals(""))){str1=tx.getText();d1=Double.parseDouble(str1);m=4;} //除
else if(e.getSource() == button[14]&&(!tx.getText().equals(""))){str1=tx.getText();d1=Double.parseDouble(str1);m=5;}//求模
else if(e.getSource() == button[15]&&(!tx.getText().equals(""))){str2=tx.getText();d2=Double.parseDouble(str2); //运算
switch(m){case 1:dsum=d1+d2;tx.setText(Double.toString(dsum));break;
case 2:dsum=d1-d2;tx.setText(Double.toString(dsum));break;
case 3:dsum=d1*d2;tx.setText(Double.toString(dsum));break;
case 4:dsum=d1/d2;tx.setText(Double.toString(dsum));break;
case 5:dsum=d1%d2;tx.setText(Double.toString(dsum));break;
}
str1="";str2="";m=0;
}
else if((e.getSource() == button[16])){tx.setText("");str1="";str2="";} //清零
}
}
[此贴子已经被作者于2006-6-5 15:42:48编辑过]