Swing HTML 绘制字符串
我正在尝试为特定目的创建一些特殊组件,在该组件上我需要绘制一个 HTML 字符串,这里是一个示例代码:
public class MyComponent extends JComponent{
public MyComponent(){
super();
}
protected void paintComponent(Graphics g){
//some drawing operations...
g.drawString("<html><u>text to render</u></html>",10,10);
}
}
不幸的是,drawString 方法似乎无法识别 HTML 格式,它愚蠢地绘制字符串,就像这是。
有什么办法可以让它发挥作用吗?
I'm trying to create some special component for a specific purpose, on that component I need to draw a HTML string, here's a sample code:
public class MyComponent extends JComponent{
public MyComponent(){
super();
}
protected void paintComponent(Graphics g){
//some drawing operations...
g.drawString("<html><u>text to render</u></html>",10,10);
}
}
Unfortunately the drawString method seems to be not recognizing the HTML format, it foolishly draws the string just as it is.
Is there any way to make that work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果是 Java2D 的粉丝;但为了在 Swing 组件和布局中充分利用 HTML,我鼓励您使用 @camickr 建议的组件方法。如有必要,您可以使用 Flyweight 渲染器方法参见
JTable
、等,其中重复使用单个组件进行绘图。下面的示例是该技术非常的简化轮廓,仅更改颜色和位置。附录:更新的示例;另请参见
CellRendererPane
和使您的应用程序飞翔:实施 Flyweight 以提高性能。If a fan of Java2D; but to get the most leverage from HTML in Swing components and layouts, I'd encourage you to use the component approach suggested by @camickr. If necessary, you can use the flyweight renderer approach seen in
JTable
, et al, in which a single component is used repeatedly for drawing. The example below is a very simplified outline of the technique, changing only the color and location.Addendum: Updated example; see also
CellRendererPane
and Make your apps fly: Implement Flyweight to improve performance.正如其他人所评论的,Swing 组件支持 HTML 3.2 和基本样式。
有关如何在
paintComponent(Graphics)
方法中利用该功能的详细信息,请参阅 此线程。方法是将标签渲染到图像,然后将图像渲染到
Graphics
对象。As others have commented, Swing components support HTML 3.2 and basic styles.
For details on how to leverage that ability in the
paintComponent(Graphics)
method, see theLabelRenderTest.java
source on this thread.The approach is to render the label to an image, then render the image to the
Graphics
object.您的问题的答案是不支持 HTML。
简单的解决方案是使用 Swing 组件并正确布局它们。
如果您想自己执行此操作,则需要操作用于 drawString() 方法的 Font。您可以使用斜体、粗体等字体。
The answer to your question is that HTML is not supported.
The easy solution is to use Swing components and lay them out properly.
If you want to do it yourself then you need to manipulate the Font that you use for the drawString() method. You can use a Font with italics, bold etc.
我找到了一种简短而干净的方法来模拟
paintHtmlString
;这是代码:感谢大家的帮助,我非常感激。
I've found a short and a clean way to simulate the
paintHtmlString
; here's the code:Thanks to every one for the help, I do appreciate that very much.
使用
JLabel
作为您的基类。Graphics.drawString()
只能...绘制一个字符串。Use
JLabel
as your base class.Graphics.drawString()
can only... draw a string.这不是实现的方法。
drawString(String str, int x, int y)
仅使用此图形上下文的当前字体和颜色绘制指定字符串给出的文本。您可以从这里获取有关您的问题的更多信息,如何在 Swing 中使用 HTML组件
This is not the way to implement.
The
drawString(String str, int x, int y)
only draws the text given by the specified string, using this graphics context's current font and color.You can get more information for your problem from here, How to Use HTML in Swing Components
没有一种简单的方法来渲染 HTML 以使用 Graphics 进行绘制,请使用 JLabel。
然而,有一种有趣的方法可以通过使用 HTML 创建 JLabel 并将其图形绘制到组件来实现此目的:
实现此目的的最简单方法是为图形设置字体,例如:
There isn't an easy way to render HTML to paint with Graphics, go with a JLabel.
However, there is an interesting way to accomplish this by creating a JLabel with HTML and painting it's graphics to the component:
The easiest way to accomplish this would be to set a font for your graphics, for example: