3

我正在处理JEditorPane显示 HTML 文档,我创建了一个按钮,每次单击它时都会滚动到下一个视图端口,“就好像我在翻一页书一样”。

但是,有时我会在视口顶部看到一行的一部分,那么有没有办法强制JScrollBar滚动到一行的前面?

我尝试setBlockIncrement()了成员方法,但它根本不起作用。

这是我最好的尝试:

//get the visible rectangle as well as the most bottom right point. 
Rectangle rec = jEditorPane1.getVisibleRect();
Point pt = new Point((int)rec.getX() +(int)rec.getWidth(),(int)rec.getY() +      (int)rec.getHeight());
// get the offset of the most bottom right point 
int off = jEditorPane1.viewToModel(pt);
try {
//get next viewable rectangle and scroll to it 
rec = jEditorPane1.modelToView(off+100);
rec.height = jEditorPane1.getVisibleRect().height;
jEditorPane1.scrollRectToVisible(rec);
} catch (BadLocationException ex) {
Logger.getLogger(NewJFrame2.class.getName()).log(Level.SEVERE, null, ex);
}
4

1 回答 1

4

猜猜您在要显示的文档中有一个职位。您可以使用 modelToView() 方法获得位置的可见矩形。并使用 y 位置设置您的视口。例如,使用 scrollRectToVisible 在矩形参数中传递 y 和视口高度。

于 2012-01-24T05:53:33.203 回答