Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there a way for me to add buttons to the right side of my JLayeredPane? The layered pane contains a JPanel which represents a chess board and on top of this board I have JLabels representing chess pieces. I want basically another panel attached to the right side of the board which contains player information and buttons allowing for rematches/etc. What would be the best way to go about adding this panel?

Here is a snippet of my code. The part which sets up the board panel inside the layeredpane:

private void setupBoard() {
    paneX = new JLayeredPane();
    getContentPane().add(paneX);
    paneX.setPreferredSize(new Dimension(500,500));

    boardX = new JPanel(); 
    paneX.add(boardX, JLayeredPane.DEFAULT_LAYER);
    boardX.setLayout(new GridLayout(8,8));
    boardX.setBounds(0,0,500,500);
    chessBoard.setPreferredSize(new Dimension(500,500));
}

Then, I go on to add the jlabels to each component on the panel. I want to add another big panel attached to the right side of the board like I mentioned earlier.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
360 views
Welcome To Ask or Share your Answers For Others

1 Answer

Can't quite see why you would use a JLayeredPane for this, but that's just me.

Set the Layout for the main container to BorderLayout. Add the boardX to the BorderLayout.CENTER position of the main container.

Add the player information panel to the BorderLayout.EAST position of the main container.

Setting the bounds of the boardX is not really going to have any effect, as the parent container will want to use the panels preferred/minimum/maximum size (based on whatever layout manager you might use) to determine the best size to make it, which based on your code, would probably be 500x500 anyway...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...