我有一个使用BorderLayout的中大型UI;该中心是一个标签式窗格,包含各种布局等各种面板。
我希望此边框布局中心的面板根据大小调整大小 窗口,但我不希望面板内的组件伸展。标签,组合框,文本字段,按钮 - 我希望它们保持其首选尺寸,并允许包含它们的面板拉伸。我将它们放在滚动窗格中,以防空间对于面板来说太小。
各种海报与丰富多彩的词汇表警告在组件上使用任何setXXXsize()方法的危险。这就是我现在所做的,而且我想学习如何避免它。
GridBagLayout不适合我的某些面板。它本质上是围绕行和列定向的,并不是所有东西都适合行和列。当然,我可以创建人工行和列以适应所有内容,但我真的希望Swing有更多的布局选项。
垂直胶水也不能。我把它包含在HFOE心爱的SSCE中:
package example;
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BorderAndBox extends JFrame
{
public static void main(String args[])
{
BorderAndBox bnb = new BorderAndBox();
bnb.createUI();
bnb.setVisible(true);
}
public void createUI()
{
JPanel borderPanel = new JPanel(new BorderLayout());
JLabel northLabel = new JLabel("Nawth");
borderPanel.add(northLabel, BorderLayout.NORTH);
String[] southComboChoices = { "one", "two", "three" };
JComboBox southCombo = new JComboBox(southComboChoices);
borderPanel.add(southCombo, BorderLayout.SOUTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
String[] firstChoices = { "first", "uno", "UN" };
String[] secondChoices = { "second", "dos", "zwei" };
String[] thirdChoices = { "third", "tres", "drei" };
JComboBox firstCombo = new JComboBox(firstChoices);
JComboBox secondCombo = new JComboBox(secondChoices);
JComboBox thirdCombo = new JComboBox(thirdChoices);
centerPanel.add(firstCombo);
centerPanel.add(secondCombo);
centerPanel.add(thirdCombo);
centerPanel.add(Box.createVerticalGlue()); // first attempt; does NOT
// take up available vertical space, instead it appears to create a space
// that is shared equally among the (now) four components of this space.
borderPanel.add(centerPanel, BorderLayout.CENTER);
getContentPane().add(borderPanel);
pack();
}
}
如果放大窗口,中间的组合框会放大;如上所述,它们下方的垂直胶片也会扩大,但不会占用所有可用空间。似乎给它们提供了与每个空间一样多的空间。
那么解决这个问题的好方法是什么?
答案 0 :(得分:10)
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BorderAndBox extends JFrame
{
public static void main(String args[])
{
BorderAndBox bnb = new BorderAndBox();
bnb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bnb.createUI();
bnb.setVisible(true);
}
public void createUI()
{
JPanel borderPanel = new JPanel(new BorderLayout());
JLabel northLabel = new JLabel("Nawth");
borderPanel.add(northLabel, BorderLayout.NORTH);
String[] southComboChoices = { "one", "two", "three" };
JComboBox southCombo = new JComboBox(southComboChoices);
borderPanel.add(southCombo, BorderLayout.SOUTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
String[] firstChoices = { "first", "uno", "UN" };
String[] secondChoices = { "second", "dos", "zwei" };
String[] thirdChoices = { "third", "tres", "drei" };
JComboBox firstCombo = new JComboBox(firstChoices);
JComboBox secondCombo = new JComboBox(secondChoices);
JComboBox thirdCombo = new JComboBox(thirdChoices);
centerPanel.add(firstCombo);
centerPanel.add(secondCombo);
centerPanel.add(thirdCombo);
centerPanel.add(Box.createVerticalGlue()); // first attempt; does NOT
// take up available vertical space, instead it appears to create a space
// that is shared equally among the (now) four components of this space.
JPanel centerPanelConstrain = new JPanel(new GridBagLayout());
centerPanelConstrain.add(centerPanel);
borderPanel.add(centerPanelConstrain, BorderLayout.CENTER);
getContentPane().add(borderPanel);
pack();
}
}
另见this answer。解决这个问题的方法不止一种。
答案 1 :(得分:-1)
我建议您将JPanel
与javax.swing.GroupLayout
一起使用。但是通过编码来使用这种布局并不容易。使用Netbeans Matisse Builder之类的代码生成器,如果您不想使用IDE,请将其粘贴到您想要的任何位置。