Keeping preferred sizes of components in center of BorderLayout
我有一个使用BorderLayout的中型UI;中心是一个选项卡式窗格,其中包含具有不同布局等的各种面板。
我希望此边框布局中心的面板根据
窗口,但我不希望面板中的组件伸展。标签,组合框,文本字段,按钮-我希望它们保持其首选大小,并允许包含它们的面板伸展。我将它们放在滚动窗格中,以防面板的空间过小。
各种带有丰富词汇的海报警告不要在组件上使用任何setXXXsize()方法的危险。这就是我现在要做的,并且我想学习如何避免它。
GridBagLayout不适合我的某些面板。从本质上讲,它是围绕行和列定向的,并非所有内容都适合行和列。当然,我可以创建人造的行和列以适合所有内容,但是我真的希望Swing具有更多的布局选项。
立式胶水也不做。我已将其包含在HFOE最受欢迎的SSCE中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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(); } } |
如果放大窗口,则中间的组合框会放大;如所写,它们下面的垂直胶水也会增大,但不会占用所有可用空间。似乎给它们提供了尽可能多的空间。
那么解决这个问题的好方法是什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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(); } } |
另请参阅此答案。 解决此问题的方法不止一种。
我建议您将