2016-05-17 100 views
0

我正在製作一個使用大量不同組件的程序,所以我已經開始爲它使用GridBagLayout。它的工作很好,但是當我運行該程序,我得到這樣的:GridBagLayout組件沒有填充空間,因爲它應該

Picture of current layout

應該把最右邊的列表對象直接對抗的其餘對象,並填充屏幕的整個寬度。不知道它爲什麼不是。下面是相關的代碼的短複印件(我把他們都變成了簡單一類,你應該決定來運行它。):

import javax.swing.*; 
import java.awt.*; 
import java.util.*; 

public class Example extends JFrame{ 
    private Character me; 
    public static void main(String[] args){ 
     new Example(); 
    } 

    public Example(){ 
     add(new CharacterDisplay(new Character())); 
     setSize(600,500); 
     setVisible(true); 
    } 

    private class Character{ 
     public ArrayList<String> notes = new ArrayList<String>(); 
     public Character(){ 
      notes.add("A"); 
      notes.add("few"); 
      notes.add("notes"); 
     } 
    } 

    private class CharacterDisplay extends JPanel{ 
     public CharacterDisplay(Character myself){ 
      me = myself; 
      setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 
      JTextArea filler = new JTextArea(); 
      c.gridx = 0; 
      c.weightx = 1; 
      c.weighty = 1; 
      c.gridheight = 11; 
      c.gridwidth = 6; 
      c.fill = GridBagConstraints.BOTH; 
      add(filler,c); 

      c.gridy = 0; 
      c.gridx = 6; 
      c.weightx = 1; 
      c.weighty = 1; 
      c.gridheight = 11; 
      c.gridwidth = 3; 
      c.fill = GridBagConstraints.BOTH; 
      add(new NoteBox(), c); 
     } 
    } 

    private class NoteBox extends JPanel{ 
     private int currentNotes = 0; 
     private JList<String> listContainer; 
     private JTextField addNoteField = new JTextField(); 
     private JButton removalButton = new JButton("Remove Selected Notes"); 
     private JScrollPane listScroll; 
     public NoteBox(){ 
      setLayout(new GridBagLayout()); 
      addNoteField.setText("Add a note here"); 
      addNoteField.addActionListener(e -> { 
       me.notes.add(addNoteField.getText()); 
       repaint();}); 
      String[] model = new String[me.notes.size()]; 
      model = me.notes.toArray(model); 
      listContainer = new JList<String>(model); 
      removalButton.addActionListener(e -> remove(listContainer.getSelectedIndices())); 
      listScroll = new JScrollPane(listContainer); 
      GridBagConstraints c = new GridBagConstraints(); 
      c.fill = GridBagConstraints.BOTH; 
      c.gridwidth = 3; 
      c.weighty = 0; 
      add(addNoteField, c); 
      c.weighty = 1; 
      c.gridy = 1; 
      c.gridheight = 9; 
      add(listScroll, c); 
      c.weighty = 0; 
      c.gridy = 10; 
      add(removalButton, c); 
     } 

     public void remove(int[] indices){ 
      //Go backward to avoid shifting indices of the next ones to remove; 
      for(int i = indices.length - 1; i >= 0; i--){ 
       me.notes.remove(indices[i]); 
      } 
      repaint(); 
     } 

     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      String[] model = new String[me.notes.size()]; 
      model = me.notes.toArray(model); 
      listContainer.setListData(model); 
     } 
    } 
} 

我包括所有的NoteBox類的,因爲它是一個最正常工作和我想通這是最相關的。

+0

不要更新'paintComponent方法中的'listContainer',這會導致其他問題 – MadProgrammer

+0

您是否嘗試過對'listScroll'使用'weightx = 1'? – MadProgrammer

+0

使用weightx = 1完全修正了它。我花了半個多小時的時間仔細查看了試圖找到它的內容,並且每次都完全剔除。我很感激幫助。另外,我會停止更新'paintComponent'中的'listContainer' - 但是出於好奇,是什麼原因導致問題? 此外,感謝您的快速幫助。 –

回答

0

我只需在我的NoteBox內部類中加入weightx = 1,就像MadProgrammer建議的那樣。這完全解決了這個問題,我只是寫這個來正式將答案標記爲已解決。