我正在創建一個簡單的 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 甚至沒有關閉。
uj5u.com熱心網友回復:
ActionListener如果您可以使用 lambda 運算式,我真的不明白為什么 , 的實作如此令人困惑:
...
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);
}
}
}
uj5u.com熱心網友回復:
問題是您的按鈕退出的名稱不是“退出”。這是你的代碼:
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385140.html
