Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout并没有占据所有的窗口位置_Java_Swing_Awt_Layout Manager_Gridbaglayout - Fatal编程技术网

Java GridBagLayout并没有占据所有的窗口位置

Java GridBagLayout并没有占据所有的窗口位置,java,swing,awt,layout-manager,gridbaglayout,Java,Swing,Awt,Layout Manager,Gridbaglayout,嗨,我有一个非常简单的问题,为什么我的gridBagLayout没有占据我窗口上所有有争议的位置 代码如下: public class FenConnection extends JFrame { private static final long serialVersionUID = 4799549157439445680L; private String adresse; private int port; private JLabel infoLabel

嗨,我有一个非常简单的问题,为什么我的gridBagLayout没有占据我窗口上所有有争议的位置

代码如下:

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);
    }
}
另一件奇怪的事情是,如果我添加这个.pack;在我的构造器的末尾,它创建了一个非常大且有缺陷的窗口。。。我不知道为什么


我已多次使用此布局,但从未查看过该问题。

您让weightx和weighty属性默认为0,这意味着它们将无法填充可用空间

尝试为权重x和权重y添加非零

GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1

您让weightx和weighty属性默认为0,这意味着它们将不会填充可用空间

尝试为权重x和权重y添加非零

GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = 1

@何安德烈·霍姆普森。。。是的,对不起,可能很难测试。。。我必须把整个班级放在一起吗?用整个代码编辑class@AndrewThompson呵。。。是的,对不起,可能很难测试。。。我必须把整个课堂都放进去吗?用整个代码编辑课堂谢谢,我忘了这个^^谢谢,我忘了这个^^