Java:Joptionpane消息对话框无法打开

时间:2012-06-02 18:21:26

标签: java message calculator

我决定制作一个简洁的小计算器程序,但是现在,当我按下计算并运行计算脚本时,它没有打开一个表示答案的框。我找不到问题所在。有人可以请你告诉我吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Gui extends JFrame{

    private JTextField text;
    private JTextField text2;
    private JRadioButton plus;
    private JRadioButton divide;
    private JRadioButton multiply;
    private JRadioButton subtract;
    private ButtonGroup group;
    private JButton button;
    private int str = 1;
    private String txt;
    private String txt2;
    private int num1;
    private int num2;
    private int ans;

    public Gui(){

        super("Calculator");
        setLayout(new FlowLayout());

        text = new JTextField(10);
        text2 = new JTextField(10);
        add(text);
        add(text2);
        plus = new JRadioButton("Add", true);
        divide = new JRadioButton("Divide", false);
        multiply = new JRadioButton("Multiply", false);
        subtract = new JRadioButton("Subtract", false);
        group = new ButtonGroup();
        group.add(plus);
        group.add(divide);
        group.add(multiply);
        group.add(subtract);
        add(plus);
        add(divide);
        add(multiply);
        add(divide);
        add(subtract);
        button = new JButton("Calculate");
        add(button);

        plus.addItemListener(
                new ItemListener(){
                    public void itemStateChanged(ItemEvent event){
                        str = 1;
                        System.out.println(str);
                    }
                }
            );
        divide.addItemListener(
                new ItemListener(){
                    public void itemStateChanged(ItemEvent event){
                        str = 2;
                        System.out.println(str);
                    }
                }
            );
        multiply.addItemListener(
                new ItemListener(){
                    public void itemStateChanged(ItemEvent event){
                        str = 3;
                        System.out.println(str);
                    }
                }
            );
        subtract.addItemListener(
                new ItemListener(){
                    public void itemStateChanged(ItemEvent event){
                        str = 4;
                        System.out.println(str);
                    }
                }
            );
        HandlerClass handler = new HandlerClass();
        button.addActionListener(handler);

    }

    private class HandlerClass implements ActionListener{
        public void ActionPerformed(ActionEvent event){
                txt = text.getText();
                txt2 = text2.getText();
                num1 = Integer.parseInt(txt);
                num2 = Integer.parseInt(txt2);
                if(str==1){
                    ans=num1+num2;
                }
                if(str==2){
                    ans=num1/num2;
                }
                if(str==3){
                    ans=num1*num2;
                }
                if(str==4){
                    ans=num1-num2;
                }
                JOptionPane.showMessageDialog(null, String.format("Answer: %s", ans));

            }
        }
    }

如果你需要,这是运行它的脚本:

import javax.swing.JFrame;

public class apples {
    public static void main(String[] args){
        Gui bucky = new Gui();
        bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bucky.setSize(300,150);
        bucky.setVisible(true);
    }
}

单击“计算”时出现错误代码:

线程中的异常“AWT-EventQueue-0”java.lang.Error:未解决的编译问题:     Gui.HandlerClass类型必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent)

at Gui$HandlerClass.actionPerformed(Gui.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:2)

问题可能出在听众身上。您的所有单选按钮都使用ItemListener,这很好,但您的JButton也是如此:用于JButton的标准侦听器是ActionListener。如果您将ItemListener更改为ActionListener(并将事件更改为ActionEvent),则应该有效。

未触发item事件的原因是JButton only fires the event if setSelected is called

更新:新代码中的错误是您将ActionPerformed而不是actionPerformed作为方法名称。案件很重要。

更新2 :在声明所有变量的地方,添加以下变量:

private ArrayList<JRadioButton> radioButtons = new ArrayList<JRadioButton>

如果您尚未导入,则可能需要导入java.util.ArrayList

然后,在行

之下
plus = new JRadioButton("Add", true);
divide = new JRadioButton("Divide", false);
multiply = new JRadioButton("Multiply", false);
subtract = new JRadioButton("Subtract", false);

添加以下内容:

radioButtons.add(plus);
radioButtons.add(divide);
radioButtons.add(multiply);
radioButtons.add(subtract);

当您需要检查选择了哪个按钮时,请使用以下代码:

for (JRadioButton button : radioButtons) { // Iterate over each button in the list
    if (button.isSelected()) { // If the button is selected...
        // do something
    }
}

你的其他代码看起来很好,但我建议使用String常量(或者更好的是枚举)来管理+ - / *操作。也就是说,您应该声明以下枚举,而不是str = "add"(等人)。

private enum OperationType {
    ADD, SUBTRACT, MULTIPLY, DIVIDE;
}

并更改

private String str = "add"

private OperationType op = OperationType.ADD;

这样做的结果是输入中的任何错误(包括tpyo或aCcidental资本)都会导致字符串无效,并且你会得到奇怪的行为。

这也意味着您可以使用switch语句:而不是使用大量if .. else语句,请尝试以下操作:

switch (op):
    case ADD:
        // blah
        break;
    case SUBTRACT:
        // blah
        break;
    case MULTIPLY:
        // blah
        break;
    case DIVIDE:
        // blah
        break;
    default:
        System.out.println("Error: unsupported operation");
        // or whatever
}

这通常被认为是更干净的代码,它看起来更好,更易于维护。

祝你好运!

答案 1 :(得分:2)

您正在尝试运行无法编译的代码 - 不要这样做!

而是在编译时修复所有编译问题,然后仅在代码编译良好时运行。

首先:您需要正确地将actionPerformed(...)大写。

下一步:您的JRadioButtons上不应该有任何侦听器。而是检查仅在ActionListener中按下哪个按钮用于主计算JButton。原因是你不关心用户是否选择了一个JRadioButton然后后者改变主意并选择另一个,而是你只关心在按下计算JButton时选择了 JRadioButton。