到目前为止,我几乎已经尝试了所有方法,我还读到,Java 的 HTML 渲染非常有限,只能像 HTML 3/2 或类似的东西那样做。所以大多数现代 CSS 样式都不起作用。基本上我需要小图标(10x10 像素)与多行文本垂直对齐,但我找不到可行的解决方案。我发现的唯一解决方法是创建一个表:
<table><tr><td>text</td><td><img></td><td>text</td></tr></table>
这行得通,但只针对单行,如果有换行,显然就不行了。
通常在浏览器中工作的所有其他解决方案在 JTextPane 中都不起作用,例如为 div 中的表设置 style="float: left;"
,或者只使用 vertical-align: middle;
与 img 和 div 元素。
可能有一种“肮脏”的方式来操纵 HTMLEditorKit 的 getViewFactory 以返回所有 ImageViews 低于它们应该呈现的几个像素?
最佳答案
这就是您可以编辑 HTMLFactory 的方式。在这里,我更改了 InlineView 以使我能够沿对 Angular 线穿过它。将其用于您自己的目的。
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.Stroke;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.CSS;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.InlineView;
/**
*
* @author François Billioud
*/
public class HTMLBetterEditorKit extends HTMLEditorKit {
private final HTMLEditorKit.HTMLFactory factory = new HTMLBetterFactory();
@Override
public ViewFactory getViewFactory() {
return factory;
}
public static class HTMLBetterFactory extends HTMLEditorKit.HTMLFactory {
@Override
public View create(Element elem) {
AttributeSet attrs = elem.getAttributes();
Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
if (o == HTML.Tag.CONTENT) {
return new StrikeView(elem);
}
return super.create(elem);
}
private class StrikeView extends InlineView {
public StrikeView(Element elem) { super(elem); }
public void paint(Graphics g, Shape allocation) {
setStrikeThrough(false);//We will do that ourselves
super.paint(g, allocation);
Object textDecorationValue = getAttributes().getAttribute(CSS.Attribute.TEXT_DECORATION);
if(textDecorationValue!=null && textDecorationValue.toString().equals("line-through")) {
paintStrikeLine(g, allocation);
}
}
//paints the strike line
public void paintStrikeLine(Graphics g, Shape a) {
int y = a.getBounds().y+a.getBounds().height/2;
int x1 = (int) a.getBounds().getX();
int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth());
Stroke oldStroke = ((Graphics2D)g).getStroke();
((Graphics2D)g).setStroke(new BasicStroke(2));
g.drawLine(x1, y, x2, y);
((Graphics2D)g).setStroke(oldStroke);
}
}
}
}
您只需将 BetterHTMLEditorKit 设置为您的 JTextPane 的 EditorKit。
关于如何编辑图像的呈现方式,例如创建一个 FloatView。问题是您需要编辑围绕 float 图像呈现段落和文本的方式,因此您需要重新定义 BlockView 和 InlineView。所以这不会是一件容易的工作。但如果您成功了,请毫不犹豫地分享!
检查 HTMLEditorKit.create 方法以更好地了解 View 生成期间发生的情况。
祝你好运
编辑:
仔细阅读您的问题后,我意识到您不想呈现 float
属性,而只想呈现垂直对齐...为此,您有 setAlignmentY
。将其设置为 0.5 以实现中间对齐。对于 JLabel,您可能还需要使用 setVerticalAlignment
和 setVerticalTextPosition
关于java - JTextPane 垂直对齐文本和 <img> 图像与 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041881/