将组件的首选大小保持在 BorderLayout 的中心 [英] Keeping preferred sizes of components in center of BorderLayout

查看:50
本文介绍了将组件的首选大小保持在 BorderLayout 的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 BorderLayout 的中大型 UI;中心是一个选项卡式窗格,其中包含具有各种布局等的各种面板.

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.

带有丰富多彩词汇的各种海报警告不要在组件上使用任何 setXXXsize() 方法的危险.这就是我现在所做的,我想学习如何避免它.

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 不适用于我的某些面板.它本质上是围绕行和列定位的,并不是所有的东西都适合行和列.当然,我可以创建人为的行和列来适应所有内容,但我真的希望 Swing 有更多的布局选项.

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.

立式胶水也不行.我已将其包含在 HFOE 深受喜爱的 SSCE 中:

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?

推荐答案

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.

这篇关于将组件的首选大小保持在 BorderLayout 的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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