从jpanel中绘制和删除jlabels

时间:2013-02-07 09:46:45

标签: java swing jpanel jlabel

我正在制作一个国际象棋游戏gui,通过在顶部设置一个带有jlabels的jpanel网格(片段图像)。我设置了每个正方形(jpanel)的板,其中空的jlabel w / visibility设置为false。然后我浏览每个jpanel并将jlabel图片设置为与我的一个部分相对应的图像。我知道是否要绘制一块因为我有一个变量_board [8] [8]告诉我该点是否被占用以及由谁占用。这就是我创建原始板的方式:

编辑:

我实际上改变了这一点,这样我每次都不需要重新加载图像:

public ChessGameGUI(Square[][] board) throws IOException 
    {
        Dimension size = new Dimension(500, 400);
        Dimension boardSize = new Dimension(400, 400);

        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(size);

        theBoard = new JPanel();
        layeredPane.add(theBoard, JLayeredPane.DEFAULT_LAYER);
        layeredPane.addMouseListener(this);

        GridLayout chessBoardLayout = new GridLayout(8,8);
        theBoard.setLayout(chessBoardLayout);
        theBoard.setPreferredSize(boardSize);
        theBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        boolean drawWhite = true;

        // setup initial squares
        for(int y = 0; y < 8; y++)
        {
            for(int x = 0; x < 8; x++)
            {
                BorderLayout squareBorder = new BorderLayout();
                JPanel aChessSquare = new JPanel(squareBorder);

                if(drawWhite) aChessSquare.setBackground(Color.WHITE);
                else aChessSquare.setBackground(Color.GRAY);

                // load initial images
                if(board[x][y]._isOccupied)
                {
                    BufferedImage logo = null;

                    try{
                        // load the image
                        String path = findPieceImagePath(board, x, y);
                        logo = ImageIO.read(new File(path));
                        ImageIcon pieceImage = new ImageIcon(logo);

                        JLabel pieceJLabel = new JLabel();
                        pieceJLabel.setIcon(pieceImage); // new
                        pieceJLabel.setVisible(true); // new

                        aChessSquare.add(pieceJLabel);
                    } 
                    catch (IOException e)
                    { 
                        e.printStackTrace(); 
                    } // end try catch
                }// end if
                else
                {
                    JLabel pieceJLabel = new JLabel();
                    pieceJLabel.setVisible(false);
                    aChessSquare.add(pieceJLabel);
                }

                theBoard.add(aChessSquare);

                // alternate background colors
                if(x == 7);
                else drawWhite = !drawWhite;
            }
        }

        System.out.println("the number of components = " + theBoard.getComponentCount());

    }

这样可以很好地加载初始板,但是我似乎无法获得特定组件。当我点击其中一个棋子时,那个方格应该亮起来。唯一得到绿色轮廓的正方形是左上角的那个(0,0)。

public void mousePressed(MouseEvent e) 
    {
        System.out.println("mouse press x = " + e.getX() + " y = " + e.getY());

        // find which piece on the board is being clicked
        int gameX, gameY;
        gameX = e.getX() / 50;
        gameY = e.getY() / 50;

        System.out.println("gameX = " + gameX + " gameY = " + gameY);

        // out of bounds
        if(gameX < 0 || gameY < 0) return;

        // true if selected a piece on this team
        if(myGame._board[gameX][gameY]._isOccupied && !_pieceSelected && myGame._board[gameX][gameY]._team == myGame._turn) 
        {
            System.out.println("selected new piece");
            JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY);
            curSquare.setBorder(BorderFactory.createLineBorder(Color.green));
            _pieceSelected = !_pieceSelected;



            return;
        }

我想这行有问题:JPanel curSquare =(JPanel)theBoard.getComponentAt(gameX,gameY);

但是,正确设置了gameX和gameY值。

由于

1 个答案:

答案 0 :(得分:1)

我认为你缺少一条线来为每个面板添加标签。在“// setup initial squares”中:

curLabel.setVisible(false);

应该是:

curLabel.setVisible(false);
aChessSquare.add(curLabel);