gpt4 book ai didi

java - 使 BoxLayout 的单元格保持大小并且仅在 java 中调整粘合大小

转载 作者:行者123 更新时间:2023-11-30 05:43:00 25 4
gpt4 key购买 nike

我正在尝试制作一个设置面板,我希望能够继续查看哪些设置属于哪些文本。因此,我将每个带有 jcheckbox 或 jspinner 的 jlabel 放入 jpanel 中,然后将它们添加到 jpanels 列中。我有 4 列 jpanels,它们彼此相邻。在附图中,您可以看到两种不同的宽度尺寸。我希望红色区域保持静态,蓝色区域调整大小,因为这样您就可以看到哪个 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));
}
}
}


编辑

我应用了 Camickr 的答案,这就是结果 enter image description here正如您所看到的,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(...)而不是“胶水”。

请参阅 Swing 教程中 Using Invisible Components as Filler 的部分了解更多信息。

编辑:

搞乱了您正在使用的布局管理器的详细信息。

问题出在设置面板上。您正在使用水平 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());

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

关于java - 使 BoxLayout 的单元格保持大小并且仅在 java 中调整粘合大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55344985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com