在我的 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/