Java 为JButton单击打开Action Listener类的新窗口
在ActionListener类中,我有if语句提示用户输入字符串。当我试图执行程序时,什么也没有发生。在我添加JButton之前,拼写游戏会出现在一个小窗口中,可以输入文本,并显示一条消息,显示拼写是否正确Java 为JButton单击打开Action Listener类的新窗口,java,swing,user-interface,jbutton,actionlistener,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.swi
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对象应仅在上构造和操作
- 为了避免出现
,通常的做法是对常量调用NullPointerException
方法,该常量已知为非nullequals()
"reciprocate".equals(answer1)
- 通过包含相关文本,使错误对话框更易于阅读
answer1 + " is the wrong answer"
- 除非要添加新功能,否则不要扩展JFrame
- 不要打开新的框架;您可以使用现有的
;消息对话框可能有一个框架
,但不是必需的父组件
- 通过单击问题上的“取消”来测试您的程序,以查看结果。考虑一下你打算如何处理这个问题。
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);
});
}
}
您的代码似乎很好,对我来说运行正常。