简体   繁体   English

如何使Swing使用我自己的HTMLEditorKit用于JLabel / JButton / etc

[英]How to make Swing use my own HTMLEditorKit for JLabel/JButton/etc

Lets say I've made a sublcass of HTMLEditorKit (and other relevant classes) in order to display some custom HTML tags. 假设我已经创建了HTMLEditorKit (和其他相关类)的子级,以显示一些自定义HTML标记。 I want to be able to to use these custom tags in JLabel , JButton , and so on. 我希望能够在JLabelJButton使用这些自定义标签。 Is there any way to do this beside creating my own look-and-feel? 除了创建自己的外观之外,有没有办法做到这一点? That is, I want to tell swing "use this instance of HTMLEditorKit for rendering HTML in JLabel /etc" regardless of what look-and-feel is currently being used. 也就是说,我想告诉swing“使用这个HTMLEditorKit实例在JLabel / etc中呈现HTML”,无论当前使用什么样的外观。 From the little poking around I've done in Swing internals I don't think it's possible, but I'd love to be proven wrong. 从我在Swing内部进行的小捅我不认为这是可能的,但我很想被证明是错的。

It can be done: 可以办到:

The key is in the class javax.swing.plaf.basic.BasicLabelUI , which is a basic UI for labels. 关键是在类javax.swing.plaf.basic.BasicLabelUI ,它是标签的基本UI。

In the paint method, we can see this code: 在paint方法中,我们可以看到这段代码:

View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
    v.paint(g, paintTextR);
}

A BasicHTML class is the supplier of HTML capabilities in Java, so, theoretically, if you replace the client property for BasicHTML.propertyKey with your own implementation of View, then that class will be used and you can do whatever you want to render the text. BasicHTML类是Java中HTML功能的供应商,因此,理论上,如果用您自己的View实现替换BasicHTML.propertyKey的客户端属性,那么将使用该类,您可以执行任何想要呈现文本的内容。

The class javax.swing.plaf.basic.BasicLabelUI is the parent of most of other LAF label UIs out there, but not all, so it may not work for all LAFs. javax.swing.plaf.basic.BasicLabelUI是大多数其他LAF标签UI的父级,但不是全部,所以它可能不适用于所有LAF。 LAFs that don't support HTML using the BasicHTML class will not work with your fix, either. 使用BasicHTML类不支持HTML的BasicHTML也不适用于您的修复程序。

But IMHO this is more of a hack than feature. 但恕我直言,这更像是一个黑客而非功能。 You are programming againts implementation, not interface. 你正在编写反向编程实现,而不是接口。 So if you don't have really serious reasons to do that, I would suggest to find a cleaner way to render your custom HTML, such as JLabel subclass. 因此,如果您没有真正认真的理由这样做,我建议您找一种更清晰的方式来呈现您的自定义HTML,例如JLabel子类。

It can't be done normally . 它不能正常完成。 . . If you extend the JLabel and JButton classes, it might be possible, but these seems like a lot of work for something there are better ways to do[1]. 如果扩展JLabelJButton类,它可能是可能的,但这些似乎有很多工作可以做更好的方法[1]。 Would be useful classes though. 虽然会是有用的课程。

[1]: If the text doesn't need to be dynamic, try using images in JImagesIcons . [1]:如果文本不需要是动态的,请尝试在JImagesIcons使用图像。

Please refer to https://bugs.openjdk.java.net/browse/JDK-6540252 请参阅https://bugs.openjdk.java.net/browse/JDK-6540252
The bug reporter proposes a several workarounds. 臭虫记者提出了几个解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM