2013-03-04 61 views
1

我是新來的Java和Java Swing,我無法讓它工作。我有2個文本框,textfield1中的文本需要通過單擊按鈕傳遞給textfield2,但它不斷崩潰。Java Swing Textfield錯誤

下面是代碼:

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) 
+1

你打算讓我們猜猜錯誤是什麼嗎? – 2013-03-04 19:10:19

+0

請再次查看代碼,我已更新它。 – 2013-03-04 19:13:56

回答

3

您目前陰影變量button1field1field2Paneel,即要創建這些組件的當地實例在Paneel的構造函數,但這些名稱類的成員變量保持null,導致NPE

通過消除JButtonJTextField類型的關鍵字,您將組件分配到預期的類成員變量:

public Paneel(){ 
    button1 = new JButton("Click"); 
    ... 
    field1 = new JTextField(10); 
    ... 
    field2 = new JTextField(10); 
    ... 
} 
+0

我不知道如何解決這個問題,你能舉個例子嗎? – 2013-03-04 19:14:59

+0

只要刪除上面顯示的'JButton'&'JTextField'類型關鍵字:) – Reimeus 2013-03-04 19:16:41

+0

你解決了它!非常感謝你。 – 2013-03-04 19:17:29

2

您申報新的本地「按鈕1」,「字段1」和你的構造函數,而不是初始化實例變量裏面的「域2」的變量。

2

使用此代碼Paneel構造確實如此。

 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); 
     } 

你已經在你的構造函數創建field1field2是本地的構造函數。 Paneel類的成員字段(field1field2)仍爲空。所以在actionPerformed這是投擲NullPointerException