我正在处理显示超文本标记语言文档的JEditorPane,我创建了一个按钮,每当我点击它时,它就会把我滚动到下一个视窗,“就像我正在翻一页书”。
但是,有时我在视口的顶部看到一行的一部分,那么有没有办法强制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);
}发布于 2012-01-24 13:53:33
我猜您在文档中有一个想要显示的位置。您可以使用modelToView()方法获取该位置的可视矩形。并使用y位置设置视口。例如,使用scrollRectToVisible在矩形参数中传递y和视口高度。
https://stackoverflow.com/questions/8981965
复制相似问题