繁体   English   中英

JTextArea不断将主面板滚动到顶部

[英]JTextArea keeps scrolling the main panel to the top

我定义了一个JTextArea,如下所示:

JTextArea textArea = new JTextArea();
textArea.setText("Some text");
textArea.setEditable(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.getVerticalScrollBar().setAutoscrolls(false);

现在,此组件是已添加到主JPanel中的许多组件之一,其定义如下:

JPanel panel = new JPanel();
panel.setName("Some name");

JScrollPane scrollPane = new JScrollPane();
scrollPane.setName("Some name");
scrollPane.getViewport().add(panel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getVerticalScrollBar().setAutoscrolls(false);

现在,JTextArea组件显示在主面板底部附近,并且部分隐藏,因此您必须向下滚动才能看到所有内容。 但是我遇到的问题是,当我单击它时,主面板会自动再次滚动回到顶部,再次隐藏了我的大部分JTextArea组件。 因此,在单击它之后,用户必须再次向下滚动才能键入内容。

但是我不知道为什么要这么做。 使用JTextArea是否有问题? 如果我使用JTextField,则不会发生此问题。

任何帮助将不胜感激!!

我认为,用scrollPane.getViewport().add(panel) scrollPane.getViewport().setView(panel)替换scrollPane.getViewport().add(panel) scrollPane.getViewport().setView(panel)将解决此问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM