当与 linewrap=true 一起使用时,MigLayout JTextArea 不会收缩

发布于 2024-08-25 23:21:50 字数 538 浏览 10 评论 0原文

如果我将 JTextAreaMigLayout 一起使用,如下所示:

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气尐潴 2024-09-01 23:21:50

我刚刚发现这个问题可以简单地通过更改线路来解决

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

I just discovered that this can simply be resolved by changing the line

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

to

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

and no extra panels are needed. Setting an explicit minimum size is what does the trick.

Explanation: see the note under the section on padding in the MiGLayout whitepaper:

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

木落 2024-09-01 23:21:50

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

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

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);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文