2010-03-19 82 views
8

如果我使用像这样用MigLayout一个JTextArea:与linewrap使用时MigLayout JTextArea是不缩水=真

MigLayout thisLayout = new MigLayout("", "[][grow]", "[]20[]"); 
    this.setLayout(thisLayout); 
    { 
jLabel1 = new JLabel(); 
this.add(jLabel1, "cell 0 0"); 
jLabel1.setText("jLabel1"); 
    } 
    { 
jTextArea1 = new JTextArea(); 
this.add(jTextArea1, "cell 0 1 2 1,growx"); 
jTextArea1.setText("jTextArea1"); 
jTextArea1.setLineWrap(false); 
    } 

那么的JTextArea增长和调整窗口大小时缩小完美。当我将linewrap设置为true时,当再次使窗口变小时,JTextArea不会缩小。我非常感谢任何帮助。由于

马塞尔

+0

一个简单而快速的解决方案,你可以在这里找到! http://stackoverflow.com/a/7833439/2530822 – TotoliciCristian 2014-05-29 13:28:52

回答

8

这是因为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); 
} 
+0

非常感谢Kaleb。我也试图把它放在一个scrollpane中,这也很好。问候Marcel – 2010-05-01 15:57:34

17

我才发现,原来这可以简单地通过解决转产

this.add(jTextArea1, "cell 0 1 2 1,growx"); 

this.add(jTextArea1, "cell 0 1 2 1,growx, wmin 10"); 

并且不需要额外的面板。设置一个明确的最小大小是诀窍。

说明:见附注下段上的MiGLayout白皮书填充:

http://www.migcalendar.com/miglayout/whitepaper.html

+0

+1这似乎也解决了其他顽固组件的问题。在我的情况下,我的铬浏览器框架不会缩小,直到添加最小尺寸。 – 2011-11-30 10:02:26

+0

已证实,这种方法可行,比使用公认的答案更容易使用。你可以指定''wmin 0“'这在我看来是清楚的,是为了解决一个问题而添加的。为您的最小修复+1。 – Timmos 2014-04-10 12:32:30

+0

请考虑将此答案标记为已接受。像这里的魅力一样。谢谢。 – FaithReaper 2017-06-23 08:58:33