0
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);

如果我打开面板,垂直滚动条从面板中间开始,我需要始终从顶部启动滚动条。

有什么方法可以这样做吗?

提前致谢

4

2 回答 2

1

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 JComponents. 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. 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. However, if you add the JComponent later by calling setViewportView(), it positions it centrally by default. It has something to do with the way that the JComponents 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. 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.

Give both of these a try and see how you go.

于 2012-10-31T09:15:27.507 回答
0

setCaretPosition(0) 为我工作

于 2012-12-20T06:28:20.157 回答