提问人:Afsal Baaqir A 提问时间:12/15/2021 最后编辑:TomerikooAfsal Baaqir A 更新时间:12/17/2021 访问量:88
需要在单击JFrame上的“退出”按钮时关闭JFrame
Need to close JFrame on clicking Exit button on JFrame
问:
我正在创建一个简单的 Java 应用程序,我是 Java 的新手。请帮我解决这个问题
我正在使用 java 8,我正在尝试从一个帧移动到另一个帧,我可以这样做,但为此,我在代码中发生了一些异常,我无法弄清楚为什么以及那是什么
下面是我的代码,错误发生在下面给出。
package oops;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class HumanBenchmarkApp implements ActionListener{
JFrame AppWindow, ReflexFrame, VisualFrame, CreditsFrame;
JLabel Title;
JButton Game1, Game2, Credits, Exit;
public HumanBenchmarkApp(){
AppWindow = new JFrame("Human Benchmark");
AppWindow.setSize(400, 400);
JPanel Content = new JPanel();
Content.setBorder(new EmptyBorder(10, 10, 10, 10));
Content.setLayout(new GridBagLayout());
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
System.err.println(e.getMessage());
}
AppWindow.setLayout(new BoxLayout(AppWindow.getContentPane(),BoxLayout.Y_AXIS));
Title = new JLabel("<html><h1><strong>Human Benchmark App</strong></h1><br></html>");
Game1 = new JButton("<html><h4>Reflex Action Test</h4></html>");
Game2 = new JButton("<html><h4>Visual Memory Test</h4></html>");
Credits = new JButton("<html><h4>Credits</h4></html>");
Exit = new JButton("<html><h4>Exit</h4></html>");
Game1.addActionListener(this);
Game2.addActionListener(this);
Credits.addActionListener(this);
Exit.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
Content.add(Title, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel Buttons = new JPanel(new GridBagLayout());
Buttons.add(Game1, gbc);
Buttons.add(Game2, gbc);
Buttons.add(Credits, gbc);
Buttons.add(Exit, gbc);
gbc.weighty = 1;
Content.add(Buttons);
AppWindow.add(Content);
AppWindow.setVisible(true);
AppWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new HumanBenchmarkApp();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton)e.getSource();
if (clicked.getName().equals("Game1")){
}
else if(clicked.getName().equals("Game2")){
}
else if(clicked.getName().equals("Credits")){
}
else if(clicked.getName().equals("Exit")){
System.exit(0);
}
}
}
这是我的代码,当我执行它时,我在 NetBeans 输出区域收到以下错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at oops.HumanBenchmarkApp.actionPerformed(HumanBenchmarkApp.java:56)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
错误在控制台上,JFrame 甚至没有关闭。
答:
1赞
Agzam
12/15/2021
#1
我真的不明白为什么会有如此令人困惑的实现,如果你可以使用 lambda 表达式:ActionListener
...
Game1.addActionListener(this);
Game2.addActionListener(this);
Credits.addActionListener(this);
Exit.addActionListener(e -> { // <---- NEW HERE
System.exit(0); // <---- NEW HERE
}); // <---- NEW HERE
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
Content.add(Title, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel Buttons = new JPanel(new GridBagLayout());
...
编辑:
或者你可以这样做:
Exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
艺术 您不必比较名称,可以比较对象引用:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class HumanBenchmarkApp implements ActionListener {
JFrame appWindow, reflexFrame, visualFrame, creditsFrame;
JLabel title;
JButton game1, game2, credits, exit;
public HumanBenchmarkApp() {
appWindow = new JFrame("Human Benchmark");
appWindow.setSize(400, 400);
JPanel content = new JPanel();
content.setBorder(new EmptyBorder(10, 10, 10, 10));
content.setLayout(new GridBagLayout());
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e){
System.err.println(e.getMessage());
}
appWindow.setLayout(new BoxLayout(appWindow.getContentPane(),BoxLayout.Y_AXIS));
title = new JLabel("<html><h1><strong>Human Benchmark App</strong></h1><br></html>");
game1 = new JButton("<html><h4>Reflex Action Test</h4></html>");
game2 = new JButton("<html><h4>Visual Memory Test</h4></html>");
credits = new JButton("<html><h4>Credits</h4></html>");
exit = new JButton("<html><h4>Exit</h4></html>");
game1.addActionListener(this);
game2.addActionListener(this);
credits.addActionListener(this);
exit.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
content.add(title, gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel Buttons = new JPanel(new GridBagLayout());
Buttons.add(game1, gbc);
Buttons.add(game2, gbc);
Buttons.add(credits, gbc);
Buttons.add(exit, gbc);
gbc.weighty = 1;
content.add(Buttons);
appWindow.add(content);
appWindow.setVisible(true);
appWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new HumanBenchmarkApp();
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton)e.getSource();
if (clicked == game1) {
}
else if(clicked == game2) {
}
else if(clicked == credits) {
}
else if(clicked == exit) {
System.exit(0);
}
}
}
评论
1赞
Extorc Productions
12/15/2021
完美!!!!
0赞
Afsal Baaqir A
12/16/2021
@Agzam我听说过 lambda 表达式,但我们的老师没有教我们这些,所以我用基本的简单实现来制作
0赞
Agzam
12/16/2021
@20CSE028 Afsal Baaqir A,我编辑了答案并添加了 2 个变体
0赞
Cheng Thao
12/17/2021
#2
问题是您的按钮 Exit 的名称不是“Exit”。这是您的代码:
Exit = new JButton("<html><h4>Exit</h4></html>");
相反,您可以比较 exit 和 clicked 是否引用了 Agzam 描述的相同按钮。
@Override
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton)e.getSource();
if (clicked == game1) {
}
else if(clicked == game2) {
}
else if(clicked == credits) {
}
else if(clicked == exit) {
System.exit(0);
}
}
评论
getText()