簡體   English   中英

Swing復制對ActionEvent引發java.lang.NullPointerException

[英]Swing apllication throws java.lang.NullPointerException on ActionEvent

我正在制作一個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);

您正在定義字段,例如protected JTextField textfield1;

然后,您要定義局部變量,例如使用JTextField textfield1 = new JTextField(30);

因此,例如, textfield1是您的對象(在本地上下文中),而this.textfield1為null,因為您從未給它賦值。

我建議像這樣為您的字段分配一個變量:

textfield1 = new JTextField(30);

在您的字段中嘗試使用此技術,既可以避免分配相同名稱的局部變量,又可以避免在分配值時編寫隱式this

我還建議您看一下http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM