2013-08-29 33 views
1

我已經看到關於此主題的其他帖子,但他們發現的解決方案並不適用於我。我正在設置加權值,並使用c.fill = GridBagConstraints.BOTH約束。JScrollPane無法正確伸展​​GridBagLayout中的水平距離

我包含了我的全部GUI代碼,以防萬一我的錯誤來自GridBagLayout以外的其他東西。

我希望右側的可滾動文本塊擴展GUI內的剩餘空間,並且我已經設置了所有應該歸因於該變量的變量,但仍然無法正常工作。我究竟做錯了什麼?

我的結果:

GridBagLayout result

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

public class TestCode extends JFrame { 
    JTextArea textArea = new JTextArea(); 
    JComboBox <String> typeComboBox; 
    JTextField searchField; 
    JTextField fileField; 

    public TestCode(){ 

     setTitle ("GUI Test"); 
     setSize (600, 300); 
     setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     setVisible (true); 

     JScrollPane scrollPane = new JScrollPane(textArea); 

     JButton readButton = new JButton("Read File"); 
     JButton displayButton = new JButton("Display"); 
     JButton searchButton = new JButton("Search"); 


     searchField = new JTextField(10); 
     fileField = new JTextField(15); 

     typeComboBox = new JComboBox <String>(); 
     typeComboBox.addItem("Index"); 
     typeComboBox.addItem("Type"); 
     typeComboBox.addItem("Name"); 




     JPanel container = new JPanel(); 
      container.setLayout(new GridBagLayout()); 
      container.setPreferredSize(new Dimension(250, 100)); 
     JPanel filePanel = new JPanel(); 
      filePanel.setLayout(new BoxLayout(filePanel, BoxLayout.Y_AXIS)); 
      filePanel.add(new JLabel("Source file", SwingConstants.LEFT)); 
       JPanel filePanelTop = new JPanel(); 
        filePanelTop.setLayout(new FlowLayout(FlowLayout.LEFT)); 
        filePanelTop.add(fileField); 
       JPanel filePanelBottom = new JPanel(); 
        filePanelBottom.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
        filePanelBottom.add(readButton); 
        filePanelBottom.add(displayButton); 
      filePanel.add(filePanelTop); 
      filePanel.add(filePanelBottom); 
      filePanel.setMaximumSize(filePanel.getPreferredSize()); 
      filePanel.setBorder(BorderFactory.createTitledBorder("Import File")); 
     JPanel searchPanel = new JPanel(); 
      searchPanel.setLayout(new BoxLayout(searchPanel, BoxLayout.Y_AXIS)); 
      searchPanel.add(new JLabel("Search target", SwingConstants.LEFT)); 
       JPanel searchPanelTop = new JPanel(); 
        searchPanelTop.setLayout(new FlowLayout(FlowLayout.LEFT)); 
        searchPanelTop.add(searchField); 
        searchPanelTop.add(typeComboBox); 
      searchPanel.add(searchPanelTop); 
      searchPanel.add(searchButton); 
      searchPanel.setMaximumSize(searchPanel.getPreferredSize()); 
      searchPanel.setBorder(BorderFactory.createTitledBorder("Search Objects")); 

      GridBagConstraints c = new GridBagConstraints(); 
      c.gridx = 0; 
      c.gridy = 0; 
      container.add(filePanel, c); 
      c.gridx = 0; 
      c.gridy = 1; 
      container.add(searchPanel, c); 
      c.gridx = 1; 
      c.gridy = 0; 
      c.weightx = 1.0; 
      c.weighty = 1.0; 
      c.gridwidth = GridBagConstraints.REMAINDER; 
      c.gridheight = GridBagConstraints.REMAINDER; 
      c.fill = GridBagConstraints.BOTH; 
      c.anchor = GridBagConstraints.NORTHWEST; 
      container.add(scrollPane, c); 

     add(container, BorderLayout.WEST); 

     validate(); 

    } // end method toString 

    public static void main(String[] args){ 
     TestCode run = new TestCode(); 
    } 
} // end class Treasure 
+2

您在添加組件之前設置可見*,並且不會調用包。你怎麼能期望這個工作?不要在任何地方調用setSize(...)。讓組件和佈局管理器適當調整自己的大小。 –

+0

@HovercraftFullOfEels這只是把一切都搞砸了。 ScrollPanel沒有「大小」,因爲它最初沒有內容,所以不設置大小和調用pack()將它搗碎成什麼都沒有。而且它也將其他面板搗毀爲無,我想我只需要回到繪圖板。 – leigero

+0

它沒有任何尺寸,因爲您沒有給它任何尺寸。您應該考慮在創建JTextArea的可見列和行時設置它們:「新的JTextArea(rows,cols)',然後讓JScrollPane **與所有其他組件**自行調整大小。 –

回答

7
//add(container, BorderLayout.WEST); 
add(container); 

西contrains組件以它們的優選寬度。默認情況下,CENTER允許組件擴展以填充可用空間。

此外,您的代碼的主要結構是錯誤的。您應該先將所有組件添加到幀中,然後調用:

frame.pack(); 
frame.setVisible(true); 

然後不需要驗證()。

+0

哇。謝謝。有時最煩人的問題有最令人尷尬的簡單解決方案。 – leigero

+0

frame.pack()設法將所有東西都搞砸了。它「包裝」了所有元素,並將它們壓縮成完全不合需要的尺寸,然後忽略最初宣佈的窗口大小。 – leigero

+0

@leigero,那是因爲你不應該在任何組件上使用'setPreferredSize()'。每個組件都有自己的首選大小。你在容器上使用這個事實意味着所有的子組件都不能正確地裝入容器中。 GridBagLayout會將組件縮小到最小尺寸。學會正確使用佈局管理器。正如我在你上一篇文章中告訴你的,你不應該使用任何setXXX方法。 – camickr