[英]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.