2014-06-17 51 views
1

我正在嘗試爲我已完成的程序編寫GUI,該程序將各種文本文件編譯爲完整的筆記本。這個GUI的一個特點是它有多個文件瀏覽textareas和滾動窗格中的按鈕,還有更多可以用加號或減號按鈕添加或刪除。Java Swing文本字段高度

問題是,textarea默認佔用剩餘空間,即使我設置了首選大小。有沒有什麼辦法可以讓它和按鈕一樣高或至少與它接近?

這是我的GUI看起來像現在。

這裏是我的代碼。

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

public class GUI { 

    public static void main(String[] args) { 

     JFrame frame = new JFrame("Notebook Builder"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPanel = new JPanel(); 
     contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS)); 
     contentPanel.setPreferredSize(new Dimension(860, 500)); 

     JPanel leftPanel = new JPanel(); 
     leftPanel.setLayout(new BorderLayout()); 
     leftPanel.setPreferredSize(new Dimension(480, 500)); 

     JScrollPane rightPanel = new JScrollPane(); 
     rightPanel.setPreferredSize(new Dimension(480, 500)); 

     frame.getContentPane().add(contentPanel); 

     contentPanel.add(leftPanel); 
     contentPanel.add(rightPanel); 

     JPanel headerPanel = new JPanel(); 
     JLabel headerLabel = new JLabel("Directories"); 
     headerLabel.setFont(new Font("serif", Font.BOLD, 32)); 
     headerPanel.add(headerLabel); 

     JPanel directoryBrowserPanel = new JPanel(); 
     directoryBrowserPanel.setLayout(new BoxLayout(directoryBrowserPanel, BoxLayout.Y_AXIS)); 
     JScrollPane directoryBrowserPanelView = new JScrollPane(directoryBrowserPanel); 

     JPanel addOrRemoveButtonPanel = new JPanel(); 
     addOrRemoveButtonPanel.setLayout(new BoxLayout(addOrRemoveButtonPanel, BoxLayout.X_AXIS)); 

     JButton remove = new JButton("-"); 
     JButton add = new JButton("+"); 

     JPanel directoryBrowserFieldPanel = new JPanel(); 
     directoryBrowserFieldPanel.setLayout(new BoxLayout(directoryBrowserFieldPanel, BoxLayout.X_AXIS)); 

     JTextField filePathField = new JTextField(); 

     JButton fileChooseButton = new JButton("Browse"); 

     directoryBrowserFieldPanel.add(filePathField); 
     directoryBrowserFieldPanel.add(fileChooseButton); 
     addOrRemoveButtonPanel.add(remove); 
     addOrRemoveButtonPanel.add(Box.createRigidArea(new Dimension(80, 0))); 
     addOrRemoveButtonPanel.add(add); 
     directoryBrowserPanel.add(directoryBrowserFieldPanel); 
     directoryBrowserPanel.add(addOrRemoveButtonPanel); 

     JPanel buildButtonPanel = new JPanel(); 
     JButton buildButton = new JButton("Build Notebook"); 
     buildButton.setFont(new Font("serif", Font.PLAIN ,12)); 
     buildButtonPanel.add(BorderLayout.CENTER, buildButton); 

     leftPanel.add(BorderLayout.NORTH, headerPanel); 
     leftPanel.add(BorderLayout.CENTER, directoryBrowserPanelView); 
     leftPanel.add(BorderLayout.SOUTH, buildButtonPanel); 

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

對不起,如果這是一個簡單的問題,這是我第一次偏離Java中的控制檯程序。

非常感謝。

+5

開始採取看看[我應避免使用一套(首選|最大|最小)?在的Java Swing大小的方法( http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi),然後考慮使用'LayoutManager',然後'BoxLayout',類似例如FlowLayout或者GridBagLayout,例如 – MadProgrammer

+0

FlowLayout工作。謝謝。我猜這只是因爲佈局經理處理不同經理的高度?我也沒有使用設置的首選高度。 – user1472065

+2

如果它適合你,你可以回答你自己的問題,以幫助未來的訪問者到這個網站。 –

回答

1

使用GridbagLayout而不是BoxLayout。我認爲它更有用。示例代碼如下

int xPos=0; 
    int yPos=0; 
    directoryBrowserFieldPanel.setLayout(new GridBagLayout()); 
    directoryBrowserFieldPanel.add(filePathField, new GridBagConstraints(xPos++, yPos, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0)); 
    directoryBrowserFieldPanel.add(fileChooseButton, new GridBagConstraints(xPos++, yPos, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0)); 

ANA請看看到A Visual Guide to Layout Managers