Java 列中的 GridBagLayout 问题

发布于 2024-10-26 07:51:22 字数 2770 浏览 0 评论 0原文

我正在尝试使用 GridBagLayout 布局管理器来实现此目的:

在此处输入图像描述

但是,我目前得到的是这个:

在此处输入图像描述

问题是橙色和棕色/灰色面板应该占据第二列,但是在运行代码时似乎只想占据第三。

我用于布局的代码:

 Container contentPane = form.getContentPane();
    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JPanel pnlGame = new JPanel();
    pnlGame.setBackground(Color.green); //temp
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.85;
    c.weighty = 0.65;
    contentPane.add(pnlGame, c);

    JPanel pnlBuy = new JPanel();
    c.gridx = 2;
    pnlBuy.setBackground(Color.blue); //temp
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.46;
    contentPane.add(pnlBuy, c);

    JPanel pnlUpgrade = new JPanel();
    pnlUpgrade.setBackground(Color.yellow); //temp
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.19;
    contentPane.add(pnlUpgrade, c);

    JPanel pnlStats = new JPanel();
    pnlStats.setBackground(Color.red); //temp
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.61;
    c.weighty = 0.35;
    contentPane.add(pnlStats, c);

    JPanel pnlSpeed = new JPanel();
    pnlSpeed.setBackground(Color.orange); //temp
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.04;
    contentPane.add(pnlSpeed, c);

    JPanel pnlRounds = new JPanel();
    pnlRounds.setBackground(Color.gray); //temp
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlRounds, c);

那么,我做错了什么?抱歉,如果我的英语有点糟糕,和/或我犯的错误非常明显……现在是凌晨 20 点到 5 点,我已经度过了漫长的一天。应该很快就可以了。

更新:

看来,如果我更改棕色/灰色面板的网格宽度,一切似乎都正确对齐,但最终我的布局中出现了令人讨厌的间隙。这里:

i.imgur.com/6JUx2.png

以及面板的代码(包括 Kevin S 建议的修正):

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

那么,我做错了什么吗,或者这只是 GridBagLayout 的一些奇怪的行为我要和谁一起生活?

不幸的是,由于我的编辑,我丢失了 Bala R 好心放入的所有嵌入内容。所以,恐怕我们又回到了图像链接。现在看来我不能发布两个以上的超链接,所以最后一个链接已被删除,你需要复制并粘贴它。

谢谢,Sam

I'm trying to use the GridBagLayout layout manager to achieve this:

enter image description here

However, what I am currently getting is this:

enter image description here

The problem being that the orange and brown/gray panels are supposed to occupy the second column, but seem to only want to occupy the third when it comes to running the code.

The code that I'm using for the layout:

 Container contentPane = form.getContentPane();
    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JPanel pnlGame = new JPanel();
    pnlGame.setBackground(Color.green); //temp
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.85;
    c.weighty = 0.65;
    contentPane.add(pnlGame, c);

    JPanel pnlBuy = new JPanel();
    c.gridx = 2;
    pnlBuy.setBackground(Color.blue); //temp
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.46;
    contentPane.add(pnlBuy, c);

    JPanel pnlUpgrade = new JPanel();
    pnlUpgrade.setBackground(Color.yellow); //temp
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.19;
    contentPane.add(pnlUpgrade, c);

    JPanel pnlStats = new JPanel();
    pnlStats.setBackground(Color.red); //temp
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.61;
    c.weighty = 0.35;
    contentPane.add(pnlStats, c);

    JPanel pnlSpeed = new JPanel();
    pnlSpeed.setBackground(Color.orange); //temp
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.04;
    contentPane.add(pnlSpeed, c);

    JPanel pnlRounds = new JPanel();
    pnlRounds.setBackground(Color.gray); //temp
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlRounds, c);

So, what am I doing wrong? Sorry if my English is a bit shitty, and/or the mistake I'm making is blindingly obvious... it's 20 to 5 in the morning, and I've had a long day. Should probably be hitting the hay, fairly shortly.

UPDATE:

It appears that if I change the gridwidth of the brown/gray panel, everything seems to align properly, but I end up with a nasty gap in my layout. Here:

i.imgur.com/6JUx2.png

And the code for the panel (including the amendment suggested by Kevin S):

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

So, is there anything that I'm doing wrong, or is this just some weird behaviour of the GridBagLayout that I'm going to have to live with?

Unfortunately, thanks to me editing, I've lost all the embeds that Bala R kindly put in there. So, we're back to the links for images, I'm afraid. And now it seems that I can't post more than two hyperlinks, so the link has been killed in the last one, you need to copy and paste it in.

Thanks, Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栩栩如生 2024-11-02 07:51:33

不完全适合您的解决方案,而更像是临时的解决方法。如果您在灰色面板之后添加这个新面板

    JPanel pnlTemp = new JPanel();
    pnlTemp.setBackground(Color.pink); //temp
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlTemp, c);

,它会修复布局。不知道为什么,但也许这会有所帮助,直到您(或其他人)能够找出正确的解决方案。这个新的粉红色面板不可见,但只是以某种方式帮助修复布局。 之后的布局

这是在此处输入图像描述

