Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

java - JLayeredPane with gridlayout?

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

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

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...