小程序:加法计算器
你们好,这个计算器我用MyEclipse编译没有错误,但是运行的时候算不了加法,大家能帮我看看吗,谢谢了!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class calculator extends JPanel implements ActionListener{
JButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0;
JButton addbutton;
JButton reset;
JButton equalbutton;
JTextField result;
int flag =0,calresult;
Integer rt,rs;
String current; //当前值
String tem; //显示屏上的值
StringBuffer number;
public calculator( ) {
rt = new Integer(0);
rs =new Integer(0);
number =new StringBuffer( );
result = new JTextField(8);
result.setHorizontalAlignment(JTextField.RIGHT);
bt1 =new JButton("1");
bt1.addActionListener(this);
bt2 =new JButton("2");
bt2.addActionListener(this);
bt3 =new JButton("3");
bt3.addActionListener(this);
bt4 =new JButton("4");
bt4.addActionListener(this);
bt5 =new JButton("5");
bt5.addActionListener(this);
bt6 =new JButton("6");
bt6.addActionListener(this);
bt7 =new JButton("7");
bt7.addActionListener(this);
bt8 =new JButton("8");
bt8.addActionListener(this);
bt9 =new JButton("9");
bt9.addActionListener(this);
bt0 =new JButton("0");
bt0.addActionListener(this);
addbutton=new JButton("+");
addbutton.addActionListener(this);
reset = new JButton("reset");
reset.addActionListener(this);
setLayout(new BorderLayout(20,20));
add(result,BorderLayout.NORTH);
JPanel numberpanel=new JPanel();
numberpanel.setLayout(new GridLayout(4,3,5,5));
numberpanel.add(bt1);
numberpanel.add(bt2);
numberpanel.add(bt3);
numberpanel.add(bt4);
numberpanel.add(bt5);
numberpanel.add(bt6);
numberpanel.add(bt7);
numberpanel.add(bt8);
numberpanel.add(bt9);
numberpanel.add(bt0);
numberpanel.add(addbutton);
add(numberpanel,BorderLayout.CENTER);
JPanel controlpanel=new JPanel( );
controlpanel.setLayout(new GridLayout(1,2));
controlpanel.add(reset);
equalbutton=new JButton("=");
equalbutton.addActionListener(this);
equalbutton.setEnabled(false);
controlpanel.add(equalbutton);
add(controlpanel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent evt){
Object source =evt.getSource();
if(source == bt1)number.append("1");
else if (source == bt2)number.append("2");
else if (source == bt3)number.append("3");
else if (source == bt4)number.append("4");
else if (source == bt5)number.append("5");
else if (source ==bt6)number.append("6");
else if (source ==bt7)number.append("7");
else if (source ==bt8)number.append("8");
else if (source ==bt9)number.append("9");
else if (source ==bt0)number.append("0");
result.setText(number.toString());
if(flag ==0)
{current = number.toString();
equalbutton.setEnabled(false);
}
if(flag == 1)
{
tem=number.toString();
equalbutton.setEnabled(true);
}
if(source == addbutton)
{flag = 1;number=new StringBuffer();
equalbutton.setEnabled(false);}
if(source == equalbutton)
{
rs=Integer.valueOf(tem);
rt=Integer.valueOf("");
if(flag== 1)
calresult=rt.intValue()+rs.intValue();
result.setText(String.valueOf(calresult));
flag=0;
equalbutton.setEnabled(false);
number=new StringBuffer();
}
if(source == reset)
{number = new StringBuffer();
equalbutton.setEnabled(false);
flag=0;
}
}
public static void main(String set[ ]){
JFrame count = new JFrame("计算器");
count.addWindowListener(new WindowAdapter( ){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});