[英]Always start vertical ScrollBar from the top
nspQuestionArea = new NScrollPane();
nspQuestionArea.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
nspQuestionArea.setVerticalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
nspQuestionArea.setViewportView(getNpQuestionArea());
//where getNpQuestionArea() return a panel
nspQuestionArea.getVerticalScrollBar().setValue(0);
nspQuestionArea.getVerticalScrollBar().setValue(0);
nspQuestionArea.getVerticalScrollBar().setUnitIncrement(5);
nspQuestionArea.setOpaque(false);
如果我打开面板,则垂直滚动条从面板中间开始,我需要始终从顶部开始滚动条。
有什么办法可以做到吗?
提前致谢
我不太确定NScrollPane
是什么,还是从getNpQuestionArea()
返回的组件是什么,但是看起来它们可能只是JComponent
的扩展。 在这种情况下,您可能可以致电以下人员...
getNpQuestionArea().scrollRectToVisible(new Rectangle(0,0,0,0));
此外,假设NScrollPane
是一个扩展JScrollPane
,如果在通过getNpQuestionArea()
进入的构造NScrollPane
,我相信它会自动在顶部。 所以像这样
nspQuestionArea = new NScrollPane(getNpQuestionArea());
如果将JComponent
解析为NScrollPane
构造函数,我相信它将滚动条放置在JComponent
的左上方。 但是,如果稍后通过调用setViewportView()
添加JComponent
,则默认情况下它将在中央setViewportView()
它。 它与生成显示时对JComponent
进行布局的方式有关-如果将其添加到构造函数中,则会将NScrollPane
为传递给它的JComponent
的正确大小和位置。 但是,如果您创建一个默认的NScrollPane
并仅在以后为其提供JComponent
,则它只会创建一个默认大小的NScrollPane
,而不是将其适当地适合JComponent
。
尝试一下这两种方法,看看您的计划如何。
setCaretPosition(0)为我工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.