我有一個大面板,包含3個較小的面板(locationPanel
,usagePanel
,structuralAspectsPanel
)。如何使用MigLayout將內容分佈在多個面板上?
每個較小的面板上都有一些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");
}
}