2012-10-31 60 views
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); 

如果我打開面板,垂直滾動條從面板中間開始,我需要始終從頂部開始滾動條。始終從頂部開始垂直滾動條

是否有任何方法可以做到這一點?

在此先感謝

+0

是的有方法可以做到這一點,但視口視圖可能會自動改變它。例如,可編輯的JTextPane會自動嘗試顯示插入位置並可能自動觸發滾動。考慮發佈[SSCCE](http://sscce.org),以便我們可以仔細檢查您的問題 –

回答

1

我真的不知道的NScrollPane是什麼,或從getNpQuestionArea()返回的成分是什麼,但看起來他們很可能只是JComponent秒的擴展。在這種情況下,你也許可以撥打下列...

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

此外,假設NScrollPaneJScrollPane的延伸,如果你在getNpQuestionArea()傳遞到NScrollPane的構造,我相信它應該自動地在頂部。所以,這樣的事情...

nspQuestionArea = new NScrollPane(getNpQuestionArea()); 

如果您解析JComponentNScrollPane構造,我相信它在位置滾動條左上角的JComponent的。但是,如果稍後通過調用setViewportView()來添加JComponent,則默認情況下會將其置於中央。它與JComponent在生成顯示時的佈局方式有關 - 如果將它添加到構造函數中,它會將NScrollPane佈局爲您傳遞給它的JComponent的正確大小和位置。但是,如果您創建默認的NScrollPane並且以後只給它JComponent,它只會創建一個NScrollPane默認大小,而不是適當地適合JComponent

給這兩個這些嘗試,看看你如何去。

0

setCaretPosition(0)在我工作

相關問題