GridBagConstraints 如何在 Java 中工作

How does GridBagConstraints work in Java

我只是不明白 GridBagConstraints 是如何工作的,因为有时它会变得不可预测。

首先介绍一下我想要的矩阵布局:

[l1][ca] Legend:
[jt][ca] l1 - JLabel1; l2 - JLabel2;
[jt][l2] jt - jTable;  jb - JButton; 
[jt][jb] ca - camera (temporary just JButton);

我实际收到的是:

[l1][ca]
[jt][l2]
[jt][l2]
[jt][jb]
[jt][jb]

我对这个框架的代码:

public static void addToPollFramePane(Container pane){
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;

    JLabel label = new JLabel("Chose one party from the list");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.25;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(label, gbc);

    JTable table = createTable(partiesDoc);
    JScrollPane scrollPane = new JScrollPane(table);
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.75;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 3;
    pane.add(scrollPane, gbc);

    JButton button = new JButton("Camera");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 2;
    pane.add(button, gbc);

    label = new JLabel("Press button");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.5;
    gbc.weighty = 0.25;
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(label, gbc);

    button = new JButton("Vote");
    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 0.50;
    gbc.weighty = 0.25;
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    pane.add(button, gbc);

}


public static void showPollFrame(JFrame rootFrame){
    JFrame pollFrame = new JFrame("Poll");
    pollFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    pollFrame.setResizable(false);
    pollFrame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            rootFrame.setVisible(true);
        }
    });

    Document confDoc = MainFrame.loadDocumnetFromXML("conf.xml");
    conf = new MainFrame.Config(confDoc);
    partiesDoc = MainFrame.loadDocumnetFromXML(conf.pathToParties);
    addToPollFramePane(pollFrame.getContentPane());

    GraphicsEnvironment enviroment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice device = enviroment.getDefaultScreenDevice();
    device.setFullScreenWindow(pollFrame);
    pollFrame.setVisible(true);
}

帧图像:screenshot

如您所见,显示完全错误,我只是不明白为什么...甚至全屏也不是全屏。

请解释我哪里做错了!

我已阅读https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

有些东西因为重量而变得混乱。如果您从第一个标签中删除权重,它会正确显示。也许这是因为不同的列跨越不同的行,因此对行进行加权并不总是完全有意义,或者可能是 Swing 中的错误。下面显示了要注释掉/删除的权重:

JLabel label = new JLabel("Chose one party from the list");
gbc.fill = GridBagConstraints.BOTH;
// gbc.weightx = 0.5;
// gbc.weighty = 0.25;