GridBagLayout 不要占据所有 window 的地方
GridBagLayout don't take all the window place
你好,有一个非常简单的问题,为什么我的 gridBagLayout 没有占据我 window 上所有不负责任的地方?
这里是代码:
public class FenConnection extends JFrame
{
private static final long serialVersionUID = 4799549157439445680L;
private String adresse;
private int port;
private JLabel infoLabel;
private JTextField tf_adresse;
private JButton boutonConnexion;
private JTextField tf_port;
public FenConnection()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(400, 150);
this.setTitle("Felix - Connexion");
adresse = Felix.CONFIGURATION.getString("ADRESSE_CHAT");
port = Integer.parseInt(Felix.CONFIGURATION.getString("PORT_CHAT"));
construireFormulaire();
}
private void construireFormulaire()
{
Container pane = this.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//Construction des labels
c.gridx = 0;
c.gridy = 0;
pane.add(new JLabel("Adresse IP :"), c);
c.gridy = 1;
pane.add(new JLabel("Port :"), c);
c.gridy = 2;
c.gridwidth = 2;
infoLabel = new JLabel("Saisir l'adresse et le port du serveur chat");
pane.add(infoLabel, c);
//Creation du bouton de connection
c.gridy = 3;
c.gridwidth = 2;
boutonConnexion = new JButton("Connexion");
pane.add(boutonConnexion, c);
//Construction des champs de texts
c.gridx = 1;
c.gridy = 0;
tf_adresse = new JTextField(adresse);
pane.add(tf_adresse, c);
c.gridy = 1;
tf_port = new JTextField(port);
pane.add(tf_port, c);
}
}
另一件奇怪的事情是,如果我在构造函数的末尾添加 this.pack();
,它会产生一个非常大且有问题的 windows...我不明白为什么。
这个Layout我用过很多次了,一直没看到问题
您让 weightx 和 weighty 属性默认为 0,这意味着它们不会填充可用 space。
尝试为 weightx 和 weighty 添加非零值
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1
你好,有一个非常简单的问题,为什么我的 gridBagLayout 没有占据我 window 上所有不负责任的地方?
这里是代码:
public class FenConnection extends JFrame
{
private static final long serialVersionUID = 4799549157439445680L;
private String adresse;
private int port;
private JLabel infoLabel;
private JTextField tf_adresse;
private JButton boutonConnexion;
private JTextField tf_port;
public FenConnection()
{
super();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(400, 150);
this.setTitle("Felix - Connexion");
adresse = Felix.CONFIGURATION.getString("ADRESSE_CHAT");
port = Integer.parseInt(Felix.CONFIGURATION.getString("PORT_CHAT"));
construireFormulaire();
}
private void construireFormulaire()
{
Container pane = this.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
//Construction des labels
c.gridx = 0;
c.gridy = 0;
pane.add(new JLabel("Adresse IP :"), c);
c.gridy = 1;
pane.add(new JLabel("Port :"), c);
c.gridy = 2;
c.gridwidth = 2;
infoLabel = new JLabel("Saisir l'adresse et le port du serveur chat");
pane.add(infoLabel, c);
//Creation du bouton de connection
c.gridy = 3;
c.gridwidth = 2;
boutonConnexion = new JButton("Connexion");
pane.add(boutonConnexion, c);
//Construction des champs de texts
c.gridx = 1;
c.gridy = 0;
tf_adresse = new JTextField(adresse);
pane.add(tf_adresse, c);
c.gridy = 1;
tf_port = new JTextField(port);
pane.add(tf_port, c);
}
}
另一件奇怪的事情是,如果我在构造函数的末尾添加 this.pack();
,它会产生一个非常大且有问题的 windows...我不明白为什么。
这个Layout我用过很多次了,一直没看到问题
您让 weightx 和 weighty 属性默认为 0,这意味着它们不会填充可用 space。
尝试为 weightx 和 weighty 添加非零值
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1