具有网格布局的JLayeredPane? [英] JLayeredPane with gridlayout?
问题描述
我是否可以在JLayeredPane的右侧添加按钮?分层的窗格包含一个JPanel,它代表一个国际象棋棋盘,在该棋盘的顶部,我有JLabel代表国际象棋棋子.我基本上希望在面板右侧安装另一个面板,其中包含播放器信息和允许重新比赛等的按钮.添加此面板的最佳方法是什么?
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));
}
然后,我继续将jlabel添加到面板上的每个组件.我想像前面提到的那样在板的右侧添加另一个大面板.
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.
推荐答案
无法完全理解为什么为此使用JLayeredPane
的原因,但这仅是我自己.
Can't quite see why you would use a JLayeredPane
for this, but that's just me.
将主容器的Layout
设置为BorderLayout
.将boardX
添加到主容器的BorderLayout.CENTER
位置.
Set the Layout
for the main container to BorderLayout
. Add the boardX
to the BorderLayout.CENTER
position of the main container.
将玩家信息面板添加到主容器的BorderLayout.EAST
位置.
Add the player information panel to the BorderLayout.EAST
position of the main container.
设置boardX
的bounds
并不会真正起任何作用,因为父容器将要使用面板的首选/最小/最大尺寸(基于您可能使用的布局管理器)来确定最好的尺寸(根据您的代码)可能还是500x500 ...
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...
这篇关于具有网格布局的JLayeredPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!