GridBagLayout 不会为每一行更改
GridBagLayout not changing for each row
我希望第一行的三个组件之间的权重分别为 1、0.5、0.5。所以第一个组件应该是其他组件的两倍。在第二行中,我希望所有三个组件的权重均为 0.5(相同长度)。 GridBagLayout
表现得很奇怪。
public class Main extends JFrame{
public Main() {
super();
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label = new JLabel("Label 1");
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 0;
label.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label, c);
JLabel label2= new JLabel("Label 2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
label2.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label2, c);
JLabel label3= new JLabel("Label 3");
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
label3.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label3, c);
JLabel label4 = new JLabel("1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
label4.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label4, c);
JLabel label5= new JLabel("2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
label5.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label5, c);
JLabel label6= new JLabel("3");
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
label6.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label6, c);
this.setSize(900,900);
this.setVisible(true);
}
public static void main(String[] args){
new Main();
}
}
I want the first row of three components to be spaced out with weights of 1,0.5,0.5.
"weights"仅在调整框架大小时使用。也就是说,组件最初以其首选大小显示。然后,当您增加框架中的 space 时,额外的 space 会按照您指定的比例进行分配。
如果您想始终保持该比例,那么您还需要创建具有该比例的首选尺寸的组件。
或者您可以使用 Relative Layout。它旨在避免此问题并允许您为每个组件指定相对大小。您显然需要为每一行创建多个面板。
我希望第一行的三个组件之间的权重分别为 1、0.5、0.5。所以第一个组件应该是其他组件的两倍。在第二行中,我希望所有三个组件的权重均为 0.5(相同长度)。 GridBagLayout
表现得很奇怪。
public class Main extends JFrame{
public Main() {
super();
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label = new JLabel("Label 1");
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 0;
label.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label, c);
JLabel label2= new JLabel("Label 2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
label2.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label2, c);
JLabel label3= new JLabel("Label 3");
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
label3.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label3, c);
JLabel label4 = new JLabel("1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
label4.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label4, c);
JLabel label5= new JLabel("2");
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
label5.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label5, c);
JLabel label6= new JLabel("3");
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
label6.setBorder(BorderFactory.createLineBorder(Color.red));
this.add(label6, c);
this.setSize(900,900);
this.setVisible(true);
}
public static void main(String[] args){
new Main();
}
}
I want the first row of three components to be spaced out with weights of 1,0.5,0.5.
"weights"仅在调整框架大小时使用。也就是说,组件最初以其首选大小显示。然后,当您增加框架中的 space 时,额外的 space 会按照您指定的比例进行分配。
如果您想始终保持该比例,那么您还需要创建具有该比例的首选尺寸的组件。
或者您可以使用 Relative Layout。它旨在避免此问题并允许您为每个组件指定相对大小。您显然需要为每一行创建多个面板。