我在使用我的swing GUI文件時遇到了一些問題,我一直在收到一個錯誤,我使用oracle的教程作爲參考,但是,不管我一直收到相同的錯誤。有人能告訴我我的錯誤在Java中的擺動?
這裏的錯誤:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Calculator.CalcGUI.CalGUI(CalcGUI.java:29)
at Calculator.CalcGUI.access$0(CalcGUI.java:27)
at Calculator.CalcGUI$1.run(CalcGUI.java:74)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(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.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)
,這裏是我的GUI代碼記住,這是一個有點亂,因爲我試圖盡我所能來解決它。
package Calculator;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class CalcGUI implements ActionListener {
public static boolean geometry = false;
public static boolean is2D = false;
public static boolean is3D = false;
static boolean working = false;
static JFrame cfrm;
static JButton cb1;
static JButton cb2;
static JButton cb3;
static JLabel cl1;
static JLabel cl2;
static JTextField ctf1;
static JTextField ctf2;
static JTextField ctf3;
static ActionListener a, b, c, d, e, f;
private static void CalGUI() {
cfrm.setLayout(new FlowLayout());
cfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cfrm.setSize(300, 600);
cb1 = new JButton();
cb2 = new JButton();
cb3 = new JButton();
do {
if(geometry && is2D != true) {
cfrm.removeAll();
cfrm.add(cb1);
cb1.setText("geometry");
cb1.addActionListener(a);
}
else if(geometry = true && is2D != true) {
cfrm.removeAll();
cfrm.add(cb1);
cfrm.add(cb2);
cb1.setText("2D Shape");
cb2.setText("3D Shape");
cb1.addActionListener(b);
cb2.addActionListener(c);
}
}
while(working = false);
}
@Override
public void actionPerformed(ActionEvent arg0) {
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
CalGUI();
}
});
}
}
哇,我不能相信我忘了最簡單的一步,我是教...謝謝你親切的先生。 – 2012-08-01 21:35:37
不客氣。 – kosa 2012-08-01 21:36:59
現在我有一個新的問題,當我運行該程序的swing應用程序不會出現? – 2012-08-01 21:45:36