3

HTMLドキュメントを表示することを扱って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);
}
4

1 に答える 1

4

Document 内に表示したい位置があるとします。modelToView() メソッドを使用して、位置の可視矩形を取得できます。y 位置を使用してビューポートを設定します。たとえば、rectangle パラメータで y とビューポートの高さを渡す scrollRectToVisible を使用します。

于 2012-01-24T05:53:33.203 に答える