gpt4 book ai didi

java - JButton 和 ActionListener 在 java.awt、安全性和事件中产生多个异常?

转载 作者:行者123 更新时间:2023-12-02 06:51:15 25 4
gpt4 key购买 nike

我在东、西、北、南有四个按钮,我试图让按下的按钮更改其文本。在 Eclipse 中,编译前似乎没有问题,但是当我编译并尝试按四个按钮中的任何一个时,它会显示许多异常。

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class gui {
JButton rightbutton;
JButton leftbutton;
JButton downbutton;
JButton upbutton;

public static void main(String[] args) {
gui GUI = new gui();
GUI.go();
}

public void go() {
JFrame myFrame = new JFrame("The Gui");
JPanel myPanel = new JPanel();

JButton rightbutton = new JButton("Right Button");
rightbutton.addActionListener(new rightListener());
JButton leftbutton = new JButton ("Button of the west");
leftbutton.addActionListener(new leftListener());
JButton downbutton = new JButton ("Button on the bottom");
downbutton.addActionListener(new downListener());
JButton upbutton = new JButton ("Button in the sky");
upbutton.addActionListener(new upListener());

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(500,500);
myFrame.setVisible(true);

myFrame.getContentPane().add(myPanel);
myFrame.getContentPane().add(BorderLayout.EAST,rightbutton);
myFrame.getContentPane().add(BorderLayout.NORTH,upbutton);
myFrame.getContentPane().add(BorderLayout.SOUTH,downbutton);
myFrame.getContentPane().add(BorderLayout.WEST,leftbutton);
}
class rightListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
rightbutton.setText("Pushed");
}
}
class leftListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
leftbutton.setText("Pushed");
}
}
class downListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
downbutton.setText("Pushed");
}
}
class upListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
upbutton.setText("Pushed");
}
}
}

以下是当我按下四个按钮中的任何一个时给出的所有异常(exception)情况。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui$leftListener.actionPerformed(gui.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui$upListener.actionPerformed(gui.java:56)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

您的问题是您声明了JButtons,但没有将它们放入为它们保留的实例变量中。您可以通过替换以下行来解决此问题:

JButton rightbutton = new JButton("Right Button");

与:

rightbutton = new JButton("Right Button");

第一种形式声明的局部变量掩盖了实例变量,因此实例变量消失了。当监听器尝试访问实例变量时,会发生空指针异常,该实例变量实际上为空,因为它从未被初始化。

解决此问题的另一种方法是使用 event.getSource() 而不是使用实例变量,在这种情况下实例变量就变得多余。

关于java - JButton 和 ActionListener 在 java.awt、安全性和事件中产生多个异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18005759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com