import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Computer implements ActionListener
{
Frame f;
TextField tf;
Panel p1,p2,p3;
Button bt[] = new Button[10];
// Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button jia,jian,cheng,chu,deng;
Button ce,backspace,c,nz,dian;
////////////////////////////////////////////////////
float result=0.0f;
String str="";
boolean flag=true;
int i=0;
////////////////////////////////////////////////////
public void display()
{
f= new Frame("计算器");
f.setSize(300,400);
f.setLocation(320,240);
f.setBackground(Color.lightGray);
// f.setLayout(new FlowLayout(FlowLayout.LEFT));
tf = new TextField(30);
tf.setEditable(false);
f.add(tf);
for(i=0;i<=9;i++)
{
bt[i]= new Button(Integer.toString(i));
p1.add(bt[i]);
bt[i].addActionListener(this);
}
nz=new Button("+/-");
dian=new Button(".");
nz.addActionListener(this);
dian.addActionListener(this);
p1.add(nz);
p1.add(dian);
p1.setLayout(new GridLayout(4,3));
jia=new Button("+");
jian=new Button("-");
cheng=new Button("*");
chu=new Button("/");
deng=new Button("=");
ce=new Button("CE");
backspace=new Button("BACKSPACE");
c=new Button("C");
f.add(jia);
f.add(jian);
f.add(cheng);
f.add(chu);
f.add(deng);
f.add(ce);
f.add(backspace);
f.add(c);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
deng.addActionListener(this);
ce.addActionListener(this);
backspace.addActionListener(this);
c.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
for(i=0;i<=9;i++)
{
if(e.setSource()==bt[i])
{
str=str+e.getActionCommand();
tf.setText(str);
}
else if(e.getSource()==dian)
{
str=str+".";
tf.setText(str);
}
else if(e.getSource()==jia)
{
result=result+Float.parseFloat(str);
str="";
tf.setText(""+result);
}
else if(e.getSource()==jian)
{
result=result-Float.parseFloat(str);
str="";
tf.setText(""+result);
}
else if(e.getSource()==cheng)
{
result=result*Float.parseFloat(str);
str="";
tf.setText(""+result);//?
}
else if(e.getSource()==chu)
{
if(Float.parseFloat(str)==0)
System.out.print("除数不能为零");
else
{
result=result/Float.parseFloat(str);
str="";
tf.setText(""+result);
}
}
else if(e.getSource()==ce)
{
result=0.0f;
str="";
tf.setText("0.");
}
else if(e.getSource()==c)
{
result=0.0f;
str="";
tf.setText("0.");
}
else if(e.getSource()==nz)
{ if(flag==false)
{
str='-'+str;
tf.setText(str);
flag=true;
}
else if(flag==true)
{
str=str.substring(1,str.length());
tf.setText(str);
flag=false;
}
}
else if(e.getSource()==backspace)
{
str=str.substring(0,str.length()-1);
tf.setText(str);
}
}
}
public static void main(String args[])
{
(new Computer()).display();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}不好意思 麻烦各位一下
代码全贴出来了
import java.awt.event.*;
import java.lang.*;
public class Computer implements ActionListener
{
Frame f;
TextField tf;
Panel p1,p2,p3;
Button bt[] = new Button[10];
// Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
Button jia,jian,cheng,chu,deng;
Button ce,backspace,c,nz,dian;
////////////////////////////////////////////////////
float result=0.0f;
String str="";
boolean flag=true;
int i=0;
////////////////////////////////////////////////////
public void display()
{
f= new Frame("计算器");
f.setSize(300,400);
f.setLocation(320,240);
f.setBackground(Color.lightGray);
// f.setLayout(new FlowLayout(FlowLayout.LEFT));
tf = new TextField(30);
tf.setEditable(false);
f.add(tf);
for(i=0;i<=9;i++)
{
bt[i]= new Button(Integer.toString(i));
p1.add(bt[i]);
bt[i].addActionListener(this);
}
nz=new Button("+/-");
dian=new Button(".");
nz.addActionListener(this);
dian.addActionListener(this);
p1.add(nz);
p1.add(dian);
p1.setLayout(new GridLayout(4,3));
jia=new Button("+");
jian=new Button("-");
cheng=new Button("*");
chu=new Button("/");
deng=new Button("=");
ce=new Button("CE");
backspace=new Button("BACKSPACE");
c=new Button("C");
f.add(jia);
f.add(jian);
f.add(cheng);
f.add(chu);
f.add(deng);
f.add(ce);
f.add(backspace);
f.add(c);
jia.addActionListener(this);
jian.addActionListener(this);
cheng.addActionListener(this);
chu.addActionListener(this);
deng.addActionListener(this);
ce.addActionListener(this);
backspace.addActionListener(this);
c.addActionListener(this);
f.addWindowListener(new WinClose());
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
for(i=0;i<=9;i++)
{
if(e.setSource()==bt[i])
{
str=str+e.getActionCommand();
tf.setText(str);
}
else if(e.getSource()==dian)
{
str=str+".";
tf.setText(str);
}
else if(e.getSource()==jia)
{
result=result+Float.parseFloat(str);
str="";
tf.setText(""+result);
}
else if(e.getSource()==jian)
{
result=result-Float.parseFloat(str);
str="";
tf.setText(""+result);
}
else if(e.getSource()==cheng)
{
result=result*Float.parseFloat(str);
str="";
tf.setText(""+result);//?
}
else if(e.getSource()==chu)
{
if(Float.parseFloat(str)==0)
System.out.print("除数不能为零");
else
{
result=result/Float.parseFloat(str);
str="";
tf.setText(""+result);
}
}
else if(e.getSource()==ce)
{
result=0.0f;
str="";
tf.setText("0.");
}
else if(e.getSource()==c)
{
result=0.0f;
str="";
tf.setText("0.");
}
else if(e.getSource()==nz)
{ if(flag==false)
{
str='-'+str;
tf.setText(str);
flag=true;
}
else if(flag==true)
{
str=str.substring(1,str.length());
tf.setText(str);
flag=false;
}
}
else if(e.getSource()==backspace)
{
str=str.substring(0,str.length()-1);
tf.setText(str);
}
}
}
public static void main(String args[])
{
(new Computer()).display();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}不好意思 麻烦各位一下
代码全贴出来了
解决方案 »
- java中split的求助。
- JavaEE中,写方法怎样根据传入的参数的类型返回对应类型的值
- 散发!大家分享下在工作当中遇到的技术问题!
- 如何用HQL分组并排序后获得每组前十名
- mysql 中文显示乱码问题, 急!高分求助
- 使用在Tomcat上部署Spring+Hibernate遇到了一个非常奇怪的问题
- 100分!关于用xml替换掉structs中的tag
- 请前辈指点迷津
- 关于SUN的J2EE服务器的调试
- Norwaywoods请来,刚才分太少,追加给分 谢谢
- Hibernate 3 查询遇到的一个问题! 50分求
- 新手求教:使用PowerDesigner建完MySql的数据表后,代码里没有我增加的外键代码...急..
if(e.setSource()==bt[i]) //应该改为: e.getSource()
{
str=str+e.getActionCommand();
tf.setText(str);
}