這是因爲JTextArea
的自動擁有自己的最小寬度設定他們隨時調整。詳情請見MigLayout forum。粗略地總結一下,創建一個包含JTextArea
的面板,並讓您進一步控制調整行爲。下面是從上述論壇上的帖子摘錄:
static class MyPanel extends JPanel implements Scrollable
{
MyPanel(LayoutManager layout)
{
super(layout);
}
public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
{
return 0;
}
public boolean getScrollableTracksViewportHeight()
{
return false;
}
public boolean getScrollableTracksViewportWidth()
{
return true;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
{
return 0;
}
}
然後,無論你將使用JTextArea中,使用含有文本區域面板:
MigLayout thisLayout = new MigLayout("", "[][grow]", "[]20[]");
this.setLayout(thisLayout);
{
jLabel1 = new JLabel();
this.add(jLabel1, "cell 0 0");
jLabel1.setText("jLabel1");
}
{
JPanel textAreaPanel = new MyPanel(new MigLayout("wrap", "[grow,fill]", "[]"));
jTextArea1 = new JTextArea();
textAreaPanel.add(jTextArea1);
this.add(textAreaPanel, "cell 0 1 2 1,grow,wmin 10");
jTextArea1.setText("jTextArea1");
jTextArea1.setLineWrap(false);
}
一個簡單而快速的解決方案,你可以在這裏找到! http://stackoverflow.com/a/7833439/2530822 – TotoliciCristian 2014-05-29 13:28:52