简体   繁体   English

从jpanel绘制和删除jlabel

[英]Drawing and deleting jlabels from a jpanel

I am making a chess game gui by setting up a grid of jpanels with jlabels on top (the piece images). 我通过设置顶部带有jlabel的jpanels网格(棋子图像)来制作国际象棋游戏gui。 I set up the board with each square (jpanel) having an empty jlabel w/ visibility set to false. 我将每个正方形(jpanel)的空jlabel的可见性设置为false来设置板。 I then go through each of the jpanels and set the jlabel picture to an image corresponding to one of my pieces. 然后,我遍历每个jpanel,并将jlabel图片设置为与我的一块相对应的图像。 I know whether or not to draw a piece because I have a variable _board[8][8] that tells me if that spot is occupied and by whom. 我知道是否要绘画,因为我有一个变量_board [8] [8]告诉我那个地方是否被占领,以及被谁占据。 This is how I create the original board: 这是我创建原始板的方式:

EDIT: 编辑:

I actually changed this up quite a bit, that way I don't have to reload images each time: 实际上,我对此进行了相当大的更改,因此不必每次都重新加载图像:

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());

    }

This loads the initial board just fine, however I can't seem to get a specific component. 这样可以很好地加载初始板,但是我似乎无法获得特定的组件。 When I click on one of the pieces, that square should light up. 当我单击其中一件时,该正方形应会亮起。 The only square that ever gets the green outline is the one in the top left (0, 0). 唯一获得绿色轮廓的正方形是左上角的那个正方形(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;
        }

I guess I am having problems with this line: JPanel curSquare = (JPanel)theBoard.getComponentAt(gameX, gameY); 我猜我在这行上有问题:JPanel curSquare =(JPanel)theBoard.getComponentAt(gameX,gameY);

however, the gameX and gameY values are being set properly. 但是,正确设置了gameX和gameY值。

Thanks 谢谢

I think you are missing a line to add the label to each panel. 我认为您缺少将标签添加到每个面板的一行。 During "// setup initial squares": 在“ //设置初始平方”期间:

curLabel.setVisible(false);

Should be: 应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM