2016-01-25 19 views
1

我做了一个JPANE p,放在“东”侧。然后,我将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

+0

你可能需要设置'weighty'到一些东西,是不为0邮报[MCVE(HTTP: //stackoverflow.com/help/mcve)。一定要将你的代码复制粘贴到一个*新的项目中*,并确保它在发布之前编译并运行。 – user1803551

回答

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); 
+0

没帮忙:/// – szufi