更改 JScrollPane 的视图,以便 JEditorPane 的特定部分可见

Changing view of JScrollPane so that a certain part of JEditorPane is visible

如何使 jEditorPane 中的某行文本在 JScrollPane 中可见?

private JEditorPane myEditorPane = new JEditorPane();
private JScrollPane myScrollPane = new JScrollPane(myEditorPane);

myEditorPane.setContentType("text/html");
myEditorPane.setText("<html>" + getMyString(x) + "</html>");
myEditorPane.repaint();

getMyString 得到一个由\n 分隔的多行长字符串。 该程序有 2 个面板。然后程序沿着上面面板中的行向下移动,并为每一行在该行中的文本下划线,并在另一个面板中显示相关图像。每个都被浏览 1 秒,然后移动到下一行文本下划线并显示下一张图像。我已经把它往下看并依次给它们加下划线,显示每个带下划线的文本行的相关图像。但是滚动窗格每次都会跳到开头。

我想我必须在视口上使用 scrollRectToVisible,但我如何找出 JEditorPAne 中的部分字符串的矩形?

我已经能够通过在滚动条上直接调用 setValue 来操纵滚动条的位置。

myScrollPane.getVerticalScrollBar().setValue()

问题是,要设置什么值?你能假设文本行是相同的吗?如果他们包装,事情会变得复杂。我假设他们不这样做,并且每行的高度相同,因为字体和大小相同。然后你需要知道当前显示的行是什么索引,以及总行数。

完成后,您可以通过将行索引乘以编辑器高度乘以行数来将行索引转换为编辑器 Y 位置,并进行相应设置。

myScrollPane.getVerticalScrollBar().setValue( (int) indexOfCurrentLine * myEditorPane.getHeight() / countOfLines );

显然,如果没有 SSCCE,就无法测试其中的任何一个。