读书人

java计算器

发布时间: 2012-01-20 18:53:53 作者: rapoo

java计算器,高手请进
下面是我写的java计算器的代码,可是生成的计算器中,按键反映都是 "0.0 ",高手帮忙啊!
到底是怎么会事啊?
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener {

private double a=0,b=0,sum=0;
int flag=0,count=0,p=1;
char mode= '? ';//运算符
private TextField display =new TextField();
private Button one =new Button( "1 ");
private Button two =new Button( "2 ");
private Button three =new Button( "3 ");
private Button four =new Button( "4 ");
private Button five =new Button( "5 ");
private Button six =new Button( "6 ");
private Button seven =new Button( "7 ");
private Button eight =new Button( "8 ");
private Button nine =new Button( "9 ");
private Button zero =new Button( "0 ");
private Button point =new Button( ". ");
private Button add =new Button( "+ ");
private Button sub =new Button( "- ");
private Button mul =new Button( "* ");
private Button div =new Button( "/ ");
private Button result =new Button( "= ");
private Button clr =new Button( "CLR ");
private Button quit =new Button( "quit ");
private Label lable =new Label( "计算器 ");

private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

private void setup()//布局管理
{
Panel top= new Panel();
top.setLayout(new GridLayout(2,1));
top.add(lable);
top.add(display);
Panel number=new Panel();
number.setLayout(new GridLayout(6,3));
number.add(one);
number.add(two);
number.add(three);
number.add(four);
number.add(five);
number.add(six);
number.add(seven);
number.add(eight);
number.add(nine);
number.add(zero);
number.add(add);
number.add(sub);
number.add(point);
number.add(mul);
number.add(div);
number.add(result);
number.add(clr);
number.add(quit);
setLayout(new BorderLayout());
add( "North ",top);
add( "South ",number);

}

public Calculator()
{//构造方法
super( "计算器 ");
setup();
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);


nine.addActionListener(this);
zero.addActionListener(this);
point.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
result.addActionListener(this);
clr.addActionListener(this);
quit.addActionListener(this);
addWindowListener(new WindowCloser());
pack();
setSize(200,220);
setVisible(true);
}

void reset()
{
a=0;b=0;count=0;sum=0;p=1;mode= '? ';
}

void setAndDis(char c)
{
count=0;flag=0;
a=Double.parseDouble(display.getText());
mode=c;
}

public void actionPerformed(ActionEvent ae)//时间相应
{
if((ae.getSource()==quit))
System.exit(0);
if((ae.getSource()==clr))
{
reset();
flag=0;
display.setText( "0 ");
}
else
if((ae.getSource()==zero))
{
if(count!=0){reset();}
if(flag==0)
{
display.setText( "0 ");
flag=1;
}
else
display.setText(display.getText()+ "0 ");
}


//0-9键的响应
if((ae.getSource()==one))
{
if(count!=0){reset();}
if((flag==0)||display.getText().equals( "0 "))
{
display.setText( "1 ");
flag=1;
}
else
display.setText(display.getText()+ "1 ");

}

if((ae.getSource()==two))
{
if(count!=0){reset();}


if(flag==0||display.getText().equals( "0 "))
{
display.setText( "2 ");
flag=1;
}
else
display.setText(display.getText()+ "2 ");
}

if((ae.getSource()==three))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "3 ");
flag=1;
}
else
display.setText(display.getText()+ "3 ");
}

if((ae.getSource()==four))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "4 ");
flag=1;
}
else
display.setText(display.getText()+ "4 ");
}

if((ae.getSource()==five))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "5 ");
flag=1;
}
else
display.setText(display.getText()+ "5 ");
}

if((ae.getSource()==six))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "6 ");
flag=1;
}
else
display.setText(display.getText()+ "6 ");
}

if((ae.getSource()==seven))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "7 ");
flag=1;
}
else
display.setText(display.getText()+ "7 ");


}

if((ae.getSource()==eight))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "8 ");
flag=1;
}
else
display.setText(display.getText()+ "8 ");
}

if((ae.getSource()==nine))
{
if(count!=0){reset();}
if(flag==0||display.getText().equals( "0 "))
{
display.setText( "9 ");
flag=1;
}
else
display.setText(display.getText()+ "9 ");
}

