代码之家  ›  专栏  ›  技术社区  ›  Geoff Murphy

J面板棋盘不显示

  •  1
  • Geoff Murphy  · 技术社区  · 10 年前

    我有创建棋盘的代码。它最近在工作,但我不确定我改变了什么。当我运行它时,会出现一个空窗口。

    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++;
            }
        }
    

    下面是一个for循环,它添加了新的JButton并设置了背景色。这个 setVisible(true) 是在一个单独的班级。我很乐意发布更多代码,但我确信问题出在某处。我可能遗漏了什么。目前,我还没有添加棋子游戏棋子。 这是我使用的驱动程序类:

       public class ChessGUI {
          public static void main(String[] args){
              Chess run = new Chess();
              run.setVisible(true);
          }
       }
    
    3 回复  |  直到 10 年前
        1
  •  0
  •   resueman    10 年前

    代码有两个问题。首先,您要在 Chess 对象,而不是在您要添加的面板上 Button 是的。第二,你从不加上 JPanel 到框架。

    因为看起来你并不真的需要面板,所以修复它的最简单方法是移除 JPanel board = new JPanel(); ,并更改间隔 board.add(a[i][j]); add(a[i][j]);

    这将添加 按钮 s直接指向框架(因此它们实际上正在显示),并将它们放入 Container 用一个 GridLayout ,以便正确定位。

        2
  •  0
  •   veeraprathapm    10 年前

    仔细检查一下,我认为你没有将这些按钮添加到你创建的板面板中,并将该面板添加到框架设置边框布局到面板中,而不是用于象棋类本身。。。。。希望有帮助

        3
  •  0
  •   Nirgn camilo_u    10 年前

    用for循环发布另一个类。 我猜你添加了这样的按钮(例如):

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

    如果是这样,您可以正确添加按钮,但需要将JPanel添加到JFrame,如下所示:

       add(board);
    

    (您只需添加它,因为您的类扩展了JFrame,所以将其添加到此类中)。

    编辑: 正如我所想。您需要将JPanel(创建后)添加到JFrame,如下所示:

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

    在您的案例中,在构造函数的开头添加这一行。