2014-03-26 54 views
0

對不起,另一個「我的代碼在這裏有什麼問題」的問題,但我一直在與這個GridBagOfHurtLayout戰鬥,我不明白什麼可能發生。GridBagLayout對齊列它不應該

我希望做一個UI,像這樣的佈局:

What it should be

的畫圖程序崩潰和所有我得到的是一個縮小的屏幕截圖,遺憾的顆粒感。所以我看到它的方式,我想要一個帶有4列的GridBagLayout,每個頂部的兩個窗格跨越2列,文本字段和大窗格跨越3,搜索按鈕和左下方的窗格跨越1(水平)。這是我的代碼:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
this.setPreferredSize(new Dimension(1080, 720)); 

this.setLayout(new GridBagLayout()); 
GridBagConstraints constraints; 

projectLabel = new JLabel("Projects"); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.NONE; 
constraints.anchor = GridBagConstraints.LINE_START; 
constraints.gridx = 0; 
constraints.gridy = 0; 
constraints.gridwidth = 2; 
constraints.weightx = 0.25; 
constraints.weighty = 0; 
constraints.insets = new Insets(4,4,4,4); 
this.add(projectLabel); 

projectScrollPane = new JScrollPane(); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.gridx = 0; 
constraints.gridy = 1; 
constraints.gridwidth = 2; 
constraints.weightx = 1.0; 
constraints.weighty = 0.5; 
constraints.insets = new Insets(4,4,4,4); 
projectScrollPane.setPreferredSize(new Dimension(540, 240)); 
this.add(projectScrollPane,constraints); 

fieldLabel = new JLabel("Fields"); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.NONE; 
constraints.anchor = GridBagConstraints.LINE_START; 
constraints.gridx = 2; 
constraints.gridy = 0; 
constraints.gridwidth = 2; 
constraints.weightx = 1.0; 
constraints.weighty = 0; 
constraints.insets = new Insets(4,4,4,4); 
this.add(fieldLabel); 

fieldScrollPane = new JScrollPane(); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.gridx = 2; 
constraints.gridy = 1; 
constraints.gridwidth = 2; 
constraints.weightx = 1.0; 
constraints.weighty = 0.5; 
constraints.insets = new Insets(4,4,4,4); 
fieldScrollPane.setPreferredSize(new Dimension(540, 240)); 
this.add(fieldScrollPane,constraints); 

searchTextField = new JTextField(); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.HORIZONTAL; 
constraints.gridx = 0; 
constraints.gridy = 2; 
constraints.gridwidth = 3; 
constraints.weightx = 2.0; 
constraints.weighty = 0.0; 
constraints.insets = new Insets(4,4,4,4); 
searchTextField.setPreferredSize(new Dimension(1000,30)); 
this.add(searchTextField,constraints); 

searchButton = new JButton("Search"); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.NONE; 
constraints.gridx = 3; 
constraints.gridy = 2; 
constraints.weightx = 0.0; 
constraints.weighty = 0.0; 
constraints.insets = new Insets(4,4,4,4); 
searchButton.setSize(new Dimension(80,30)); 
this.add(searchButton,constraints); 

imageScrollPane = new JScrollPane(); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.gridx = 0; 
constraints.gridy = 3; 
constraints.weightx = 0.25; 
constraints.weighty = 1.0; 
constraints.insets = new Insets(4,4,4,4); 
imageScrollPane.setPreferredSize(new Dimension(360,810)); 
this.add(imageScrollPane,constraints); 

imagePane = new JEditorPane(); 
constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.gridx = 1; 
constraints.gridy = 3; 
constraints.gridwidth = 3; 
constraints.weightx = 1.0; 
constraints.weighty = 1.0; 
constraints.insets = new Insets(4,4,4,4); 
imagePane.setPreferredSize(new Dimension(720,810)); 
this.add(imagePane,constraints); 

this.pack(); 

標籤不在原始佈局中。每個ScrollPanes最終都會包含一個JList,但現在我只是試圖讓空白的佈局看起來不錯,而不是。我目前得到的是這個。

What I actually get

