[英]MigLayout JTextArea is not shrinking when used with linewrap=true
If I use a JTextArea with MigLayout like this: 如果我像这样使用带有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);
}
then the JTextArea grows and shrinks perfectly when resizing the window. 然后,当调整窗口大小时,JTextArea会完美地缩小和缩小。 When I set the linewrap to true the JTextArea is not shrinking when I make the window smaller again.
当我将linewrap设置为true时,当我再次缩小窗口时,JTextArea不会缩小。 I would very much appreciate any help.
我非常感谢任何帮助。 Thanks
谢谢
Marcel 马塞尔
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: 说明:请参阅MiGLayout白皮书中有关填充的部分下的注释:
http://www.migcalendar.com/miglayout/whitepaper.html http://www.migcalendar.com/miglayout/whitepaper.html
This is because JTextArea
's automatically have their minimum width set anytime they resize. 这是因为
JTextArea
在调整大小时自动设置其最小宽度。 Details are available on the MigLayout forum . 有关详细信息,请访问MigLayout论坛 。 To roughly summarize, create a panel that contains the
JTextArea
and gives you further control over the resize behavior. 粗略地总结一下,创建一个包含
JTextArea
的面板,让您进一步控制调整大小行为。 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: 然后,无论您在何处使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.