【发布时间】:2016-12-15 03:33:21
【问题描述】:
我有一个内容类型为“text/html”的JTextPane。它集成在JScrollPane 中。
用户可以在这个JTextPane 中向下滚动并点击一个按钮。此时我想计算JTextPane的最顶部实际可见线!
我在另一篇文章中发现了这些行:
public Integer getActualDisplayedRows() {
int y1 = jtextpane.getVisibleRect().y;
int lineHeight = jtextpane.getFontMetrics(jtextpane.getFont()).getHeight();
int topMostRow = (int) Math.ceil((double) y1 / lineHeight);
return topMostRow;
}
但这计算不正确。lineHeight 中的数字太小。因此,如果我滚动到第 20 行 - 例如 - 该方法返回的值超过 20..
我尝试通过样式表设置行高(如here):
StyleSheet sh = editorKit.getStyleSheet();
sh.addRule("body {line-height: 50px}");
但不管我在那里设置什么像素数,生成的JTextPane 始终具有相同的高度(我使用的是body 标签)..
你有什么建议吗??
非常感谢您的帮助!
【问题讨论】:
-
一个 HTML 文档可以包含多种字体的内容。您是否尝试确定 body 元素字体的行高?