2016-11-26 1 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에서 인덱스와 3 × 3 격자 요소를 레이아웃 - 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; 
관련 문제