一个关于计算器的问题
我得计算器现在只完成了一部分 但是它却部能输出结果 晕呢import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Jisuan extends JFrame implements ActionListener{
private JButton btn1;
private JButton btn2;
private JButton btn3;
private JButton btn4;
private JButton btn5;
private JButton btn6;
private JButton btn7;
private JButton btn8;
private JButton btn9;
private JButton btn10;
private JButton btn11;
private JButton btn12;
private JButton btn13;
private JButton btn14;
private JButton btn15;
private JButton btn16;
private JTextField text;
private BorderLayout layout1;
private GridLayout layout2;
private Container cp;
private JPanel panel;
private int a;
private int b;
private int c;
private int d;
public Jisuan(){
super("简易计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
layout1=new BorderLayout();
layout2=new GridLayout(4,4,6,6);
btn1=new JButton("1");
btn2=new JButton("2");
btn3=new JButton("3");
btn4=new JButton("4");
btn5=new JButton("5");
btn6=new JButton("6");
btn7=new JButton("7");
btn8=new JButton("8");
btn9=new JButton("9");
btn10=new JButton("0");
btn11=new JButton("+");
btn12=new JButton("-");
btn13=new JButton("*");
btn14=new JButton("/");
btn15=new JButton("=");
btn16=new JButton("清空");
text=new JTextField(10);
cp=getContentPane();
cp.add(text,"North");
panel=new JPanel(layout2);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btn7);
panel.add(btn8);
panel.add(btn9);
panel.add(btn10);
panel.add(btn11);
panel.add(btn12);
panel.add(btn13);
panel.add(btn14);
panel.add(btn15);
panel.add(btn16);
cp.add(panel,"South");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn10.addActionListener(this);
btn11.addActionListener(this);
btn12.addActionListener(this);
btn13.addActionListener(this);
btn14.addActionListener(this);
btn15.addActionListener(this);
btn16.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
boolean flag=true;
char op;
if(e.getSource()==btn1)
{ if(flag)
{a=1;text.setText(String.valueOf(a));}
else
{ b=1;text.setText(String.valueOf(b));}}
if(e.getSource()==btn2)
{ if(flag)
{a=2;text.setText(String.valueOf(a));}
else
{ b=2;text.setText(String.valueOf(b));}}
/*if(e.getSource()==btn2)
{ if(flag)
{a=2;text.setText(String.valueOf(a));}
else
{b=2;text.setText(String.valueOf(b));}}
if(e.getSource()==btn3)
if(flag)
a=3;
else b=3;
if(e.getSource()==btn4)
if(flag)
a=4;
else b=4;*/
if(e.getSource()==btn11)
{op='+';c=1;flag=false;}
/*if(e.getSource()==btn12)
{op='-';c=2;}
if(e.getSource()==btn13)
{op='*';c=3;}
if(e.getSource()==btn14)
{op='/';c=4;}*/
if(e.getSource()==btn15)
{ flag=true;
d=a+b;
text.setText(String.valueOf(d));
}
if(e.getSource()==btn16)
{text.setText("");
}
}
public static void main(String[] args){
Jisuan jj=new Jisuan();
jj.setVisible(true);
jj.pack();
}
} 当你输入1+1得时候不可以得出正确得结果
[此贴子已经被作者于2006-3-31 21:21:27编辑过]