与linewrap = true一起使用时,MigLayout JTextArea不会收缩 [英] MigLayout JTextArea is not shrinking when used with linewrap=true

查看:102
本文介绍了与linewrap = true一起使用时,MigLayout JTextArea不会收缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将JTextArea与MigLayout这样使用:

If I use a JTextArea with MigLayout like this:

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不会收缩.我将非常感谢您的帮助.谢谢

then the JTextArea grows and shrinks perfectly when resizing the window. When I set the linewrap to true the JTextArea is not shrinking when I make the window smaller again. I would very much appreciate any help. Thanks

马塞尔

推荐答案

这是因为JTextArea会在每次调整大小时自动设置其最小宽度.有关详细信息,请访问 MigLayout论坛.概括地说,创建一个包含JTextArea的面板,使您可以进一步控制调整大小行为.以下是上述论坛文章的摘录:

This is because JTextArea's automatically have their minimum width set anytime they resize. Details are available on the MigLayout forum. To roughly summarize, create a panel that contains the JTextArea and gives you further control over the resize behavior. Here's an excerpt from the above forum post:

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,都应使用包含文本区域的面板:

Then, wherever you would use the JTextArea, use the panel containing the text area:

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);
} 

这篇关于与linewrap = true一起使用时,MigLayout JTextArea不会收缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