2014-09-11 67 views
1

我有創建棋盤的代碼。它最近在工作,但我不確定我改變了什麼。當我運行它時會出現一個空的窗口。JPanel棋盤不顯示

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

public class Chess extends JFrame implements ActionListener { 

    JButton[][] a = new JButton[8][8];  //This is the individual squares of the game board 
    JPanel board = new JPanel();    //The first instance of the game board 

    int lineCounter = 0;      //Records the current horizontal row 

    String[][] pieceList = new String[8][8]; //This list has the game pieces recorded in it 


    public Chess() { 

     //Create the board 
     setTitle("CHESS"); 
     setSize(600,600);      //Sets the window size to 600x600 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new GridLayout(8,8));  //Sets the game board to an 8x8 grid 

     for (int i = 0; i < 8; i++) {       /*This nested loop determines     the colour of the the board squares, and which colour pieces should go 
                   onton which tiles.*/ 
      for (int j = 0; j < 8; j++){ 

       if((i+j)%2 == 0 && lineCounter < 3) {    //This if statement sets the top part of the game board, and sets the red pieces 
        pieceList[i][j] = "RedPiece"; 
        a[i][j] = new JButton(); 
        a[i][j].setBackground(Color.black); 
        board.add(a[i][j]); 
       } else if ((i+j)%2 == 0 && lineCounter >= 5) { //This if statement sets the bottom of the board, and the blue pieces 
        pieceList[i][j] = "BluePiece"; 
        a[i][j] = new JButton(); 
        a[i][j].setBackground(Color.black); 
        board.add(a[i][j]); 

       } else if ((i+j)%2 == 0 && 3 <= lineCounter && lineCounter < 5) {  //This if statement sets the middle of the game board 
        pieceList[i][j] = null; 
        a[i][j] = new JButton(); 
        a[i][j].setBackground(Color.black); 
        board.add(a[i][j]); 
       } else { 
        pieceList[i][j] = null; 
        a[i][j] = new JButton(); 
        a[i][j].setBackground(Color.gray); 
        board.add(a[i][j]); 
       } 
      } 
      lineCounter++; 
     } 
    } 

A for循環後面添加新的JButtons並設置背景顏色。 setVisible(true)在一個單獨的類。我會很高興發佈更多的代碼,但我相信問題在這裏。我可能已經遺漏了一些東西。目前,我還沒有添加棋子游戲。 這是我使用的驅動程序類:

public class ChessGUI { 
     public static void main(String[] args){ 
      Chess run = new Chess(); 
      run.setVisible(true); 
     } 
    } 
+1

您發佈的代碼中沒有任何內容實際上將按鈕添加到「容器」中。 – resueman 2014-09-11 13:49:38

+0

如果我添加一個添加JButton的嵌套循環,並且使用'setVisible(true)',它可以正常工作。你能告訴我們應該添加按鈕並設置背景顏色的代碼嗎?您可以使用帖子下方的「修改」鏈接將其修改爲帖子。 – 2014-09-11 13:51:03

+1

另請參見[製作健壯,可調整大小的國際象棋GUI](http://stackoverflow.com/q/21142686/418556)。 – 2014-09-11 13:54:55

回答

0

您的代碼有兩個問題。首先,您要在Chess對象上設置佈局,而不是在要將Button添加到的面板上。其次,你永遠不會將JPanel添加到框架。

因爲它看起來像你並不真正需要的面板,解決它是去除JPanel board = new JPanel();最簡單的方法,並改變每個board.add(a[i][j]);add(a[i][j]);

這將增加Button小號直接在框架(所以他們實際上正在顯示),並將它們放入ContainerGridLayout,以便他們正確的位置。

0

仔細檢查我認爲你是不添加這些按鈕,您所創建的板面板,並添加面板設置幀邊界佈局面板不是爲國際象棋類本身.....希望它可以幫助

0

用for循環發佈其他類。 我猜你添加按鈕,像這樣(例如):

board.add(a[0][0]); 

如果是這樣,你正確添加的按鈕,但你需要JPanel中添加到JFrame,就像這樣:

add(board); 

(您只需添加它,因爲您的類擴展了JFrame,將其添加到此類中)。

編輯: 正如我的想法。您需要添加的JPanel(創建後)到JFrame,就像這樣:

JPanel board = new JPanel(); 
    add(board); 

在你的情況就在構造函數中的乞討加入這一行。