if((ae.getSource()==point))
{
if(flag==1&&p!=0)
{
display.setText(display.getText()+ ". ");
p=0;
}
}
//运算符的响应
if(ae.getSource()==add)
{
setAndDis( '+ ');
}

if(ae.getSource()==sub)
{
setAndDis( '- ');
}

if(ae.getSource()==mul)
{
setAndDis( '* ');
}

if(ae.getSource()==div)
{
setAndDis( '/ ');
}
//结果的响应
if((ae.getSource()==result)&&mode!= '? ');
{
flag=0;
if(count==0)
{
b=Double.parseDouble(display.getText());
count=1;
}
sum=run(a,b,mode);
a=sum;
display.setText(String.valueOf(sum));
}
}
//计算
public double run(double a,double b,char mode)
{
double sun=0;


if(mode== '+ ')sum=a+b;
if(mode== '- ')sum=a-b;
if(mode== '* ')sum=a*b;
if(mode== '/ ')sum=a/b;
return(sum);
}

public static void main (String[] args)
{
Calculator c=new Calculator();
}
}


[解决办法]
看下面代码,第一行多了分号.
//结果的响应
if((ae.getSource()==result)&&mode!= '? ');
{
flag=0;
if(count==0)
{
b=Double.parseDouble(display.getText());
count=1;
}
sum=run(a,b,mode);
a=sum;
display.setText(String.valueOf(sum));
}
}
[解决办法]
大家也帮我看哦
呵呵
我的“退出”不成功
还有个问题:
当只输入 一个操作数的时候 重置 失效
需要输入两个操作数 才可以
import java.awt.*;
import java.awt.event.*;
public class keke extends Frame implements ActionListener
{

Button a1,a2,a3,a4,a5,a6;
TextField text1,text2,text3;
Double a,b;
Frame f1;

keke()
{

super( "简单计算器 ");
setSize(300,250);
setVisible(true);

Panel pan=new Panel();
pan.setBackground(Color.cyan);

Panel pan2=new Panel();
pan2.setBackground(Color.blue);

setLayout(new BorderLayout());
add( "Center ",pan);
add( "East ",pan2);

Label la=new Label( "操作数 ");
text1=new TextField(5);
text1.getText();
Label lb=new Label( "操作数 ");
text2=new TextField(5);
text2.getText();
Label lc=new Label( "结果 ");
text3=new TextField(5);
// text3.setEditable(false);
a1=new Button ( "+ ");
a2=new Button ( "- ");
a3=new Button ( "* ");
a4=new Button ( "/ ");
a5=new Button ( "重置 ");
a6=new Button ( "退出 ");



pan.setLayout(new GridLayout(3,2));
pan.add(la);
pan.add(text1);
pan.add(lb);
pan.add(text2);
pan.add(lc);
pan.add(text3);
pan2.setLayout(new GridLayout(3,2));
pan2.add(a1);
pan2.add(a2);
pan2.add(a3);
pan2.add(a4);
pan2.add(a5);
pan2.add(a6);

a1.addActionListener(this);
a2.addActionListener(this);
a3.addActionListener(this);
a4.addActionListener(this);
a5.addActionListener(this);
a6.addActionListener(this);
addWindowListener(new WindowCloser());






pack();







}
public void actionPerformed(ActionEvent e)

{
//a=Double.valueOf(text1.getText()).doubleValue();
// b=Double.valueOf(text2.getText()).doubleValue();

a=Double.parseDouble(text1.getText());
b=Double.parseDouble(text2.getText());

if(e.getSource()==a1)
{text3.setText(String.valueOf(a+b));}

if(e.getSource()==a2)
{text3.setText(String.valueOf(a-b));}

if(e.getSource()==a3)
{text3.setText(String.valueOf(a*b));}


if(e.getSource()==a4)
{text3.setText(String.valueOf(a/b));}

if(e.getSource()==a5)
{
text1.setText( " ");
text2.setText( " ");
text3.setText( " "); }
if(e.getSource()==a6)
System.exit(0);



}

private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

public static void main(String args[])
{

new keke();

}

}


读书人网 >J2SE开发

热点推荐