我是新來的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類(框架),沒有畫布或動作偵聽器或運行遊戲的任何其他類。
會發生什麼事,當你運行該代碼?你是否遇到錯誤,是否編譯但沒有任何反應?你在學習某種教程嗎?更多信息將幫助您獲得更好的答案。 – jkeuhlen
當我運行它時,它編譯並運行,但沒有任何反應。這是我的計算課的作業,所以我遵循作業規範。 – itsnotme