简体   繁体   English

Swing GridBagLayout锚点位于窗口外部

[英]Swing GridBagLayout anchor is outside of the window

I'm playing around with GridBagLayout in Swing and I noticed the anchor point on the left is correct, but the one at the top is at -60 pixels. 我在Swing中玩GridBagLayout ,我注意到左侧的锚点是正确的,但顶部的锚点是-60像素。 I know the exact size of the window, 960x640. 我知道窗口的确切尺寸是960x640。 I'm making two columns: one with two panels on the left (grid heights are 2 for both) and another with three panels on the right (grid heights are 1, 2 and 1). 我正在制作两列:一列在左侧有两个面板(两个的网格高度均为2),另一列在右侧有三个面板(网格的高度分别为1、2和1)。 This is the code: 这是代码:

    gameFrame.setLayout(new GridBagLayout());

    GridBagConstraints constraints = new GridBagConstraints();
    constraints.anchor = GridBagConstraints.NORTHWEST;

    InfoPanel infoPanel = new InfoPanel();
    infoPanel.setPreferredSize(new Dimension(320, 320));
    infoPanel.setMinimumSize(new Dimension(320, 320));
    infoPanel.setMaximumSize(new Dimension(320, 320));
    infoPanel.setBackground(Color.BLUE);
    constraints.gridx = 0;
    constraints.gridy = 0;
    //constraints.gridwidth = 1;
    constraints.gridheight = 2;

    gameFrame.add(infoPanel, constraints);


    InfoPanel infoPanel2 = new InfoPanel();
    infoPanel2.setPreferredSize(new Dimension(320, 320));
    infoPanel2.setMinimumSize(new Dimension(320, 320));
    infoPanel2.setMaximumSize(new Dimension(320, 320));
    infoPanel2.setBackground(Color.RED);
    constraints.gridx = 0;
    constraints.gridy = 2;
    //constraints.gridwidth = 1;
    constraints.gridheight = 2;

    gameFrame.add(infoPanel2, constraints);


    InfoPanel infoPanel3 = new InfoPanel();
    infoPanel3.setPreferredSize(new Dimension(640, 200));
    infoPanel3.setMinimumSize(new Dimension(640, 200));
    infoPanel3.setMaximumSize(new Dimension(640, 200));
    infoPanel3.setBackground(Color.GREEN);
    constraints.gridx = 1;
    constraints.gridy = 0;
    //constraints.gridwidth = 1;
    constraints.gridheight = 1;

    gameFrame.add(infoPanel3, constraints);


    InfoPanel infoPanel4 = new InfoPanel();
    infoPanel4.setPreferredSize(new Dimension(640, 240));
    infoPanel4.setMinimumSize(new Dimension(640, 240));
    infoPanel4.setMaximumSize(new Dimension(640, 240));
    infoPanel4.setBackground(Color.YELLOW);
    constraints.gridx = 1;
    constraints.gridy = 1;
    //constraints.gridwidth = 1;
    constraints.gridheight = 2;

    gameFrame.add(infoPanel4, constraints);


    InfoPanel infoPanel5 = new InfoPanel();
    infoPanel5.setPreferredSize(new Dimension(640, 200));
    infoPanel5.setMinimumSize(new Dimension(640, 200));
    infoPanel5.setMaximumSize(new Dimension(640, 200));
    infoPanel5.setBackground(Color.PINK);
    constraints.gridx = 1;
    constraints.gridy = 3;
    //constraints.gridwidth = 1;
    //constraints.gridheight = 1;

    gameFrame.add(infoPanel5, constraints);

    gameFrame.pack();
    gameFrame.setLocationRelativeTo(null);

I appreciate that the code isn't clean, it's not what it will look like, I just wanted to make sure I can get the layout I need working first. 我很欣赏代码不是很干净,也不是它的样子,我只是想确保自己可以得到需要首先使用的布局。

Here is the result: 结果如下: GridBagLayout

Because the layout starts placing objects at -60 pixels off the top, there's a 60 pixel gap at the bottom. 由于布局开始将对象放置在距离顶部-60像素的位置,因此底部存在60像素的间隙。

Has anyone come across something like this? 有没有人遇到过这样的事情? It baffles me. 这让我感到困惑。

I think this is one of those moments that that layout manager has thrown it's arms up in the air and said, "Well, that's the best I can do with what you've given me" . 我认为这是布局经理将其举起手臂然后说: “好吧,这是我能用您给我的东西做的最好的事情”之一

If you change constraints.gridheight = 2; 如果更改constraints.gridheight = 2; to constraints.gridheight = 3; constraints.gridheight = 3; for infoPanel2 it'll work. 对于infoPanel2它将起作用。

I "think" it has a combination of things to do with the total height of the second column when compared to the first (without consideration to the row spanning). “认为”与第二列的总高度相比,它与第一列的总和有关(不考虑行跨度)。 Allowing the second row to expand beyond the the two rows seems to allow it to collapse the extra space. 让第二行扩展到两行之外,似乎可以使其折叠多余的空间。

For what it's worth. 物有所值。 I would have probably put the two columns into individual panels and laid out those two panels instead, but that's just me... 我可能会将这两列放在单独的面板中,然后将这两个面板布置成小平面,但这只是我...

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

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