使BoxLayout的单元格保持大小并且仅在Java中调整胶水大小

时间:2019-03-25 19:17:06

标签: java swing layout-manager boxlayout

我正在尝试创建一个设置面板,我希望能够继续查看什么设置属于什么文本。所以我将每个带有jcheckbox或jspinner的jlabel放在一个jpanel中,然后将它们添加到jpanels列中。我有4列jpanels,它们彼此相邻。在所附的图像中,您可以看到2种不同的宽度尺寸。我希望红色区域是静态的,而蓝色区域要调整大小,因为那样可以看到什么jlabel属于什么设置。 enter image description here enter image description here

我这里有一个SSCCE

import javax.swing.*;
import java.awt.*;

public class SSCCE {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            JFrame jFrame = new JFrame();
            jFrame.setMinimumSize(new Dimension(580, 113));
            jFrame.add(new SettingsPanel());
            jFrame.pack();
            jFrame.setLocationRelativeTo(null);
            jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame.setVisible(true);
        });
    }

    public static class Setting extends JPanel {
        public String name;
        public JComponent component;

        public Setting(String name, JComponent component) {
            this.name = name;
            this.component = component;
            setLayout(new BorderLayout());
            add(new JLabel(name), BorderLayout.LINE_START);
            add(component, BorderLayout.LINE_END);
        }
    }

    private static class SettingsPanel extends JPanel {

        public SettingsPanel() {
            setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

            JPanel subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Amount of faces", new JSpinner()));
            subPanel.add(new Setting("Amount of vertices", new JSpinner()));
            subPanel.add(new Setting("Amount of textured faces", new JSpinner()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Textures", new JCheckBox()));
            subPanel.add(new Setting("Priorities", new JCheckBox()));
            subPanel.add(new Setting("Alpha", new JCheckBox()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("TSkins", new JCheckBox()));
            subPanel.add(new Setting("VSkins", new JCheckBox()));
            subPanel.add(new Setting("X data", new JSpinner()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Y data", new JSpinner()));
            subPanel.add(new Setting("Z data", new JSpinner()));
            subPanel.add(new Setting("Triangle data", new JSpinner()));
            add(subPanel);

            setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        }
    }
}


编辑

我应用了卡米克尔的答案,这就是结果enter image description here 如您所见,jspinners和jcheckboxes现在不再排列,看起来有些混乱,但是调整大小的方式是我想要的功能。这是带有Camickr答案的最新SSCCE

import javax.swing.*;
import java.awt.*;

public class SSCCE {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
            JFrame jFrame = new JFrame();
            jFrame.setMinimumSize(new Dimension(580, 113));
            jFrame.add(new SettingsPanel());
            jFrame.pack();
            jFrame.setLocationRelativeTo(null);
            jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jFrame.setVisible(true);
        });
    }

    public static class Setting extends JPanel {
        public String name;
        public JComponent component;

        public Setting(String name, JComponent component) {
            this.name = name;
            this.component = component;
            setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
            add(new JLabel(name), BorderLayout.LINE_START);
            add(Box.createRigidArea(new Dimension(50, 0)));
            add(component, BorderLayout.LINE_END);
        }
    }

    private static class SettingsPanel extends JPanel {

        public SettingsPanel() {
            setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

            JPanel subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Amount of faces", new JSpinner()));
            subPanel.add(new Setting("Amount of vertices", new JSpinner()));
            subPanel.add(new Setting("Amount of textured faces", new JSpinner()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Textures", new JCheckBox()));
            subPanel.add(new Setting("Priorities", new JCheckBox()));
            subPanel.add(new Setting("Alpha", new JCheckBox()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("TSkins", new JCheckBox()));
            subPanel.add(new Setting("VSkins", new JCheckBox()));
            subPanel.add(new Setting("X data", new JSpinner()));
            add(subPanel);

            add(Box.createHorizontalGlue());

            subPanel = new JPanel(new GridLayout(0, 1));
            subPanel.add(new Setting("Y data", new JSpinner()));
            subPanel.add(new Setting("Z data", new JSpinner()));
            subPanel.add(new Setting("Triangle data", new JSpinner()));
            add(subPanel);

            setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

我希望红色区域是静态的

使用Box.createRigidArea(...)代替“胶水”。

有关更多信息,请参见Using Invisible Components as Filler的Swing教程中的部分。

编辑:

了解所使用的布局管理器的详细信息。

问题是SettingPanel。您正在使用水平的BoxLayout。 BoxLayout遵守添加到其中的任何组件的最大大小。您正在添加一个没有最大大小的JPanel,因此每个面板的大小都会增加,这会导致GridLayout也增加,从而导致BorderLayout组件中的空隙。

您要防止面板尺寸增加。为了证明这一概念的简单更改,是在添加所有面板时添加以下内容:

subPanel.setMaximumSize( subPanel.getPreferredSize() );
add(subPanel);

更好的解决方案是扩展JPanel并重写getMaximumSize()方法以返回getPreferredSize(),以允许动态更改面板的首选大小。

实施此建议后,我建议您删除:

jFrame.setMinimumSize(new Dimension(580, 113)); 

使用随机数绝不是一个好主意。让布局管理器确定大小。

执行此操作时,您会看到所有组件都聚集在一起,因此我建议您进行更改:

//setLayout(new BorderLayout());
setLayout(new BorderLayout(10, 0));

这将在每个标签/组件之间留出空间。

然后我要添加:

add(Box.createRigidArea(new Dimension(50, 0)));
//add(Box.createHorizontalGlue());

您真的需要胶水吗?上面将给出每个面板之间的固定间距。