Java Swing绘制html格式的字符串

标签 java html string swing draw

我想创建一个 String ,它以以下格式显示时间:10h 30min,但单位(小时和分钟)的字体应该比数字更小。当使用 JLabel 时,我使用带有 span 属性的 html 格式字符串来完成这项工作。

现在我想将这样的字符串添加到自定义对象中,并使用drawAlignedString方法编写它。但是,这里 html 传递不起作用。自定义对象显示我的代码,而不是格式化的字符串。

有没有办法让这个工作或任何其他解决方案来绘制具有不同子字符串的字符串?

这是我尝试过的:

    String time = String.format(
            "<html>%d<span style=\"font-family:Arial Unicode MS;font-size:12px;\">h </span>  %d<span "
                    + "style=\"font-family:Arial Unicode MS;font-size:12px;\">min</span></html>",
            absSeconds / 3600, (absSeconds % 3600) / 60);
    g2.setFont(this.centerTextFont);
    g2.setPaint(this.centerTextColor);
    TextUtilities.drawAlignedString(time, g2, (float) area.getCenterX(), (float) area.getCenterY(),
            TextAnchor.CENTER);

最佳答案

配置标签后,将Graphics传递给标签的paint方法。

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class LabelRenderTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {

            String title = "<html><body style='width: 200px; padding: 5px;'>"
                + "<h1>Do U C Me?</h1>"
                + "Here is a long string that will wrap.  "
                + "The effect we want is a multi-line label.";

                JFrame f = new JFrame("Label Render Test");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BufferedImage image = new BufferedImage(
                    400,
                    300,
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D imageGraphics = image.createGraphics();
                GradientPaint gp = new GradientPaint(
                    20f,
                    20f,
                    Color.red,
                    380f,
                    280f,
                    Color.orange);
                imageGraphics.setPaint(gp);
                imageGraphics.fillRect(0, 0, 400, 300);

                JLabel textLabel = new JLabel(title);
                textLabel.setSize(textLabel.getPreferredSize());

                Dimension d = textLabel.getPreferredSize();
                BufferedImage bi = new BufferedImage(
                    d.width,
                    d.height,
                    BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.createGraphics();
                g.setColor(new Color(255, 255, 255, 128));
                g.fillRoundRect(
                    0,
                    0,
                    bi.getWidth(f),
                    bi.getHeight(f),
                    15,
                    10);
                g.setColor(Color.black);
                textLabel.paint(g);
                Graphics g2 = image.getGraphics();
                g2.drawImage(bi, 20, 20, f);

                ImageIcon ii = new ImageIcon(image);
                JLabel imageLabel = new JLabel(ii);

                f.getContentPane().add(imageLabel);
                f.pack();
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}

关于Java Swing绘制html格式的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47748700/

相关文章:

java - 没有引用或无法找到引用导致的不确定错误 - java.lang.IndexOutOfBoundsException

java - 将 C# 字节数组转换为 Java 字节数组以及 Java 中的最大数组大小

html - 如何用CSS将右边的div向右移动

c++ - std::string 到 LPCTSTR

c++ - 为变量与参数中的字符串键入 std::string 的 id?

regex - 模糊正则表达式

java - 困难的 JAXB 映射

jquery - 如何使用其他页面的 CSS?

python - Django 模板中的 Rowspan 和分组

java - 包含 WebView 的 Intent