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);
}
}不好意思 麻烦各位一下
代码全贴出来了
解决方案 »
- Struts2 json 返回 报了个异常
- Struts2中的中文文件下载乱码问题!
- hibernate+spring中使用openSession().connection()不能关闭问题。。。
- 我的网页素材不显示 朋友们来帮帮忙~
- 急求关于weblogic的问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- JSP中自定义标签嵌套的问题,请高手指教:)
- (讨论)到底什么样的Web应用程序才能称得上是基于J2EE的?
- 关于事务的问题?
- 怎么声明一个本身带有""(双引号)的String
- navicat中文竖着展示,有没有人遇到这个问题
- Hibernate 3 查询遇到的一个问题! 50分求
- 新手求教:使用PowerDesigner建完MySql的数据表后,代码里没有我增加的外键代码...急..
if(e.setSource()==bt[i]) //应该改为: e.getSource()
{
str=str+e.getActionCommand();
tf.setText(str);
}