且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Java Swing的左上角开始GridBagLayout

更新时间:2023-10-29 08:34:46

您需要使用 GridBagConstraints '属性。这应该适合你:

You need to use your GridBagConstraints' anchor property. This should do it for you:

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
frame.add(panel, gbc);

我不保证你不必设置约束对象的其他属性来获取你想要的布局。特别是,您可能需要将 widthx widthy 设置为 1 以便面板占用给它的所有可用空间。

I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set widthx and widthy to be 1 so that the panel takes up all of the available space given to it.