![](/img/trans.png)
[英]MigLayout causes exception in JOption pane when JTextArea is used. FlowLayout is just fine
[英]MigLayout JTextArea is not shrinking when used with linewrap=true
如果我像這樣使用帶有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);
}
然后,當調整窗口大小時,JTextArea會完美地縮小和縮小。 當我將linewrap設置為true時,當我再次縮小窗口時,JTextArea不會縮小。 我非常感謝任何幫助。 謝謝
馬塞爾
我剛剛發現這可以通過改變線來簡單解決
this.add(jTextArea1, "cell 0 1 2 1,growx");
至
this.add(jTextArea1, "cell 0 1 2 1,growx, wmin 10");
並且不需要額外的面板。 設置明確的最小尺寸就是訣竅。
說明:請參閱MiGLayout白皮書中有關填充的部分下的注釋:
這是因為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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.