GridBagConstraints如何在Java中工作

时间:2016-11-26 07:19:43

标签: java swing layout-manager gridbaglayout

我无法理解,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

1 个答案:

答案 0 :(得分:1)

由于重量,有些东西搞砸了。如果从第一个标签中删除重量,则会正确显示。也许这是因为不同的列跨越不同的行,因此称量行不会完全有意义或者可能是Swing中的错误。下面显示了要注释/删除的权重:

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