文本字段不能被說服佔用3列,也許第一滾動窗格佔用3.我試圖改變它的每一個方式,我能想到的,但什麼也沒有幫助。我可能會放棄這一點,並使用BoxLayout進行嵌套面板,但我想知道爲什麼這不起作用,因爲我認爲我做的都是正確的。任何見解?提前致謝。

回答

2

Swing在這裏感到困惑,因爲它並沒有真正引用列應該在哪裏。

在這種情況下,最好的解決方案是創建至少一個包含所有列的行,並在這些列上定義weightx。

此外,JEditorPane在gridBagLayout中不能正常工作。您應該使用JScrollPane(或其他)並將JEditorPane放在此窗格中。

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setPreferredSize(new Dimension(1080, 720)); 

    this.setLayout(new GridBagLayout()); 
    GridBagConstraints constraints; 

    JLabel a = new JLabel(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    constraints.weightx=0.25; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(a,constraints); 

    JLabel b = new JLabel(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 1; 
    constraints.gridy = 0; 
    constraints.weightx=0.25; 
    this.add(b,constraints); 

    JLabel c = new JLabel(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 2; 
    constraints.gridy = 0; 
    constraints.weightx=0.5; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(c,constraints); 

    JLabel d = new JLabel(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 3; 
    constraints.gridy = 0; 
    constraints.weightx=0; 
    this.add(d,constraints); 

    JLabel p1 = new JLabel("Project - 1"); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 0; 
    constraints.gridy = 0; 
    constraints.gridwidth=2; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(p1,constraints); 

    JLabel p2 = new JLabel("Project - 2"); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 2; 
    constraints.gridy = 0; 
    constraints.gridwidth=2; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(p2,constraints); 


    JScrollPane projectScrollPane = new JScrollPane(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 0; 
    constraints.gridy = 1; 
    constraints.gridwidth = 2; 
    constraints.weighty=1; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(projectScrollPane,constraints); 

    JScrollPane fieldScrollPane = new JScrollPane(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 2; 
    constraints.gridy = 1; 
    constraints.gridwidth = 2; 
    constraints.insets = new Insets(4,4,4,4); 
    fieldScrollPane.setPreferredSize(new Dimension(540, 240)); 
    this.add(fieldScrollPane,constraints); 

    JTextField searchTextField = new JTextField(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.HORIZONTAL; 
    constraints.gridx = 0; 
    constraints.gridy = 2; 
    constraints.gridwidth = 3; 
    constraints.insets = new Insets(4,4,4,4); 
    searchTextField.setPreferredSize(new Dimension(1000,30)); 
    this.add(searchTextField,constraints); 

    JButton searchButton = new JButton("Search"); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.NONE; 
    constraints.anchor = GridBagConstraints.LINE_END; 
    constraints.gridx = 3; 
    constraints.gridy = 2; 
    constraints.insets = new Insets(4,4,4,4); 
    searchButton.setSize(new Dimension(80,30)); 
    this.add(searchButton,constraints); 

    JScrollPane imageScrollPane = new JScrollPane(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 0; 
    constraints.gridy = 3; 
    constraints.insets = new Insets(4,4,4,4); 
    constraints.weighty=1; 
    this.add(imageScrollPane,constraints); 

    JScrollPane imagePane = new JScrollPane(); 
    constraints = new GridBagConstraints(); 
    constraints.fill = GridBagConstraints.BOTH; 
    constraints.gridx = 1; 
    constraints.gridy = 3; 
    constraints.gridwidth = 3; 
    constraints.weighty=1; 
    constraints.insets = new Insets(4,4,4,4); 
    this.add(imagePane,constraints); 

    this.pack(); 
+0

謝謝,這解決了這個問題。我無法讓Project JLabels工作(代碼中有一個錯字,順便說一下,它們不應該是相同的)。我最終把他們帶走了,因爲他們大多在那裏試圖去做那些看不見的標籤。我也最終取消了他們的插頁,並且我將第3欄的重量改爲0.45,因爲Search JButton是不平衡的東西。總之,不可見的行列固定列和所有其他權重約束是不需要的。我不明白爲什麼這些項目標籤是用來填滿單元格的,但他們已經不在了。謝謝! – krs013