2016-11-26 41 views
0

我只是無法理解,GridBagConstraints是如何工作的,因爲它有時會變得不可預測。GridBagConstraints如何在Java中工作

所有我將介紹佈局首先我要爲矩陣:

[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

+0

據我所知,'GridbagLayout'只佈局它的元素與從0索引一個3x3格 - 2。所以不應該有'gbc.gridy = 3;' – QBrute

回答

1

某樣東西弄亂了,由於權重。如果您從第一個標籤中移除重物,則會顯示正確。也許這是因爲不同的列跨越不同的行,因此稱重行並不總是完全意義上的,或者可能是Swing中的錯誤。下圖爲註釋掉其權重/刪除:

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