我试图在代码中尽可能地简化这一点。我已经使用 GridBagLayout 很长时间了,出于某种原因,我从未遇到过这种情况。
JDialog dialog = new JDialog();
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setResizeable(true);
JPanel guiHolder = new JPanel();
guiHolder.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
guiHolder.add(new JLabel("my test"), gbc);
dialog.add(guiHolder);
dialog.setSize(new Dimension(320, 240);
dialog.setSize(true);
JLabel 最终在屏幕中心呈正方形。我怎样才能让它到达顶部?我看过如何使用 GridBagLayout。我被难住了……