2015-05-05 104 views
0

我正在學習使用java.swing庫。我正在嘗試創建一個非常簡單的計算器佈局。我已經添加了addNumbers方法。我試圖在計算器中顯示按鈕,並且我用於loops.buttons不出現我得到nullpointerexception。按鈕不出現,空指針異常

import java.awt.*; 
import javax.swing.*; 

public class Calculator extends JFrame{ 

    /** 
    * @param args 
    */ 
    //dEFINE WIDTH AND HEIGHT 
    private static final int WIDTH = 400; 
    private static final int HEIGHT = 600; 

    //Values for buttons having numbers 
    private JButton[] numButton; 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Calculator myCalculator = new Calculator(); 

    } 

    public Calculator(){ 
     setTitle("Simple Calculator"); 
     setSize(WIDTH,HEIGHT); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     Container Pane = getContentPane(); 
     Pane.setLayout(new GridLayout(3,3)); 

     //Add numbers to screen now 
     addNumbers(Pane); 



    } 

    //Function to add numbers on screen 
    public void addNumbers(Container P){ 
     for(int i = 1; i <= 9; i++){ 
      numButton[i] = new JButton(String.valueOf(i)); 
      P.add(numButton[i]); 
     } 
    } 

} 
+0

你可以添加錯誤消息和堆棧跟蹤 –

+0

你的錯誤在哪行代碼? – OPK

+0

可能的重複:[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – Pshemo

回答

5

需要初始化您的數組:

private JButton[] numButton = new JButton[10]; 

的10此處允許數組中10個空格。

+0

我將private JButton [] numButton更改爲private JButton [] numButton = new JButton [10],仍然是隨機按鈕,有時是3,有時是4 –

+0

隨機按鈕是什麼意思?你應該更新你的問題。 –

+0

在您做出修改之後,我開始有時會得到5個按鈕,有時候會有4個按鈕。我一直需要9個按鈕。這個問題只針對你的答案 –

0

上述解決方案的工作,並刪除空指針,因爲現在內存分配給numButton陣列.... 這裏是工作的代碼剪斷,又結合了(BODMAS)的跡象... 唯一的區別是,我有將所有關鍵標籤放入ArrayList ,然後for循環現在可以在此ArrayList上爲 計算器獲取標籤... ArrayList可能不是一個好集合..可能是枚舉可能會更好,但是 這是爲了展示如何它可以變得更加動態,並添加按鈕標籤 或新按鈕會更容易... 另請注意,您可以根據關鍵標籤的大小使GridSize動態化爲 array..gridSize = this.buttons.size()/ 4 ....

0
import java.awt.Container; 
import java.awt.GridLayout; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.JButton; 
import javax.swing.JFrame; 

public class Calculator extends JFrame{ 

    private static final int WIDTH = 400; 
    private static final int HEIGHT = 600; 


    List<String> buttons = new ArrayList<String>(); 




    JButton[] numButton = null; 


    public static void main(String[] args) { 
     Calculator myCalculator = new Calculator(); 
    } 

    public Calculator(){ 
     setTitle("Simple Calculator"); 
     setSize(WIDTH,HEIGHT); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     Container Pane = getContentPane(); 


     this.buttons.add("1"); 
     this.buttons.add("2"); 
     this.buttons.add("3"); 
     this.buttons.add("4"); 
     this.buttons.add("5"); 
     this.buttons.add("6"); 
     this.buttons.add("7"); 
     this.buttons.add("8"); 
     this.buttons.add("9"); 
     this.buttons.add("0"); 
     this.buttons.add("+"); 
     this.buttons.add("="); 
     this.buttons.add("%"); 
     this.buttons.add("#"); 
     this.buttons.add("*"); 
     this.buttons.add("/"); 

     int gridSize = this.buttons.size()/4; 

     Pane.setLayout(new GridLayout(gridSize,gridSize)); 


     this.numButton = new JButton[this.buttons.size()]; 

     //Add numbers to screen now 
     addNumbers(Pane); 
     } 

    //Function to add numbers on screen 
    public void addNumbers(Container P){ 
     for(int i = 0; i < this.buttons.size(); i++){ 
      numButton[i] = new JButton(this.buttons.get(i).toString()); 
      P.add(numButton[i]); 
     } 
    } 

}