Geo*_*rey 10 html java swing jcomponent graphics2d
我正在尝试为特定目的创建一些特殊组件,在该组件上我需要绘制一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,drawString方法似乎没有识别HTML格式,它愚蠢地绘制字符串就像它一样.
有没有办法让这项工作?
tra*_*god 10
如果是Java2D的粉丝; 但是为了在Swing组件和布局中获得HTML的最大优势,我建议您使用@camickr建议的组件方法.如果必要的话,可以使用轻量级渲染方式看到JTable,等人,其中单个组件重复用于绘制.下面的示例是该技术的非常简化的轮廓,仅更改颜色和位置.
附录:更新的例子; 另请参阅CellRendererPane并使您的应用程序飞行:实施Flyweight以提高性能.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.CellRendererPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/questions/7774960 */
public class PaintComponentTest extends JPanel {
private static final int N = 8;
private static final String s = "<html><big><u>Hello</u></html>";
private JLabel renderer = new JLabel(s);
private CellRendererPane crp = new CellRendererPane();
private Dimension dim;
public PaintComponentTest() {
this.setBackground(Color.black);
dim = renderer.getPreferredSize();
this.add(crp);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < N; i++) {
renderer.setForeground(Color.getHSBColor((float) i / N, 1, 1));
crp.paintComponent(g, renderer, this,
i * dim.width, i * dim.height, dim.width, dim.height);
}
}
private void display() {
JFrame f = new JFrame("PaintComponentTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setSize(dim.width * N, dim.height * (N + 1));
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new PaintComponentTest().display();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
您的问题的答案是不支持HTML.
简单的解决方案是使用Swing组件并正确布局.
如果您想自己动手,那么您需要操作用于drawString()方法的Font.您可以使用带斜体的字体,粗体等.
我找到了一个简短而干净的方法来模拟paintHtmlString; 这是代码:
public class MyComponent extends JComponent {
private JLabel label = null;
public MyComponent() {
super();
}
private JLabel getLabel() {
if (label == null) {
label = new JLabel();
}
return label;
}
/**
* x and y stand for the upper left corner of the label
* and not for the baseline coordinates ...
*/
private void paintHtmlString(Graphics g, String html, int x, int y) {
g.translate(x, y);
getLabel().setText(html);
//the fontMetrics stringWidth and height can be replaced by
//getLabel().getPreferredSize() if needed
getLabel().paint(g);
g.translate(-x, -y);
}
protected void paintComponent(Graphics g) {
//some drawing operations...
paintHtmlString(g, "<html><u>some text</u></html>", 10, 10);
}
}
Run Code Online (Sandbox Code Playgroud)
感谢每一个人的帮助,我非常感谢.
这不是实施的方式.
该drawString(String str, int x, int y)只绘制由指定的字符串给出的文本,用此图形上下文的当前字体和颜色.
您可以从此处获取有关问题的更多信息,如何在Swing组件中使用HTML
| 归档时间: |
|
| 查看次数: |
8619 次 |
| 最近记录: |