简体   繁体   English

始终从顶部开始垂直滚动条

[英]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);

if i open the panel,the vertical scrollbar starts from the middle of the panel,i need to start the scrollbar always from the top. 如果我打开面板,则垂直滚动条从面板中间开始,我需要始终从顶部开始滚动条。

is there any method to do like that? 有什么办法可以做到吗?

thanks in advance 提前致谢

I'm not really sure what an NScrollPane is, or what the component returned from getNpQuestionArea() is, but it looks like they're probably just an extension of JComponent s. 我不太确定NScrollPane是什么,还是从getNpQuestionArea()返回的组件是什么,但是看起来它们可能只是JComponent的扩展。 In that case, you can probably call the following... 在这种情况下,您可能可以致电以下人员...

getNpQuestionArea().scrollRectToVisible(new Rectangle(0,0,0,0));

Also, assuming that NScrollPane is an extension of JScrollPane , if you pass in the getNpQuestionArea() into the constructor of the NScrollPane , I believe it should automatically be at the top. 此外,假设NScrollPane是一个扩展JScrollPane ,如果在通过getNpQuestionArea()进入的构造NScrollPane ,我相信它会自动在顶部。 So, something like this... 所以像这样

nspQuestionArea = new NScrollPane(getNpQuestionArea());

If you parse the JComponent into the NScrollPane constructor, I believe it positions the scrollbar at the top-left of the JComponent . 如果将JComponent解析为NScrollPane构造函数,我相信它将滚动条放置在JComponent的左上方。 However, if you add the JComponent later by calling setViewportView() , it positions it centrally by default. 但是,如果稍后通过调用setViewportView()添加JComponent ,则默认情况下它将在中央setViewportView()它。 It has something to do with the way that the JComponent s are layed-out when generating the display - if you add it in the constructor, it lays out the NScrollPane to the correct size and location for the JComponent that you pass to it. 它与生成显示时对JComponent进行布局的方式有关-如果将其添加到构造函数中,则会将NScrollPane为传递给它的JComponent的正确大小和位置。 However, if you create a default NScrollPane and only give it the JComponent later, it just creates a NScrollPane at a default size, rather than fitting it appropriately to the JComponent . 但是,如果您创建一个默认的NScrollPane并仅在以后为其提供JComponent ,则它只会创建一个默认大小的NScrollPane ,而不是将其适当地适合JComponent

Give both of these a try and see how you go. 尝试一下这两种方法,看看您的计划如何。

setCaretPosition(0)为我工作

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

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