11

如果我像这样使用JTextAreawith MigLayout

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在调整窗口大小时完美地增长和收缩。当我将换行设置为 trueJTextArea时,当我再次缩小窗口时不会缩小。

4

2 回答 2

23

我刚刚发现这可以通过更改线路来解决

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

于 2011-05-16T19:16:05.550 回答
8

这是因为JTextArea' 在调整大小时会自动设置其最小宽度。详细信息可在MigLayout 论坛上找到。粗略地总结一下,创建一个包含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);
} 
于 2010-04-29T16:43:01.840 回答