首页 文章

GridBagLayout Jbutton位置

提问于
浏览
-2

这就是我所拥有的:

enter image description here

这就是我想要的:

enter image description here

这是我的一段代码

container.add(conteneur2 , BorderLayout.EAST);


conteneur2.setBackground(Color.ORANGE);


conteneur2.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

JButton Bouton= new JButton("New remote site");

// Bouton 1 

gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.LINE_START;

conteneur2.add(bouton,gbc);

所以有什么问题?

1 回答

  • 4

    你需要设置锚 NORTH

    gbc.anchor = GridBagConstraints.NORTH;
    

    并且y权重为1意味着所有自由空间都在y方向上分布 .

    gbc.weighty = 1;
    

相关问题