2015-07-20 88 views
0

我是新來的GUI,我想知道爲什麼這段代碼沒有顯示任何東西。請記住,代碼是不完整的,並承擔我的代碼有點長。GUI類不顯示任何東西在顯示器上

//imports 

public class SudokuGame extends Frame{ 

private SudokuBoard sb = new SudokuBoard(this); 

/** 
* a button to restart game 
*/ 
private Button b = new Button("Restart"); 

/** 
* 9 x 9 buttons to store the numbers used in the game 
*/ 
private Button[][] grid= new Button[9][9]; 

/** 
* TextField used to store the remaining empty blocks 
*/ 
private TextField t; 

/** 
* Constructor 
* @param ssm 
*/ 
public SudokuGame(SomeSudokuMatrix ssm) { 

    /** 
    * sets window's name 
    */ 

    super("Sudoku"); 

    /** 
    * sets background color 
    */ 

    setBackground(Color.WHITE); 





    /** 
    * adds SudokuBoard to the SudokuGame which extends frame 
    */ 

    add(sb, BorderLayout.CENTER); 

    /** 
    * initialize the remaining blocks to TextField 
    */ 

    t = new TextField("Remain:" + sb.getEmpty()); 

    /** 
    * new panel 
    */ 

    Panel p = new Panel(); 

    /** 
    * sets panel size to 100 x 10 
    */ 

    p.setSize(100,10); 

    /** 
    * sets restart button size to 10 x 5 and location to 48, 5 
    */ 

    b.setBounds(48,5,10,5); 

    /** 
    * adds restart button to the Panel 
    */ 

    p.add(b); 

    /** 
    * adds panel to south 
    */ 

    add(p, BorderLayout.SOUTH); 

    /** 
    * adds ActionListner to restart button 
    */ 

    b.addActionListener(sb); 


    /** 
    * adds TextField to the Panel 
    */ 

    p.add(t); 

    /** 
    * Sets the TextField size to 10 x 5 and location to 53, 5 
    */ 

    t.setBounds(53,5,10,5); 

    /** 
    * adds the buttons that hold the values 
    */ 

    for(int i = 0; i < 9; i++){ 
     for(int j = 0; j < 9; j++){ 

      grid[i][j]=new Button(null); 
      add(grid[i][j]); 

     } 
    } 

    /** 
    * Automatically resizing 
    */ 

    pack(); 

    /** 
    * Shows SudokuGame 
    */ 

    setVisible(true); 

    /** 
    * Shows the Panel 
    */ 

    p.setVisible(true);  
} 

/** 
* sets the label of the button 
* 
* @param s String to add to the button 
* @param i the position 
* @param j the position 
*/ 

public void setText(String s, int i, int j){ 

    /** 
    * if the button is not empty sets the label of the button 
    */ 

    if(!s.equals("-1")){ 
    grid[i][j].setLabel(s); 
    } 
} //to do: change TextField when filling empty block 

public void setText(String s){ 

    t.setText(s); 
} 
} 

這是GUI類(框架),沒有畫布或動作偵聽器或運行遊戲的任何其他類。

+0

會發生什麼事,當你運行該代碼?你是否遇到錯誤,是否編譯但沒有任何反應?你在學習某種教程嗎?更多信息將幫助您獲得更好的答案。 – jkeuhlen

+0

當我運行它時,它編譯並運行,但沒有任何反應。這是我的計算課的作業,所以我遵循作業規範。 – itsnotme

回答

0

你已經寫

private Button[][] grid= new Button[9][9]; 

應該

private Button[][] grid= new Button[9][]; 

接下來你已經直接開始從柵陣列添加按鈕:

for(int i = 0; i < 9; i++){ 
     for(int j = 0; j < 9; j++){ 
      add(grid[i][j]); 
     } 
    } 

這是錯誤的,正因爲如此它拋出NullPointerException。請參閱IDE控制檯。 解決方法是先實例柵格陣列如下:

for(int i = 0; i < 9; i++){ 
    grid[i]=new Button[9]; 
    } 

for(int i = 0; i < 9; i++){ 
    for(int j = 0; j < 9; j++){ 
     grid[i][j]=new Button("SomeLabel"); 
    } 
} 

初始化柵陣列後,添加按鈕的幀如下:

for(int i = 0; i < 9; i++){ 
    for(int j = 0; j < 9; j++){ 
     add(grid[i][j]); 
    } 
} 
+0

是的我的壞我忘了初始化他們,但我添加按鈕之前有問題,並在我啓動它們後,我有同樣的問題,但謝謝你的收穫。還有一件事,爲什麼我應該使用[9] []而不是[9] [9]來啓動數組,而不應該是9 x 9數組按鈕? – itsnotme

+0

對不起我的錯誤。兩種方式都是正確的沒關係。但是數組實例化應該如上所述 – VVJ

+0

您是在說一些問題?哪一個例外? – VVJ