【发布时间】:2016-10-07 03:51:03
【问题描述】:
我有一个大面板,其中包含 3 个较小的面板(locationPanel、usagePanel、structuralAspectsPanel)。
每个较小的面板上都有一些JLabels 和JCheckBoxes。我继续将每个面板上的组件居中,但是我怎样才能在所有 3 个面板上完成居中呢? (请看中间的黑线)
我尝试在MigLayout (new JLabel("Label here"), "cell 0 0") 中使用单元格布局选项,但无法动态创建相同大小的间隙,以便所有组件居中。使用gap 200 (180, 300 , ...) 将组件“推”到视觉中心似乎可行,但我想避免使用绝对定位/间隙,因为它们可能很容易损坏。
这是我的源代码:
public class RiskAssessmentPage extends JPanel {
JPanel riskAssessmentPanel = new JPanel();
JPanel locationPanel = new JPanel();
JPanel usagePanel = new JPanel();
JPanel structuralAspectsPanel = new JPanel();
public RiskAssessmentPage() {
setLayout(new MigLayout(""));
riskAssessmentPanel.setLayout(
new MigLayout("wrap", "[grow, fill]", "[grow, fill, push][grow, fill, push][grow, fill, push]"));
locationPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[grow, center][grow, left]"));
structuralAspectsPanel.setBorder(BorderFactory.createTitledBorder("Structural Aspects"));
locationPanel.add(new JLabel("This is the first of all labels"));
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JSeparator(), "growx, span");
locationPanel.add(new JLabel("Second Label"));
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JSeparator(), "growx, span");
locationPanel.add(new JLabel("This Label is fairly large and long and pushes the text around"));
locationPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
locationPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JLabel("A label in the usage panel"));
usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JSeparator(), "growx, span");
usagePanel.add(new JLabel("And another one and another one and another one"));
usagePanel.add(new JCheckBox("Checkbox with Label"), "wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
usagePanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JLabel("Label here"));
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JSeparator(), "growx, span");
structuralAspectsPanel.add(new JLabel("I am so uncreative with label names..."));
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JSeparator(), "growx, span");
structuralAspectsPanel.add(new JLabel("Thats it. I give up with naming them."));
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
structuralAspectsPanel.add(new JCheckBox("Checkbox with Label"), "skip, wrap");
riskAssessmentPanel.add(locationPanel);
riskAssessmentPanel.add(usagePanel);
riskAssessmentPanel.add(structuralAspectsPanel);
add(riskAssessmentPanel, "grow, push");
}
}
【问题讨论】: