【问题标题】:GridBagLayout is in the center of BoxLayout instead of being on the topGridBagLayout 在 BoxLayout 的中心,而不是在顶部
【发布时间】:2013-08-30 03:33:43
【问题描述】:

以下示例描述了以下应用:

BorderLayout NORTH 上的按钮,将带有一些组件的 GridBagLayout 面板添加到 BorderLayout.CENTER 内的 BoxLayout.Y_AXIS。但是点击按钮后,面板出现在中心,而不是被添加到顶部。

public class Test extends JFrame {
    public Test() {
        super("Test");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        JButton add = new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.add(new RecordPanel("text1", "text2"));
                panel.revalidate();
            }
        });

        Container container = getContentPane();
        container.add(add, BorderLayout.PAGE_START);
        container.add(panel, BorderLayout.CENTER);

        setSize(300, 500);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }

    private class RecordPanel extends JPanel {
        private JRadioButton radioButton;
        private JLabel textLabel1;
        private JLabel textLabel2;

        public RecordPanel(String text1, String text2) {
            super();
            radioButton = new JRadioButton();
            textLabel1 = new JLabel(text1);
            textLabel2 = new JLabel(text2);
            initGUI();
        }

        private void initGUI() {
            setLayout(new GridBagLayout());
            GridBagConstraints constraints = new GridBagConstraints();

            constraints.gridx = 0;
            constraints.gridy = 0;
            add(radioButton, constraints);

            constraints.gridx = 1;
            constraints.gridy = 0;
            constraints.weightx = 1.0;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            add(textLabel1, constraints);

            constraints.gridx = 1;
            constraints.gridy = 1;
            constraints.weightx = 1.0;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            add(textLabel2, constraints);
        }
    }
}

使用锚点 (constraints.anchor = GridBagConstraints.FIRST_LINE_START) 设置单选按钮和一个标签到顶部,但第二个标签仍然出现在中心。

如何让面板显示在顶部而不是中心?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    问题在于,对于新添加的RecordPanel 组件,最大大小默认定义为Integer.MAX_VALUE x Integer.MAX_VALUE,从而导致使用最大可用区域。这个维度由BoxLayout 观察。

    您可以在 RecordPanel 类中覆盖 getMaximumSize

    @Override
    public Dimension getMaximumSize() {
       return new Dimension(300, getPreferredSize().height);
    }
    

    documentation 详细介绍了如何使用 BoxLayout 实现大小调整

    【讨论】:

    • BoxLayout doc from oracle 提供有关布局的最大和最小尺寸信息。
    • 如果为每个 GridBagLayout 添加边框,您会看到该单元格占用了所有可用空间。但是如果我设置了特定的高度,我可能会犯错,因为在大标签文本的情况下高度可能会低于/高于 30(如果我正确理解你的意思)。
    • 然后使用更新中显示的首选尺寸高度...:)
    【解决方案2】:

    元素 GridBagConstraints.anchor 是正确的选择,但您必须使用这些值之一 GridBagConstraints.NORTH 到 GridBagConstraints.NORTHWEST。

    例如constraints.anchor = GridBagConstraints.NORTH

    【讨论】:

    • 在我看来,您需要添加一个 JPanel 来填充 JFrame 的完整大小并在其中插入 GridBagConstraints。
    • 嗯,您的意思是用 Jpanel(例如 FlowLayout)包装当前的 GridBagLayout,然后用另一个 GridBagLayout 包装它?
    【解决方案3】:

    如果您不介意使用 MigLayout 而不是 BoxLayout,这可能是您的解决方案:

    首先,将您的面板布局更改为 MigLayout:

    panel.setLayout(new MigLayout("", "[]", "[]"));
    

    为类创建一个静态变量以计算新的“RecordPanel”元素添加到哪一行:

    public class Test extends JFrame {
        static int index = 0;
    

    然后将面板行中的add替换为:

    panel.add(new RecordPanel("text1", "text2"), "cell 0 " + index++);
    

    这将把每个新的RecordPanel 插入一个新行,这要归功于index 总是递增的。

    您可能希望将面板包装到 ScrollView 中,以便在元素填充面板时添加滚动条。

    【讨论】:

      猜你喜欢
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多