简体   繁体   English

GridBagLayout的麻烦

[英]GridBagLayout troubles


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). 我从Containers进行布局-所有容器都与我的示例相似(这意味着JPanel,布局,添加组件,返回JPanel)。

Structure of whole Layout: (this 3 are in main layout) 整体布局的结构:(这3个位于主布局中)
upper - BorderLayout.PAGE_START; 上部-BorderLayout.PAGE_START; //menu //菜单
center - BorderLayout.PAGE_CENTER; 中心-BorderLayout.PAGE_CENTER; //centerContainer() // centerContainer()
bottom - BorderLayout.PAGE_END; 底部-BorderLayout.PAGE_END; //statusBar - just container with text // statusBar-带有文本的容器


placement of this 3 containers works fine, but problem is with placement in the centerContainer. 这3个容器的放置效果很好,但问题在于centerContainer中的放置。
Center container structure: 3 containers - aboutServerContainer, aboutGameContainer, gameContainer. 中心容器结构:3个容器-aboutServerContainer,aboutGameContainer,gameContainer。

gameContainer has size 450x450 gameContainer尺寸为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.) 我想在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;
    }

It looks like you are trying to achieve a layout like this: 看起来您正在尝试实现如下布局:

+-----+-----+
|  A  |     |
+-----+  C  |
|  B  |     |
+-----+-----+

To achieve that, you grid constraints should be as: 为此,您的网格约束应为:

  |  x  y  width  height
--+---------------------
A |  0  0    1       1 
B |  0  1    1       1 
C |  1  0    1       2 

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. 我认为,最好始终使用“功能强大”的布局管理器,而不是处理特殊情况,嵌套面板以及将内容排列不灵活的问题。

Why not go like this: 为什么不这样:

Have a main BroderLayout 有一个主要的BroderLayout

  • add nothing to PAGE_START 不添加任何内容到PAGE_START
  • add center container to CENTER 将中心容器添加到CENTER
  • add VerticalPanel to LINE_START -in vertical panel add serverInfo -in vertical panel add gameInfo -add status bar to PAGE_END 将VerticalPanel添加到LINE_START-在垂直面板中添加serverInfo-在垂直面板中添加gameInfo-将状态栏添加到PAGE_END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM