问题描述
这两个约束有什么区别?
What is the difference between these two constraints?
来自文档:
PUSH - 使组件所在的行和/或列以权重增长
GROW - 设置组件相对于同一单元格中的其他组件的增长程度。
因此,主要思想是缩小组件内部和外部的大小?
So, the main idea is to shrink size inside and outside of component?
推荐答案
重要的是要理解 fill
,(列,行)增长
,推送
与
一起工作)增长
来定义布局。 (有两种不同的增长
约束可以执行不同的操作。)
It is important to understand that fill
, (column, row) grow
, push
work in conjunctionwith the (component) grow
to define the layout. (There are two different grow
constraints that do different things.)
MigLayout
是一个基于网格的经理。 (更准确地说,它最重要的模式是。)
有两个步骤需要完成:
MigLayout
is a grid-based manager. (More precisely, its most important mode is.)There are two steps that need to be done:
- 定义了多少空格网格的列和行
- 定义组件单元格中占用的空间大小
这是 fill
,(列,行)增长
,推送
和(组件)增长
约束
帮助实现。前三个定义了网格列和行的增长,
最后一个定义了组件如何在其
分配区域上扩展,例如:放置它的单元格。注意填充或增长是在布局中占据
空白空间的趋势或渴望。列,行或
组件未占用的窗口区域用空格填充。
This is what fill
, (column, row) grow
, push
and (component) grow
constraintshelp achieve. The first three define the growth of the grid's columns and rows,the last one defines how the component will spread over itsalotted area, e.g. cell(s) that it is placed in. Note that filling or growing is the tendency or eagerness to occupyempty space in the layout. The window area that is not taken by columns, rows, orcomponents is filled with empty space.
push
和(列,行)增长
获取可选的权重
参数。
定义了列或行相对于其他列
和行的增长程度。 fill
约束均匀分配权重
。
The push
and the (column, row) grow
take optional weight
parameter. Itdefines how keen the column or row should grow in relation to other columnsand rows. The fill
constraint distributes the weight
evenly.
( push
约束可用于在不同的上下文中使缺口变得贪婪。)
(The push
constraint may be used to make gaps greedy in different context.)
我提供了三个澄清用法的示例这些限制。
I provide three examples that clarify usage of these constraints.
填充
填充
约束会影响网格的所有单元格。它们占据了所有可用空间。
(组件)增长
约束指定组件在其单元格中的传播方式。
The fill
constraint affects all cells of the grid. They occupy all available space.The (component) grow
constraint specifies how the components spread in their cells.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class MigLayoutFillEx extends JFrame {
public MigLayoutFillEx() {
initUI();
setSize(300, 250);
setTitle("Fill");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("fill"));
pnl.add(getLabel("Area 1"), "cell 0 0, growx");
pnl.add(getLabel("Area 2"), "cell 0 1, grow");
pnl.add(getLabel("Area 3"), "cell 1 0 1 2, grow");
add(pnl);
}
private JLabel getLabel(String text) {
JLabel label = new JLabel(text, JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder());
return label;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutFillEx ex = new MigLayoutFillEx();
ex.setVisible(true);
}
});
}
}
在示例中,我们有三个标签。
In the example we have three labels.
JPanel pnl = new JPanel(new MigLayout("fill"));
fill
是布局约束;它会影响所有单元格。
The fill
is a layout constraint; it affects all cells.
pnl.add(getLabel("Area 1"), "cell 0 0, growx");
pnl.add(getLabel("Area 2"), "cell 0 1, grow");
pnl.add(getLabel("Area 3"), "cell 1 0 1 2, grow");
现在我们定义组件如何填充
布局管理器所提供的区域。
区域1标签水平填充其分配区域,其他
两个标签填充两个维度的分配区域。
Now we define how the components fill their areas alotted by thelayout manager.The Area 1 label fills its alotted area horizontally, the othertwo labels fill the alotted area in both dimensions.
列,行增长
(列,行)增长
约束会影响特定列
或行中的所有单元格。
The (column, row) grow
constraint affects all cells in particular columnor row.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutGrowEx extends JFrame {
public MigLayoutGrowEx() {
initUI();
setSize(300, 250);
setTitle("Grow constraint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("wrap", "[grow]", "[][grow]"));
JTextField field = new JTextField(10);
JTextArea area = new JTextArea(10, 10);
pnl.add(field, "growx");
pnl.add(new JScrollPane(area), "grow");
add(pnl);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutGrowEx ex = new MigLayoutGrowEx();
ex.setVisible(true);
}
});
}
}
在示例中,我们有两个组件。预期文本字段将水平增长,水平和垂直方向都为
文本区域。
In the example we have two components. The text field is expected to grow horizontally,the text area both horizontally and vertically.
JPanel pnl = new JPanel(new MigLayout("wrap", "[grow]", "[][grow]"));
在这一行中,我们指定
网格的第一列和第二行增长。
In this line we specify that the first colum and the second row of thegrid grow.
pnl.add(field, "growx");
pnl.add(new JScrollPane(area), "grow");
现在我们定义文本字段水平填充其分配区域,而
文本区域填充整个区域。
Now we define that the text field fills its alotted area horizontally whilethe text area fills the whole alotted area.
推送
推送
约束基本上与(列,行)增长
相同。
的区别在于 push
是在 add()
方法中指定的。
The push
constraint is esentially the same as the (column, row) grow
. Thedifference is that push
is specified at the add()
method.
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class MigLayoutPushEx extends JFrame {
public MigLayoutPushEx() {
initUI();
setSize(300, 250);
setTitle("Push constraint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void initUI() {
JPanel pnl = new JPanel(new MigLayout("wrap"));
JTextField field = new JTextField(10);
JTextArea area = new JTextArea(10, 10);
pnl.add(field, "pushx, growx");
pnl.add(new JScrollPane(area), "push, grow");
add(pnl);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MigLayoutPushEx ex = new MigLayoutPushEx();
ex.setVisible(true);
}
});
}
}
示例与前一个示例相同。
The example is the same as the previous one.
JPanel pnl = new JPanel(new MigLayout("wrap"));
此处未指定增长。
pnl.add(field, "pushx, growx");
pnl.add(new JScrollPane(area), "push, grow");
使用 add()$中的组件约束来指定所有内容c $ c>
方法。
Everything is specified using components' constraints within the add()
methods.
这篇关于MigLayout推动VS增长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!