JTextArea 中的换行导致 JScrollPane 与 MiGLayout 表现不佳
我遇到了和这个人同样的问题:
与 linewrap=true 一起使用时,MigLayout JTextArea 不会缩小
,我使用了答案之一中描述的解决方案;明确设置最小尺寸。如果将包含 JTextArea 的 JPanel 直接放置在 JFrame 中,然后调整窗口大小,则效果很好。
但是,当将包含 JTextArea 的面板放置在 JScrollPane 中时, 同样的问题再次出现。这是为什么?如何解决?
干杯
编辑:一个例子
public class MiGTest2 extends JFrame{
public MiGTest2(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
panel.add(textArea, "wmin 10");
//panel.add(new JTextField());
JScrollPane scrollPane = new JScrollPane(panel);
//add(panel);
add(scrollPane);
pack();
}
public static void main(String[] args){
new MiGTest2().setVisible(true);
}
}
如果您取消注释//add(panel);
,并注释add(scrollPane);
,缩小窗口大小将还缩小了 JTextArea。也就是说,它不适用于 JScrollPane。另请注意,在第一次放大窗口后缩小窗口大小时,布局管理器似乎会翻转并开始“摇动”其所有内容
I am having trouble with the same thing as this guy:
MigLayout JTextArea is not shrinking when used with linewrap=true
and I used the solution described in one of the answers; to set the minimum size explicitly. This works fine if one places the JPanel which contains the JTextArea directly in a JFrame, and then resizes the window.
However, when placing the panel which contains the JTextArea inside a JScrollPane,
the same problem occurs again. Why is this, and how can one fix it?
Cheers
EDIT: An example
public class MiGTest2 extends JFrame{
public MiGTest2(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new MigLayout("fillx, debug", "[fill]"));
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
panel.add(textArea, "wmin 10");
//panel.add(new JTextField());
JScrollPane scrollPane = new JScrollPane(panel);
//add(panel);
add(scrollPane);
pack();
}
public static void main(String[] args){
new MiGTest2().setVisible(true);
}
}
If you uncomment //add(panel);
, and comment add(scrollPane);
, shrinking the window size will also shrink the JTextArea. That is, it does not work with a JScrollPane. Also note how the layout manager seems to flip out and starts "shaking" all its contents when shrinking the size of the window after first enlarging it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有一个非常类似的问题,并遵循上述 问题也没有帮助我。然而,它确实提供了一个有价值的想法——问题在于启用换行的 JTextArea 的宽度。
对我有用的是使用命令
width
在组件级别设置最小宽度和首选宽度。例如,宽度 10:500:
。I had a very similar problem and following the answer in the mentioned question did not help me either. However, it did provide a valuable idea -- the problem is in the width of the JTextArea with wrap enabled.
What worked for me was setting both minimum and preferred width at the component level using command
width
. For example,width 10:500:
.当与 JScrollPanes 一起使用时,我在 JTextAreas 和换行方面也遇到了类似的问题。
对我有用的解决方案是创建一个自定义面板,该面板实现 Scrollable 接口并重写 getScrollableTracksViewportWidth() 方法以返回 true。这会导致滚动窗格仅垂直滚动,并让 JTextArea 中的换行按预期工作。
MigTest2 变为:
I've had similar problems with JTextAreas and wrapping when used with JScrollPanes.
A solution that worked for me was to create a custom panel that implements the Scrollable interface and overrides the getScrollableTracksViewportWidth() method to return true. This forces causes the scroll pane to only scroll vertically and lets line wrapping in the JTextArea work as expected.
and MigTest2 becomes:
通常,您会将 JTextArea 放入 JScrollPane 中。像这样:
Normally, you would put the JTextArea into your JScrollPane. Like this:
不太确定您想在这里实现什么,请尝试运行它并看看它是否适合您的需求?
Not really sure what you're trying to achieve here, try running this and see if it fits your need?