这是因为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