Keeping preferred sizes of components in center of BorderLayout

天大地大妈咪最大 提交于 2019-12-23 10:48:09

问题


I have a medium-large UI that uses a BorderLayout; the center is a tabbed pane containing various panels with various layouts, etc.

I want the panel in the center of this border layout to resize according to the size of the window, but I don't want the components within the panel to stretch. Labels, combo boxes, text fields, buttons -- I want them to stay at their preferred sizes, and allow the panel that contains them to stretch. I put them in a scroll pane in case the space gets too small for the panel.

Various posters with colorful vocabularies warn against the danger of using any of the setXXXsize() methods on components. That's what I do now, and I'd like to learn how to avoid it.

GridBagLayout is not appropriate for some of my panels. It is, by nature, oriented around rows and columns, and not everything fits into rows and columns. Of course I could create artificial rows and columns to fit everything into, but I'm really hoping Swing has more layout options than that.

Vertical Glue doesn't do it either. I've included it in HFOE's beloved 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();
        }

    }

If you enlarge the window, the comboboxes in the center enlarge; as written, a vertical glue piece below them also enlarges, but doesn't take up all available space. It appears it is given as much space as each of them.

So what is a good way to approach this?


回答1:


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

}

See also this answer. There is more than one way to solve this.




回答2:


I would recommend you using JPanel with javax.swing.GroupLayout. However it is not easy to use this layout by coding it. Use code generators like Netbeans Matisse Builder and copy paste it to wherever you want if you dont want to use the IDE.



来源:https://stackoverflow.com/questions/9537019/keeping-preferred-sizes-of-components-in-center-of-borderlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!