当前位置: 代码迷 >> J2SE >> 为何编写的计算器不能运算
  详细解决方案

为何编写的计算器不能运算

热度:961   发布时间:2013-02-25 00:00:00.0
为什么编写的计算器不能运算?
Windows操作系统自带的计算器是个很方便的小工具,利用Java的GUI编程,实现一个Java GUI计算器应用程序界面,窗口标题为“计算器”,窗口布局如下图所示,在此计算器应用程序中实现“+、-、*、/”运算操作。
代码:import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class T11 extends JFrame implements ActionListener{
int deng=0,fu=0,i=0,point=0,n=0;
float sum=0,equ;
String v="";
BorderLayout bo=new BorderLayout();
GridLayout g=new GridLayout(4,4);
JPanel jp=new JPanel();
JTextField jt=new JTextField(" 0");
JButton b1=new JButton("7");
JButton b2=new JButton("8");
JButton b3=new JButton("9");
JButton b4=new JButton("+");
JButton b5=new JButton("4");
JButton b6=new JButton("5");
JButton b7=new JButton("6");
JButton b8=new JButton("-");
JButton b9=new JButton("1");
JButton b10=new JButton("2");
JButton b11=new JButton("3");
JButton b12=new JButton("*");
JButton b13=new JButton("0");
JButton b14=new JButton(".");
JButton b15=new JButton("=");
JButton b16=new JButton("/");
@SuppressWarnings("static-access")
public T11(String title)
{
super(title);
this.setSize(300,300);
jp.setSize(300,200);
jt.setSize(300,100);
jp.setLayout(g);

jp.add(b1);
jp.add(b2);
jp.add(b3);
jp.add(b4);
jp.add(b5);
jp.add(b6);
jp.add(b7);
jp.add(b8);
jp.add(b9);
jp.add(b10);
jp.add(b11);
jp.add(b12);
jp.add(b13);
jp.add(b14);
jp.add(b15);
jp.add(b16);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
this.add(jt,bo.NORTH);
this.add(jp,bo.CENTER);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
if(deng==1||fu==1)
v="";
v=v+"7";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b2)
{
if(deng==1||fu==1)
v="";
v=v+"8";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b3)
{
if(deng==1||fu==1)
v="";
v=v+"9";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b4)//+
{
if(n==1)
{
if(i==0)
sum=Float.parseFloat(v);
else if(i==1){sum+=Float.parseFloat(v);}
else if(i==2){sum-=Float.parseFloat(v);}
else if(i==3){sum*=Float.parseFloat(v);}
else if(i==4){sum/=Float.parseFloat(v);}
}
else
{
}
i=1;
fu=1;n=0;deng=0;point=0;

}
if(e.getSource()==b5)
{
if(deng==1||fu==1)
v="";
v=v+"4";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b6)
{
if(deng==1||fu==1)
v="";
v=v+"5";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b7)
{
if(deng==1||fu==1)
v="";
v=v+"6";
jt.setText(v);
deng=0;fu=0;n=1;
}
if(e.getSource()==b8)//-
{
if(n==1)
{
if(i==0)
  相关解决方案