使 BoxLayout 的单元格保持大小并且仅在 java 中调整胶水大小
Make cells of BoxLayout keep size and only resize glue in java
我正在尝试制作一个设置面板,我希望能够一直看到哪些设置属于哪些文本。所以我将每个带有 jcheckbox 或 jspinner 的 jlabel 放在 jpanel 中,然后将它们添加到列 jpanels。我有 4 列 jpanels,它们彼此相邻。在所附图片中,您可以看到 2 种不同的宽度尺寸。我希望红色区域是静态的,蓝色区域可以调整大小,因为这样你就可以看到什么 jlabel 属于什么设置。
我这里有一个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));
}
}
}
编辑
我应用了 Camickr 的答案,这就是结果
如您所见,jspinners 和 jcheckboxes 现在不再排成一行,看起来有点乱,但它调整大小的方式正是我想要的功能。这是更新后的 SSCCE 和 Camickr 的回答
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));
}
}
}
I want the red areas to be static
使用 Box.createRigidArea(...)
而不是 "glue"。
有关详细信息,请参阅 Using Invisible Components as Filler 上的 Swing 教程部分。
编辑:
弄乱了您正在使用的布局管理器的详细信息。
问题出在设置面板上。您正在使用水平 BoxLayout。 BoxLayout 遵守添加到它的任何组件的最大尺寸。您正在添加一个没有最大尺寸的 JPanel,因此每个面板的尺寸都会增加,这会导致 GridLayout 也增加,从而导致 BorderLayout 组件出现间隙。
您想防止面板变大。证明概念的简单更改是在添加所有面板时添加以下内容:
subPanel.setMaximumSize( subPanel.getPreferredSize() );
add(subPanel);
更好的解决方案是扩展 JPanel 并将 getMaximumSize()
方法重写为 return getPreferredSize()
以允许动态更改面板的首选大小。
实施此建议后,我建议您删除:
jFrame.setMinimumSize(new Dimension(580, 113));
使用随机数从来都不是一个好主意。让布局管理器确定大小。
执行此操作时,您会看到所有组件聚集在一起,因此我建议您更改:
//setLayout(new BorderLayout());
setLayout(new BorderLayout(10, 0));
这将在每个 label/component 之间给出 space。
然后我会补充:
add(Box.createRigidArea(new Dimension(50, 0)));
//add(Box.createHorizontalGlue());
你真的需要胶水吗?以上将给出每个面板之间的固定间距。
我正在尝试制作一个设置面板,我希望能够一直看到哪些设置属于哪些文本。所以我将每个带有 jcheckbox 或 jspinner 的 jlabel 放在 jpanel 中,然后将它们添加到列 jpanels。我有 4 列 jpanels,它们彼此相邻。在所附图片中,您可以看到 2 种不同的宽度尺寸。我希望红色区域是静态的,蓝色区域可以调整大小,因为这样你就可以看到什么 jlabel 属于什么设置。
我这里有一个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));
}
}
}
编辑
我应用了 Camickr 的答案,这就是结果
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));
}
}
}
I want the red areas to be static
使用 Box.createRigidArea(...)
而不是 "glue"。
有关详细信息,请参阅 Using Invisible Components as Filler 上的 Swing 教程部分。
编辑:
弄乱了您正在使用的布局管理器的详细信息。
问题出在设置面板上。您正在使用水平 BoxLayout。 BoxLayout 遵守添加到它的任何组件的最大尺寸。您正在添加一个没有最大尺寸的 JPanel,因此每个面板的尺寸都会增加,这会导致 GridLayout 也增加,从而导致 BorderLayout 组件出现间隙。
您想防止面板变大。证明概念的简单更改是在添加所有面板时添加以下内容:
subPanel.setMaximumSize( subPanel.getPreferredSize() );
add(subPanel);
更好的解决方案是扩展 JPanel 并将 getMaximumSize()
方法重写为 return getPreferredSize()
以允许动态更改面板的首选大小。
实施此建议后,我建议您删除:
jFrame.setMinimumSize(new Dimension(580, 113));
使用随机数从来都不是一个好主意。让布局管理器确定大小。
执行此操作时,您会看到所有组件聚集在一起,因此我建议您更改:
//setLayout(new BorderLayout());
setLayout(new BorderLayout(10, 0));
这将在每个 label/component 之间给出 space。
然后我会补充:
add(Box.createRigidArea(new Dimension(50, 0)));
//add(Box.createHorizontalGlue());
你真的需要胶水吗?以上将给出每个面板之间的固定间距。