无法在开始时移动组件

时间:2016-01-25 13:37:54

标签: java layout gridbaglayout

我做了一个JPane p,把它放在“EAST”一边。然后我将GridBagLayout()设置为我的窗格布局。 当我添加按钮时,它们位于中心位置,但我希望在左上角看到它们。

这是我添加臀部的方式。

GridBagConstraints gbc = new GridBagConstraints();
this.b1 = new JButton("Button 1");
gbc.gridx = 0;
gbc.gridy = 0;
p1.add(b1, gbc);

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用GridBagConstraints例如:

pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

button = new JButton("Button 1");

if (shouldWeightX) {
    c.weightx = 0.5;
}

c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);