朋友们,我想要这个计算器实现加减乘除的功能
 但是在处理监听器和事件服务类的代码时可能出错了
 不知道错在哪里,请帮帮我,谢谢大家(还有两个box1,box2的服务类的方法体没有写,那个不用帮忙)package packege_one;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class JFrame_eight {
static JFrame frame=new JFrame("算术运算题测试");
static JPanel panel1=new JPanel();
static JPanel panelzong=new JPanel();
//static Random rnd=new Random();
static JLabel label1=new JLabel("");
static JLabel label2=new JLabel("");
static JLabel label3=new JLabel("");
static JLabel label4=new JLabel("");
static JTextField textfield=new JTextField("0000");
static JPanel panel11=new JPanel();
static JButton button=new JButton("确定");

static JPanel panel2=new JPanel();
static int totalNumber=0;
static int rightNumber=0;
static JLabel label5=new JLabel("已做"+totalNumber+"题");
static JLabel label6=new JLabel("做对"+rightNumber+"题");

static JPanel panel3=new JPanel();
static JRadioButton button1=new JRadioButton("做加法");
static JRadioButton button2=new JRadioButton("做减法");
static JRadioButton button3=new JRadioButton("做乘法");
static JRadioButton button4=new JRadioButton("做除法");
static ButtonGroup group=new ButtonGroup();

static JPanel panel4=new JPanel();
static JCheckBox box1=new JCheckBox("显示已做题数");
static JCheckBox box2=new JCheckBox("显示做对题数");

static JPanel panel5=new JPanel();
//static JPanel panel6=new JPanel();
public JFrame_eight()
{
panel1.setLayout(new FlowLayout());
panel1.add(label1);
panel1.add(label2);
panel1.add(label3);
panel1.add(label4);
panel1.add(textfield);
panel11.setLayout(new FlowLayout());
panel11.add(button);
panel2.setLayout(new FlowLayout());
panel2.add(label5);
panel2.add(label6);
panelzong.setLayout(new GridLayout(0,1));
panelzong.add(panel1);
panelzong.add(panel11);
panelzong.add(panel2);

panel3.setLayout(new GridLayout(0,1));
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(button4);

group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);

panel4.setLayout(new GridLayout(0,1));
panel4.add(box1);
panel4.add(box2);

frame.add(panelzong,BorderLayout.WEST);
frame.add(panel3,BorderLayout.CENTER);
frame.add(panel4,BorderLayout.EAST);

button1.setActionCommand("+");
button2.setActionCommand("-");
button3.setActionCommand("*");
button4.setActionCommand("/");

button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Random rnd=new Random();
int a,b;
a=rnd.nextInt(10000);
b=rnd.nextInt(10000);
label1.setText(Integer.toString(a));
label2.setText("+");
label4.setText("=");
label3.setText(Integer.toString(b));
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Random rnd=new Random();
int a,b;
a=rnd.nextInt(10000);
b=rnd.nextInt(10000);
label1.setText(Integer.toString(a));
label2.setText("-");
label4.setText("=");
label3.setText(Integer.toString(b));
}
});
button3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Random rnd=new Random();
int a,b;
a=rnd.nextInt(10000);
b=rnd.nextInt(10000);
label1.setText(Integer.toString(a));
label2.setText("*");
label4.setText("=");
label3.setText(Integer.toString(b));
}
});
button4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Random rnd=new Random();
int a,b;
a=rnd.nextInt(10000);
b=rnd.nextInt(10000);
label1.setText(Integer.toString(a));
label2.setText("/");
label4.setText("=");
label3.setText(Integer.toString(b));
}
});
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double a,b;
double c=0;
//a=Double.parseDouble(label1.getText());
//b=Double.parseDouble(label3.getText());
//Object source=e.getActionCommand();
if(e.getActionCommand().equals("+"))
{
a=Double.parseDouble(label1.getText());
b=Double.parseDouble(label3.getText());
c=a+b;
textfield.setText(Double.toString(c));
}
else if(e.getActionCommand().equals("-"))
{
a=Double.parseDouble(label1.getText());
b=Double.parseDouble(label3.getText());
c=a-b;
textfield.setText(Double.toString(c));
}
else if(e.getActionCommand().equals("*"))
{
a=Double.parseDouble(label1.getText());
b=Double.parseDouble(label3.getText());
c=a*b;
textfield.setText(Double.toString(c));
}
else if(e.getActionCommand().equals("/"))
{
a=Double.parseDouble(label1.getText());
b=Double.parseDouble(label3.getText());
c=a/b;
textfield.setText(Double.toString(c));
}
//textfield.setText(Double.toString(c));

}
});
box1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{

}
});
box2.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{

}
});
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
JFrame_eight jf=new JFrame_eight();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(panelzong);
frame.getContentPane().add(panel3);
frame.getContentPane().add(panel4);
frame.setSize(1000,500);
frame.setLocation(300,300);
frame.pack();
frame.setVisible(true);
}
}

解决方案 »

  1.   


    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    double a, b;
    double c = 0;
    // a=Double.parseDouble(label1.getText());
    // b=Double.parseDouble(label3.getText());
    // Object source=e.getActionCommand();
    if (label2.getText().equals("+")) {
    a = Double.parseDouble(label1.getText());
    b = Double.parseDouble(label3.getText());
    c = a + b;
    textfield.setText(Double.toString(c));
    } else if (label2.getText().equals("-")) {
    a = Double.parseDouble(label1.getText());
    b = Double.parseDouble(label3.getText());
    c = a - b;
    textfield.setText(Double.toString(c));
    } else if (label2.getText().equals("*")) {
    a = Double.parseDouble(label1.getText());
    b = Double.parseDouble(label3.getText());
    c = a * b;
    textfield.setText(Double.toString(c));
    } else if (label2.getText().equals("/")) {
    a = Double.parseDouble(label1.getText());
    b = Double.parseDouble(label3.getText());
    c = a / b;
    textfield.setText(Double.toString(c));
    }
    }
    });e.getActionCommand()得到的事件源,就是每次点button都会得到button的actionCommand,而不是(+-*/)