【问题标题】:Open new window for Action Listener class for a JButton click为 JButton 单击打开 Action Listener 类的新窗口
【发布时间】:2014-08-17 04:43:11
【问题描述】:

在我的 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);
    }
}

【问题讨论】:

  • 你的代码看起来很好,对我来说运行正常。

标签: java swing user-interface jbutton actionlistener


【解决方案1】:

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

  • Swing GUI 对象应event dispatch thread 上构造和操作。

  • 为避免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);
        });
    }
}

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多