在调用 add() 之前设置约束两次
Setting the constraint twice before calling add()
我正在使用来自 Oracle 的教程在 swing 中学习 GridBagLayout。
众所周知,我们可以在被1 component.Here使用后重新设置约束我注意到作者在调用方法add()之前再次设置了约束。
public class GridBagLayoutDemo {
//Here I have no idea why these 3 lines for
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
//1)why not just ignore the above declaration and just type
//pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); ?
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//same go to here
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//2)the author set the constraint in above line,then author set it again in below line
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
//here it is
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
谁能回答我的两个问题?
我认为第二个布局方向:c.fill = GridBagConstraints.HORIZONTAL;
是一个错误,第一个是有条件的,取决于之前设置的值"shouldFill"。所以我不同意这个变量什么都不做。
我正在使用来自 Oracle 的教程在 swing 中学习 GridBagLayout。
众所周知,我们可以在被1 component.Here使用后重新设置约束我注意到作者在调用方法add()之前再次设置了约束。
public class GridBagLayoutDemo {
//Here I have no idea why these 3 lines for
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
//1)why not just ignore the above declaration and just type
//pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); ?
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//same go to here
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
//2)the author set the constraint in above line,then author set it again in below line
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
//here it is
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
谁能回答我的两个问题?
我认为第二个布局方向:c.fill = GridBagConstraints.HORIZONTAL;
是一个错误,第一个是有条件的,取决于之前设置的值"shouldFill"。所以我不同意这个变量什么都不做。