JLayeredPane with gridlayout?

我是研究僧i 提交于 2019-12-13 09:50:08

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/18838076/jlayeredpane-with-gridlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!