2014-02-16 40 views
0

我想要做的事情很簡單,但我找不到解決方案。Resize JTextArea

我有一個JTextArea添加到一個JPanel,我想調整大小時調整JPanel的JTexArea的大小。當我調整JFrame的大小時,我能夠調整JPanel的大小,但它對於JTextArea看起來並不一樣。有任何想法嗎?這是我使用的代碼。

public HelpPanel(Map<ComponentType, String> compMap) { 
    super(); 
    this.componentMap = compMap; 
    compDescription = new JTextArea(); 
    setSize(300, 300); 

    JScrollPane scroll = new JScrollPane(compDescription); 
    add(scroll); 
    compDescription.setPreferredSize(new Dimension(getSize())); 
    compDescription.setLineWrap(true); //Wrap the text when it reaches the end of the TextArea. 
    compDescription.setWrapStyleWord(true); //Wrap at every word rather than every letter. 
    compDescription.setEditable(false); //Text cannot be edited. 
    displayDescription(); 
} 

回答

1

將​​更改爲super(new BorderLayout());

JPanel默認使用FlowLayout,它不會試圖強制組件的大小來填充空間。通過切換到BorderLayout,中心組件(滾動窗格)將被強制匹配面板的大小。

+0

這樣做,謝謝。 – Shaun