2014-04-17 37 views
0

這裏是我的源代碼:
主類:
Eclipse不啓動我的程序,但沒有錯誤

package daPackage; 

import javax.swing.JFrame; 

class apples{ 
    public static void main(String args[]){ 
     Gui go = new Gui(); 
     setUpGUI(go); 
    } 

    private static void setUpGUI(Gui g){ 
     g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     g.setVisible(true); 
     g.setSize(375, 175); 
    } 
} 

這裏是我的第二類:

package daPackage; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.EventListener; 
import javax.swing.*; 
import javax.swing.event.*; 

public class Gui extends JFrame{ 
    private JButton button; 
    private JTextField field1; 
    private JTextField field2; 
    private JTextField field3; 
    private int num1; 
    private int num2; 
    private int num3; 

public Gui(){ 
    super("Testing!"); 
    setLayout(new FlowLayout()); 

    field1 = new JTextField("" + num1); 
    num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 
    add(field1); 

    field2 = new JTextField("" + num2); 
    num2 = Integer.parseInt(field1.getText()) + Integer.parseInt(field3.getText()); 
    add(field2); 

    field3 = new JTextField("" + num3); 
    num3 = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText()); 
    add(field3); 

    button = new JButton(); 
    button.addActionListener(
     new ActionListener(){ 
      public void actionPerformed(ActionEvent event){ 
       field1.setText("" + num1); 
       field2.setText("" + num2); 
       field3.setText("" + num3); 
      } 
     } 
    ); 
    add(button);  
} 


} 

Eclipse中說:沒有錯誤,但是當我點擊運行按鈕沒有任何反應。我檢查了我的代碼幾次,但我找不到問題。

+0

刷新您的項目一次。 –

回答

0

聲明公共主類

你得到空指針異常,因爲您使用字段1和Field2沒有初始化它。

首先初始化然後使用它。

做這樣在你

class Gui extends JFrame{ 
    private JButton button; 
    private JTextField field1; 
    private JTextField field2; 
    private JTextField field3; 
    private int num1=0; 
    private int num2=0; 
    private int num3=0; 

public Gui(){ 
    super("Testing!"); 
    setLayout(new FlowLayout()); 

    field1 = new JTextField("" + num1); 
    field2 = new JTextField("" + num2); 
    field3 = new JTextField("" + num3); 
    num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 
    add(field1); 


    num2 = Integer.parseInt(field1.getText()) + Integer.parseInt(field3.getText()); 
    add(field2); 


    num3 = Integer.parseInt(field1.getText()) + Integer.parseInt(field2.getText()); 
    add(field3); 

    button = new JButton(); 
    button.addActionListener(
     new ActionListener(){ 
      public void actionPerformed(ActionEvent event){ 
       field1.setText("" + num1); 
       field2.setText("" + num2); 
       field3.setText("" + num3); 
      } 
     } 
    ); 
    add(button);  
} 
} 

而且你的蘋果類

public class apples{ 
     public static void main(String args[]){ 
      Gui go = new Gui(); 
      setUpGUI(go); 
     } 

     private static void setUpGUI(Gui g){ 
      g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      g.setVisible(true); 
      g.setSize(375, 175); 
     } 
    } 
+0

謝謝大家! 現在工作正常! - 好吧,它看起來很浪費,我需要一些佈局。 – Emiiiiiil

1

你所得到的錯誤:

Exception in thread "main" java.lang.NullPointerException 
    at Gui.<init>(Gui.java:21) 
    at apples.main(apples.java:5) 

也就是說,在該線:

num1 = Integer.parseInt(field2.getText()) + Integer.parseInt(field3.getText()); 

field2仍然爲空(也是field3)。

0

我編譯你的代碼,而且我得到了這些錯誤:

Exception in thread "main" java.lang.NullPointerException 
at daPackage.Gui.<init>(Gui.java:24) 
at daPackage.Main.main(Main.java:7) 
Java Result: 1 
相關問題