如何使用 MigLayout 将内容集中在多个面板上?
How to center content over several panels with MigLayout?
我有一个大面板,其中包含 3 个较小的面板(locationPanel
、usagePanel
、structuralAspectsPanel
)。
每个较小的面板上都有一些 JLabels
和 JCheckBoxes
。我继续将每个面板上的组件居中,但是我怎样才能在所有 3 个面板上实现它们的居中呢? (请看中间的黑线)
我曾尝试使用 MigLayout
中的单元格布局选项(new JLabel("Label here"), "cell 0 0")
,但无法动态创建相同大小的间隙,因此所有组件都居中。使用 gap 200
(180, 300, ...) 到 "push" 将组件放入视觉中心似乎可行,但我想避免使用绝对定位/间隙,因为它们可能很容易损坏。
我附上了我的问题的图片:
这是我的源代码:
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");
}
}
我想通了,其实很简单!
将 0:0, grow 60
添加到第一列,将 grow 40
添加到第二列。
grow 60 / 40
部分表示左列占可用宽度的 60%,而另一列占 40%。
0:0
部分是关于设置最小/首选/最大宽度(虽然我不得不承认我不完全理解它,欢迎解释!......我已经就是为了这个。)。
由于所有面板的左右尺寸相同,因此它们居中于同一点。
locationPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
为了使您的面板列对齐,您需要将它们设置为相同的大小。在 MigLayout
中,您可以通过将第一列的 preferred size 和 growth rate 设置为相同的值来做到这一点你在所有面板中。例如
panel1.new MigLayout("", //Unchanged Layout Constraints
"[:500, grow, center][grow, left]", //Column Constraints
""); //Unchanged Row Constraints
panel2.new MigLayout("", //Unchanged Layout Constraints
"[:500, grow, center][grow, left]", //Column Constraints
""); //Unchanged Row Constraints
在此示例中,panel1
和 panel2
的第一列的首选大小都设置为 500,并且增长设置为默认值,因此也相同。
可在此处查看所描述内容的工作示例
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class MigLay extends JFrame {
private MigLay() {
super("Button Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new MigLayout("wrap", //Layout Constraints
"grow, fill", //Column Constraints
"grow, fill")); //Row Constraints
createPanels();
pack();
setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen
setVisible(true);
}
private void createPanels() {
JPanel locationPanel = new JPanel();
JPanel usagePanel = new JPanel();
JPanel structuralAspectsPanel = new JPanel();
//JLabels for font metrics
JLabel one = new JLabel("This is the first of all labels");
JLabel two = new JLabel("Second Label");
JLabel three = new JLabel("This Label is fairly large and long and pushes the text around");
JLabel four = new JLabel("A label in the usage panel");
JLabel five = new JLabel("And another one and another one and another one");
//Font Metrics
FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code)
int width = metrics.stringWidth(three.getText());
locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));
locationPanel.add(one);
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(two);
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(three);
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(four);
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(five);
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");
getContentPane().add(locationPanel);
getContentPane().add(usagePanel);
}
public static void main(String[] args) {
new MigLay();
}
}
我有一个大面板,其中包含 3 个较小的面板(locationPanel
、usagePanel
、structuralAspectsPanel
)。
每个较小的面板上都有一些 JLabels
和 JCheckBoxes
。我继续将每个面板上的组件居中,但是我怎样才能在所有 3 个面板上实现它们的居中呢? (请看中间的黑线)
我曾尝试使用 MigLayout
中的单元格布局选项(new JLabel("Label here"), "cell 0 0")
,但无法动态创建相同大小的间隙,因此所有组件都居中。使用 gap 200
(180, 300, ...) 到 "push" 将组件放入视觉中心似乎可行,但我想避免使用绝对定位/间隙,因为它们可能很容易损坏。
我附上了我的问题的图片:
这是我的源代码:
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");
}
}
我想通了,其实很简单!
将 0:0, grow 60
添加到第一列,将 grow 40
添加到第二列。
grow 60 / 40
部分表示左列占可用宽度的 60%,而另一列占 40%。
0:0
部分是关于设置最小/首选/最大宽度(虽然我不得不承认我不完全理解它,欢迎解释!......我已经
由于所有面板的左右尺寸相同,因此它们居中于同一点。
locationPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
structuralAspectsPanel.setLayout(new MigLayout("gap rel 2", "[0:0, grow 60, center][grow 40, left]"));
为了使您的面板列对齐,您需要将它们设置为相同的大小。在 MigLayout
中,您可以通过将第一列的 preferred size 和 growth rate 设置为相同的值来做到这一点你在所有面板中。例如
panel1.new MigLayout("", //Unchanged Layout Constraints
"[:500, grow, center][grow, left]", //Column Constraints
""); //Unchanged Row Constraints
panel2.new MigLayout("", //Unchanged Layout Constraints
"[:500, grow, center][grow, left]", //Column Constraints
""); //Unchanged Row Constraints
在此示例中,panel1
和 panel2
的第一列的首选大小都设置为 500,并且增长设置为默认值,因此也相同。
可在此处查看所描述内容的工作示例
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class MigLay extends JFrame {
private MigLay() {
super("Button Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new MigLayout("wrap", //Layout Constraints
"grow, fill", //Column Constraints
"grow, fill")); //Row Constraints
createPanels();
pack();
setMinimumSize(getSize()); //Sets minimum size to the preferred size. Remove or change this line if you do not want that to happen
setVisible(true);
}
private void createPanels() {
JPanel locationPanel = new JPanel();
JPanel usagePanel = new JPanel();
JPanel structuralAspectsPanel = new JPanel();
//JLabels for font metrics
JLabel one = new JLabel("This is the first of all labels");
JLabel two = new JLabel("Second Label");
JLabel three = new JLabel("This Label is fairly large and long and pushes the text around");
JLabel four = new JLabel("A label in the usage panel");
JLabel five = new JLabel("And another one and another one and another one");
//Font Metrics
FontMetrics metrics = three.getFontMetrics(three.getFont()); //Take longest label manually or dynamically (You will have to add that code)
int width = metrics.stringWidth(three.getText());
locationPanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
locationPanel.setBorder(BorderFactory.createTitledBorder("Location"));
usagePanel.setLayout(new MigLayout("gap rel 2", "[:" + width + ", grow, center][grow, left]"));
usagePanel.setBorder(BorderFactory.createTitledBorder("Usage"));
locationPanel.add(one);
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(two);
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(three);
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(four);
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(five);
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");
getContentPane().add(locationPanel);
getContentPane().add(usagePanel);
}
public static void main(String[] args) {
new MigLay();
}
}