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

标签 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);
    }
}

最佳答案

您的完整示例提出了几个值得进一步考虑的问题:

  • Swing GUI 对象应该event dispatch thread 上构建和操作。 .

  • 为了避免 NullPointerException,常见的做法是对已知为非空的常量调用 equals() 方法。

    "reciprocate".equals(answer1)
    
  • 通过包含相关文本,使错误对话框更易于阅读。

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

  • 不要打开新框架 needlessly ;您可以使用现有的框架;消息对话框可能有一个 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);
        });
    }
}

关于java - 为 JButton 单击的 Action Listener 类打开新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340638/

相关文章:

Java Regex - 从 HTML anchor 提取链接

java - 如何在JAVA中找到我当前的时间在今天的时间和明天的日期之间

java - Netbeans swing 未正确显示组件

java - 如何更改 Material 设计抽屉导航中汉堡图标的颜色

python - pysimplegui 表多行文本处理

java - 从 Jboss EAP 6.0 提供静态文件

java - 添加到 ModelAndView 的变量似乎消失了

java - 限制 JSpinner 大小

java - 在 JEditorPane 中显示 XML 文件

user-interface - 如何将通用的 lisp 解释器嵌入到 gui 应用程序中