打破我的旧习惯,我使用GridBagLayout创建了一个GUI。我对这个容器还很新,所以很明显我的代码会有缺陷。
当我运行程序时,GUI看起来像这样:http://i.imgur.com/90J0paH.png
第一次尝试并不算太糟糕,但窗户高度比我预期的要大!我将滚动窗格添加到单独的面板,以便我可以使应用程序更小。因为我没有设置窗口大小的限制,我认为滚动条没有显示,因为Java自动调整窗口大小,以便尽可能多的组件显示。
此外,底部标签“0 out of ..”有点向右偏移。没什么大不了的。
但真正的恐怖表现在我调整窗口大小时。 http://i.imgur.com/njI9cgn.png
现在我们得到了滚动条,但是底部面板突然变大了,其他三个面板变得奇怪了。 (水平滚动条甚至不应该显示)
这是我的代码。感谢所有可以提供帮助的GUI专家!
...create the components blah blah blah
//----------------------- ADD COMPONENTS TO PANELS -----------------------
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0.5;
c.ipadx = 3;
c.ipady = 3;
//TODO: Add "Level", "Amount", etc. labels
for (int i = 0; i < 10; i++) //Normal troops
{
c.gridy = i;
c.gridx = 0;
c.insets = new Insets(0, 0, 0, 0);
normalTroopPanel.add(icon[i], c);
c.insets = new Insets(10, 10, 10, 0);
c.gridx = 1;
normalTroopPanel.add(setLevel[i], c);
c.gridx = 2;
normalTroopPanel.add(setAmount[i], c);
c.insets = new Insets(10, 20, 10, 0);
c.gridx = 3;
normalTroopPanel.add(typeTotalCost[i], c);
c.gridx = 4;
normalTroopPanel.add(typeTotalTime[i], c);
}
normalTroopPanel.setPreferredSize(new Dimension(400, 520));
for (int i = 10; i < 15; i++) //Dark troops
{
c.gridy = i;
c.gridx = 0;
c.insets = new Insets(0, 0, 0, 0);
darkTroopPanel.add(icon[i], c);
c.insets = new Insets(10, 10, 10, 0);
c.gridx = 1;
darkTroopPanel.add(setLevel[i], c);
c.gridx = 2;
darkTroopPanel.add(setAmount[i], c);
c.insets = new Insets(10, 20, 10, 0);
c.gridx = 3;
darkTroopPanel.add(typeTotalCost[i], c);
c.gridx = 4;
darkTroopPanel.add(typeTotalTime[i], c);
}
darkTroopPanel.setPreferredSize(new Dimension(400, 250));
for (int i = 15; i < 20; i++) //Spells
{
c.gridy = i;
c.gridx = 0;
c.insets = new Insets(0, 0, 0, 0);
spellPanel.add(icon[i], c);
c.insets = new Insets(10, 10, 10, 0);
c.gridx = 1;
spellPanel.add(setLevel[i], c);
c.gridx = 2;
spellPanel.add(setAmount[i], c);
c.insets = new Insets(10, 20, 10, 0);
c.gridx = 3;
spellPanel.add(typeTotalCost[i], c);
c.gridx = 4;
spellPanel.add(typeTotalTime[i], c);
}
spellPanel.setPreferredSize(new Dimension(400, 250));
//Totals panel
c.gridx = 0;
c.gridy = 0;
totalsPanel.add(spaceTaken, c);
c.gridx = 1;
totalsPanel.add(setHousingLimit, c);
c.gridx = 2;
totalsPanel.add(totalElixirCost, c);
c.gridx = 3;
totalsPanel.add(totalDarkCost, c);
c.gridx = 4;
totalsPanel.add(totalTime, c);
//Add scrollbars to our troop/spell panels so they can all fit in a reasonable window
JScrollPane normalTroopScroll = new JScrollPane(normalTroopPanel);
JScrollPane darkTroopScroll = new JScrollPane(darkTroopPanel);
JScrollPane spellScroll = new JScrollPane(spellPanel);
//Bundle it all up!
c.gridx = 0;
c.gridy = 0;
c.gridheight = 2;
armyEntryPanel.add(normalTroopScroll, c);
c.gridx = 1;
c.gridheight = 1;
armyEntryPanel.add(darkTroopScroll, c);
c.gridy = 1;
armyEntryPanel.add(spellScroll, c);
c.gridy = 2;
c.gridx = 0;
c.gridwidth = 2;
armyEntryPanel.add(totalsPanel, c);
}
答案 0 :(得分:3)
fill
和weighty
的组合导致了主要问题。
尝试为顶部的组件(滚动窗格)设置weighty
到1
,为底部的组件设置weighty
到0
。您也可以考虑将填充设置为NONE
(对于底部组件)
基本上,你永远不会重置约束
答案 1 :(得分:0)
我建议您根据屏幕分辨率,字体大小等对底部彩色面板进行preferredDimension
。这样,它可以 保持您希望它保持的方式。
另外,请尝试为这些组件设置Insets
,看看是否有帮助。