这就是您可以编辑 HTMLFactory 的方式。在这里,我更改了 InlineView 以使我能够沿对角线穿过它。将其用于您自己的目的。
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 即可。问题是您需要编辑段落和文本在浮动图像周围呈现的方式,因此您需要重新定义 BlockView 和 InlineView。所以这不会是一件容易的事。但是,如果您成功了,请不要犹豫分享!
检查 HTMLEditorKit.create 方法以更好地了解视图生成期间发生的情况。
祝你好运
编辑:
在仔细阅读您的问题后,我意识到您不想呈现float
属性,而只是垂直对齐...为此,您有setAlignmentY
. 将其设置为 0.5 以进行中间对齐。对于 JLabel,您可能还需要使用setVerticalAlignment
和setVerticalTextPosition