2011-07-29 62 views
0

请帮我弄清楚这里出了什么问题?刚开始用java。组件未初始化投掷NUllPointerException

public class PropertyChangeListenerTest { 
private JPanel mainPanel = null; 
private JTextField source = null; 
private JTextField target = null; 

PropertyChangeListenerTest() { 

    mainPanel = new JPanel(); 
    mainPanel.setLayout(new GridLayout(1, 0, 10, 0)); 
    source = new JTextField(20); 
    target = new JTextField(20); 
    mainPanel.add(source); 
    mainPanel.add(target); 

} 

JPanel getMainPanel() { 
    return mainPanel; 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

protected static void createAndShowGUI() { 
    JFrame f = new JFrame("PropertyChangeListener"); 
    f.getContentPane().add(new PropertyChangeListenerTest().getMainPanel()); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.pack(); 
    f.setVisible(true); 
} 

}

+0

有没有'NullPointerException'当我运行此代码,您可以加入堆栈跟踪和更多的解释什么是坏到您的文章。 。? – Sorceror

+0

将来添加更多标签。 –

+0

它适合我!我从Eclipse和命令行启动它:

回答

0

在这段代码没有问题。任何方式来运行此代码。意味着使用cmd。

CMD命令combile:

的javac PropertyChangeListenerTest.java

的Java PropertyChangeListenerTest