Java列中的GridBagLayout [英] GridBagLayout in Java Column Issue

查看:78
本文介绍了Java列中的GridBagLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,我现在得到的是:

However, what I am currently getting is this:

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

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.

我用于布局的代码:

 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点,我已经忙了一整天.可能很快就会出手了.

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.

更新:

看来,如果我更改棕色/灰色面板的网格宽度,则所有内容似乎都可以正确对齐,但是最终在布局中出现了令人讨厌的缝隙.在这里:

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

i.imgur.com/6JUx2.png

小组的代码(包括Kevin S建议的修正案):

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);

那么,我有做错什么吗,还是这仅仅是我将不得不忍受的GridBagLayout的一些怪异行为?

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?

不幸的是,多亏了我的编辑,我才失去了Bala R亲切放入的所有嵌入物.因此,恐怕我们将返回图像的链接.现在看来,我不能发布两个以上的超链接,因此该链接在上一个链接中已被杀死,您需要将其复制并粘贴到其中.

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.

谢谢,山姆

推荐答案

中间列中的所有组件至少也位于另一列中.因此,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.

这篇关于Java列中的GridBagLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