gpt4 book ai didi

java - Swing 应用程序在 ActionEvent 上抛出 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:31:57 25 4
gpt4 key购买 nike

我正在制作一个 Java Swing 应用程序,其中包含 1 个 TextArea、1 个 Label 和 1 个 TextField。

我的代码如下:

package mainpack.newboston;

import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Panel extends JPanel implements ActionListener {
protected JTextArea textarea123;
protected JTextField textfield1;
JLabel label;
JFrame frame;

public Panel() {
setLayout(null);

JTextArea textarea123 = new JTextArea();
JTextField textfield1 = new JTextField(30);
JLabel label = new JLabel("Command: ");

add(textarea123);
add(textfield1);
add(label);

// textarea123.append("Hello");
textarea123.setEditable(false);
textarea123.setSize(600, 600);
textarea123.setBackground(Color.BLACK);
textarea123.setForeground(Color.WHITE);

textfield1.setBorder(null);
textfield1.setBackground(Color.BLACK);
textfield1.setForeground(Color.white);

textfield1.addActionListener(this);
textfield1.setActionCommand("commandexecuted");

// label.setForeground(Color.WHITE);
label.setBackground(Color.ORANGE);

//#######################################################
Insets insets = getInsets();

Dimension fieldsize = textfield1.getPreferredSize();
textfield1.setBounds(63 + insets.left, 555 + insets.top, 537, fieldsize.height);

textfield1.setFocusable(true);
textfield1.setEnabled(true);
Dimension labelsize = label.getPreferredSize();
label.setBounds(0 + insets.left, 555 + insets.top, labelsize.width, labelsize.height);

@SuppressWarnings("unused")
Dimension size = textarea123.getPreferredSize();
textarea123.setBounds(0 + insets.left, 0 + insets.top, 600, 555);
//##########################################################
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("New Boston");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set up the content pane.
frame.add(new Panel());
//Size and display the window.
Insets insets = frame.getInsets();
frame.setResizable(false);
frame.setSize(600 + insets.left + insets.right, 600 + insets.top + insets.bottom);
frame.setVisible(true);
}

public void ConsoleLog(String message){
System.out.println(message);
}

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

public void textappend(String message) {
textarea123.append(message);
}

public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("commandexecuted")) {
if (textfield1.getText().equals("Try")) {
ConsoleLog("Hello");
}
}
}
}

当 TextField 处于焦点状态时按下 Enter 键时出现错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at mainpack.newboston.Panel.actionPerformed(Panel.java:127)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$400(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)

我试图让它看起来像一个控制台并使用一些命令,但我稍后会这样做,因为我无法使用 ActionEvents...

最佳答案

在您的构造函数中,更改此行

JTextField textfield1 = new JTextField(30);

this.textfield1 = new JTextField(30);

关于java - Swing 应用程序在 ActionEvent 上抛出 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27572597/

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