代码之家  ›  专栏  ›  技术社区  ›  Thomas Athanasiou

如何用JPanel填充JFrame窗口?

  •  0
  • Thomas Athanasiou  · 技术社区  · 6 年前

    我试图设置一个JPanel来填充JFrame的整个窗口。JPanel的布局是GridBagLayout。如果我将其更改为BorderLayout,它就会工作。但我需要GridBagLayout,这样JPanel就会显示在窗口的中心。 如何用JPanel填充JFrame的整个窗口(就像BorderLayout一样)?

    1 回复  |  直到 6 年前
        1
  •  2
  •   MadProgrammer    6 年前

    首先,确保阅读 How to Use GridBagLayout

    接下来,确保 the JavaDocs for GridBagConstraints 打开并可用

    这两个基本参考资料将为您提供99%的信息,您可以回答大多数问题。

    没有更多的背景,就像。。。

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    

    将实现您想要的结果

    请记住,您并不是只局限于一个布局管理器,您可以使用多个容器,所有容器都使用不同的布局管理器来实现所需的结果