如果我将JTextArea与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会完美地增长和收缩。当我将linewrap设置为true时,再次使窗口变小时,JTextArea不会收缩。非常感谢您的帮助。谢谢

马塞尔

最佳答案

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

07-28 13:58
查看更多