我对GridBagLayout发生了定位问题: 我尝试在中间(顶部)放置一个标签,但使用我的代码(出于我没有看到的原因),我已经: 使用GridBagLayout定位
我想标签测试是在我的窗口顶部和中心。有人能向我解释这种糟糕的定位原因吗?
我的程序:
public class Accueil extends JFrame {
private JPanel home = new JPanel();
private GridBagConstraints grille = new GridBagConstraints();
private JLabel title = new JLabel("Test");
public Accueil() {
home.setLayout(new GridLayout());
init_grille();
init_title();
this.add(home);
this.setSize(600,600);
this.setTitle("Test One");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void init_grille() {
grille.fill = GridBagConstraints.BOTH;
grille.weightx = 2;
grille.weighty = 5;
grille.ipady=grille.anchor=GridBagConstraints.CENTER;;
}
private void init_title() {
grille.fill = GridBagConstraints.HORIZONTAL;
grille.gridx = 0;
grille.gridy = 0;
home.add(title,grille);
}
public static void main(String [] args) {
new Accueil();
}
}
您没有使用GridBagLayout,而是使用GridLayout。 –
如果你想使用GridLayout,你需要改变你的标签 –