Not exactly asolution for you but more like a temporary workaround. if you add this new panel

    JPanel pnlTemp = new JPanel();
    pnlTemp.setBackground(Color.pink); //temp
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlTemp, c);

after you gray panel, it fixes the layout. Not sure why but maybe this will help until you are (or someone else is) able to figure out the correct solution. This new pink panel is not visible but just helps fix the layout somehow. Here's what the layout looks like after

enter image description here

如梦初醒的夏天 2024-11-02 07:51:33

您只想让布局发挥作用吗?

我做了一些修改。

 Container contentPane = form.getContentPane();
     contentPane.setLayout(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();

     JPanel pnlGame = new JPanel();
     pnlGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 1; // one column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlGame, c);

     // Added two new components
     // pnlgGame and pnlggGame

     // Covers up 2nd column and rows 0 and 1
     JPanel pnlgGame = new JPanel();
     pnlgGame.setBackground(Color.green); //temp
     c.gridx = 1;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 2; // two column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlgGame, c);

     // Covers 2nd row and column 1 
     JPanel pnlggGame = new JPanel();
     pnlggGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.20;
     contentPane.add(pnlggGame, c);


     JPanel pnlBuy = new JPanel();
     pnlBuy.setBackground(Color.blue); //temp
     c.gridx = 2;
     c.gridy = 0;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlBuy, c);

     JPanel pnlUpgrade = new JPanel();
     pnlUpgrade.setBackground(Color.yellow); //temp
     c.gridx = 2;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.20;
     contentPane.add(pnlUpgrade, c);

     JPanel pnlSpeed = new JPanel();
     pnlSpeed.setBackground(Color.BLUE); //temp
     c.gridx = 1;
     c.gridy = 2;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.05;
     contentPane.add(pnlSpeed, c);

     JPanel pnlStats = new JPanel();
     pnlStats.setBackground(Color.red); //temp
     c.gridx = 0;
     c.gridy = 2;
     c.gridwidth = 1;
     c.gridheight = 2;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlStats, c);


     JPanel pnlRounds = new JPanel();
     pnlRounds.setBackground(Color.gray); //temp
     c.gridx = 1;
     c.gridy = 3;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.35;
     contentPane.add(pnlRounds, c);

     form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     form.setVisible(true);

Do you just want the layout to work?

I did some modification.

 Container contentPane = form.getContentPane();
     contentPane.setLayout(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();

     JPanel pnlGame = new JPanel();
     pnlGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 1; // one column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlGame, c);

     // Added two new components
     // pnlgGame and pnlggGame

     // Covers up 2nd column and rows 0 and 1
     JPanel pnlgGame = new JPanel();
     pnlgGame.setBackground(Color.green); //temp
     c.gridx = 1;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 2; // two column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlgGame, c);

     // Covers 2nd row and column 1 
     JPanel pnlggGame = new JPanel();
     pnlggGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.20;
     contentPane.add(pnlggGame, c);


     JPanel pnlBuy = new JPanel();
     pnlBuy.setBackground(Color.blue); //temp
     c.gridx = 2;
     c.gridy = 0;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlBuy, c);

     JPanel pnlUpgrade = new JPanel();
     pnlUpgrade.setBackground(Color.yellow); //temp
     c.gridx = 2;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.20;
     contentPane.add(pnlUpgrade, c);

     JPanel pnlSpeed = new JPanel();
     pnlSpeed.setBackground(Color.BLUE); //temp
     c.gridx = 1;
     c.gridy = 2;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.05;
     contentPane.add(pnlSpeed, c);

     JPanel pnlStats = new JPanel();
     pnlStats.setBackground(Color.red); //temp
     c.gridx = 0;
     c.gridy = 2;
     c.gridwidth = 1;
     c.gridheight = 2;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlStats, c);


     JPanel pnlRounds = new JPanel();
     pnlRounds.setBackground(Color.gray); //temp
     c.gridx = 1;
     c.gridy = 3;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.35;
     contentPane.add(pnlRounds, c);

     form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     form.setVisible(true);
金兰素衣 2024-11-02 07:51:32

中间列中的所有组件至少也位于另一列中。因此GridBagLayout将中间列的首选宽度计算为0,这就是您看到的效果。

如果您想确保中间列具有更大的宽度,请在那里放置一些仅在此列中的组件。

All the components in your middle column are at least in one other column as well. So the GridBagLayout calculates the preferred width of the middle column as 0, and this is the effect you are seeing.

If you want to make sure your middle column has more width, put some component there which is only in this column.

初见终念 2024-11-02 07:51:32

所以看起来您需要修复 pnlRounds JPanel:

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

看起来您的问题是您的代码中有 c.gridx = 2 ,而 c.gridx = 1 是什么应该真的在那里。

另外,顺便说一句,底部面板的重量 x 不会增加到 1.00,而是增加到 0.99。只是觉得你应该知道。

So it looks like you need to fix the pnlRounds JPanel:

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

Looks like your problem is that you had c.gridx = 2 in your code when c.gridx = 1 is what should really be there.

Also, just as a side note, the bottom panels' weightx don't add to 1.00, they add to .99. Just thought you should know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文