【问题标题】:GridBagLayout don't take all the window placeGridBagLayout 不占所有窗口位置
【发布时间】:2015-04-11 02:13:39
【问题描述】:

您好,有一个非常简单的问题,为什么我的 gridBagLayout 没有占据我窗口上所有的 disponible 位置?

这里是代码:

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();,它会生成一个非常大且错误的窗口...我不知道为什么。

这个Layout我用过很多次了,一直没看到问题。

【问题讨论】:

  • @AndrewThompson Ho...是的,抱歉,这可能很难测试...我必须全班上课吗?
  • 用整个代码类编辑

标签: java swing awt layout-manager gridbaglayout


【解决方案1】:

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

尝试为 weightx 和 weighty 添加非零

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

【讨论】:

  • 谢谢,我忘了这个^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多