1

我是 Java 和 Java Swing 的新手,我无法让它工作。我有 2 个文本字段,textfield1 中的文本需要通过单击按钮传递到 te textfield2,但它不断崩溃。

这是代码:

package javaapplication7;

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


public class JavaApplication7 {

    public static void main(String[] args) {
        JFrame theFrame = new JFrame();
        theFrame.setTitle("Title");
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.setSize(400, 400);
        theFrame.setLocation(400, 400);

        theFrame.setContentPane(new Paneel());
        theFrame.setVisible(true);
    }
}

class Paneel extends JPanel{
            JButton button1;
            JTextField field1;
            JTextField field2;


    public Paneel(){
            JButton button1 = new JButton("Click");
            button1.addActionListener(new KH());
            add(button1);

            JTextField field1 = new JTextField(10);
            add(field1);

            JTextField field2 = new JTextField(10);
            add(field2);
    }


    class KH implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            field2.setText(field1.getText());
        }
    }
}

当我运行它时,它给出了一个巨大的错误列表,有人知道吗?

错误列表:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javaapplication7.Paneel$KH.actionPerformed(JavaApplication7.java:54)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    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:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
4

3 回答 3

3

您当前正在隐藏变量, button1, field1in field2,即Paneel您正在.PaneelnullNPE

通过删除JButtonand JTextFieldtype 关键字,您可以将组件分配给预期的类成员变量:

public Paneel(){
   button1 = new JButton("Click");
   ...
   field1 = new JTextField(10);
   ...
   field2 = new JTextField(10);
   ...
}
于 2013-03-04T19:11:36.917 回答
2

您在构造函数中声明新的本地“button1”、“field1”和“field2”变量,而不是初始化实例变量。

于 2013-03-04T19:11:16.587 回答
2

确实将此代码用于 Panel 构造函数。

        public Paneel(){
        JButton button1 = new JButton("Click");
        button1.addActionListener(new KH());
        add(button1);

        field1 = new JTextField(10);
        add(field1);

        field2 = new JTextField(10);
        add(field2);
       }

field1并且field2您在构造函数中创建的对于构造函数来说是本地的。类的成员字段 (field1field2)Paneel仍然为空。所以在actionPerformed里面扔NullPointerException

于 2013-03-04T19:18:15.100 回答