GridBagLayout 的麻烦
我正在尝试制作简单的游戏布局。 我从容器制作布局 - 所有容器都与我的示例类似(这意味着 JPanel、setlayout、添加组件、返回 JPanel)。
整个布局的结构:(这 3 个位于主布局中)
上部 - BorderLayout.PAGE_START; //菜单
中心 - BorderLayout.PAGE_CENTER; //centerContainer()
底部 - BorderLayout.PAGE_END; //statusBar - 只是带有文本的容器
这 3 个容器的放置工作正常,但问题在于 centerContainer 中的放置。
中心容器结构:3个容器 - aboutServerContainer、aboutGameContainer、gameContainer。
gameContainer 的大小为 450x450
我想以与 serverInfo 下的 gameContainer 和 gameInfo 相同的高度启动serveInfo, 但它以某种方式集中了 serverInfo 并且 gameInfo 位于它之下,但它也在 gameContainer 下腾出了可用空间(我在这里不需要任何可用空间。)
private Container centerContainer() {
JPanel centerJPanelJP = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
stredniJPanelJP.setLayout(new GridBagLayout());
//gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
centerJPanelJP.add(aboutServerContainer(),gbc);
gbc.gridheight = 1;
gbc.gridy = 1;
centerJPanelJP.add(aboutGameContainer(),gbc);
gbc.gridheight = 2;
gbc.gridx = 0;
gbc.gridy = 0;
centerJPanelJP.add(gamePanelContainer(),gbc);
return centerJPanelJP;
}
I'm trying to make simple game layout.
I make my layout from Containers - all containers are similar to my example(it mean JPanel, setlayout, add components, return JPanel).
Structure of whole Layout: (this 3 are in main layout)
upper - BorderLayout.PAGE_START; //menu
center - BorderLayout.PAGE_CENTER; //centerContainer()
bottom - BorderLayout.PAGE_END; //statusBar - just container with text
placement of this 3 containers works fine, but problem is with placement in the centerContainer.
Center container structure: 3 containers - aboutServerContainer, aboutGameContainer, gameContainer.
gameContainer has size 450x450
I want to start serveInfo in same height as gameContainer and gameInfo under serverInfo,
but it somehow cernter the serverInfo and the gameInfo is uder it, but it also make free space under gameContainer (I don't want any free space here.)
private Container centerContainer() {
JPanel centerJPanelJP = new JPanel();
GridBagConstraints gbc = new GridBagConstraints();
stredniJPanelJP.setLayout(new GridBagLayout());
//gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
centerJPanelJP.add(aboutServerContainer(),gbc);
gbc.gridheight = 1;
gbc.gridy = 1;
centerJPanelJP.add(aboutGameContainer(),gbc);
gbc.gridheight = 2;
gbc.gridx = 0;
gbc.gridy = 0;
centerJPanelJP.add(gamePanelContainer(),gbc);
return centerJPanelJP;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试实现这样的布局:
为了实现这一点,您的网格约束应该是:
在我看来,最好始终使用“强大的”布局管理器,而不是处理特殊情况,嵌套面板以及排列事物的不灵活性。
It looks like you are trying to achieve a layout like this:
To achieve that, you grid constraints should be as:
In my opinion, it is better to consistently use a "powerful" layout manager, than to deal with special cases, nested panels and inflexibility of lining things up.
为什么不这样做:
让主 BroderLayout
-在垂直面板中添加serverInfo
-在垂直面板中添加游戏信息
-向PAGE_END添加状态栏
Why not go like this:
Have a main BroderLayout
-in vertical panel add serverInfo
-in vertical panel add gameInfo
-add status bar to PAGE_END