为JButton单击打开Action Listener类的新窗口

时间:2014-08-16 13:29:29

标签: java swing user-interface jbutton actionlistener

在我的ActionListener类中,我的if语句提示用户输入字符串。当我尝试执行程序时,没有任何反应。在我添加JButton之前,拼写游戏将出现在一个小窗口中,并且可以输入文本,并显示是否给出正确拼写的消息。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame; 
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class spelling extends JFrame {

    private static final long serialVersionUID = 1L;

    JFrame frame = new JFrame();
    JButton button1;

    public spelling() {
        super("Question 1");
        setLayout(new FlowLayout());

        button1 = new JButton("Spelling game");
        add(button1);

        HandlerClass handler = new HandlerClass();
        button1.addActionListener(handler);
    }

    private class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            JFrame frame2 = new JFrame();
            String answer1 = JOptionPane.showInputDialog("recipracate, reciprocate, reciprokate");
            if (answer1.equals("reciprocate")) {
                JOptionPane.showMessageDialog(frame2, "recriprocate is the correct answer"); 
            }
            else {
                    JOptionPane.showMessageDialog(frame2, "is the wrong answer"); 
            }

                String answer2 = JOptionPane.showInputDialog("quintessence, quintessance, qwintessence");

            if (answer2.equals("quintessence")) {
                JOptionPane.showMessageDialog(frame2, "quintessence is the correct answer");
            }
            else {
                JOptionPane.showMessageDialog(frame2, "That is the wrong answer");
            }
        }
    }
}


import javax.swing.JFrame;

public class spellingmain {

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

1 个答案:

答案 0 :(得分:1)

您的完整示例提出了一些值得考虑的问题:

  • 应在event dispatch thread构建和操作Swing GUI对象。

  • 为避免使用NullPointerException,通常的做法是在常量上调用equals()方法,该方法已知为非空。

    "reciprocate".equals(answer1)
    
  • 通过包含相关文字,使您的错误对话更容易阅读。

    answer1 + " is the wrong answer"
    
  • 除非您要添加新功能,否则不要延长JFrame

  • 不要打开新框架needlessly;你可以使用现有的frame;消息对话框可能包含parentComponent,但不需要一个。

  • 点击问题上的取消来查看结果,以测试您的程序。考虑一下你打算如何处理这个问题。

经过测试的代码:

import java.awt.FlowLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame; 
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class Spelling extends JFrame {

    private static final long serialVersionUID = 1L;

    JFrame frame = new JFrame();
    JButton button1;

    public Spelling() {
        super("Questionss");
        setLayout(new FlowLayout());

        button1 = new JButton("Spelling game");
        add(button1);

        HandlerClass handler = new HandlerClass();
        button1.addActionListener(handler);
    }

    private class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            String answer1 = JOptionPane.showInputDialog("recipracate, reciprocate, reciprokate");
            if ("reciprocate".equals(answer1)) {
                JOptionPane.showMessageDialog(null, "recriprocate is the correct answer"); 
            }
            else {
                JOptionPane.showMessageDialog(null, answer1 + " is the wrong answer"); 
            }

            String answer2 = JOptionPane.showInputDialog("quintessence, quintessance, qwintessence");
            if ("quintessence".equals(answer2)) {
                JOptionPane.showMessageDialog(null, "quintessence is the correct answer");
            }
            else {
                JOptionPane.showMessageDialog(null, answer2 + " is the wrong answer");
            }
        }
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            Spelling test = new Spelling();
            test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            test.pack();
            test.setVisible(true);
        });
    }
}