2016-11-26 4 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; 
関連する問題