java - 将字体和颜色设置为一个 JLabel 中包含的字符串的不同部分

标签 java swing user-interface jlabel

我应该如何处理这个字符串

欺骗宝珠范围:880 冷却时间:7 成本:70/75/80/85/90 法力

我想要颜色为蓝色、尺寸为 14 的 “欺骗之球”“范围”/“冷却”/“Cost” 颜色为黑色,尺寸为 12,数字为绿色,尺寸为 10。

它必须包含在一个 JLabel 中。

这可能吗?

最佳答案

E.G. (根据需要调整)

enter image description here

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

class ColoredLabel {

    static String text =
            "<html>"
            + "<head>"
            + "<style type='text/css'>"
            + ".name {"
            + " font-size: 16px;"
            + " color: blue;"
            + "}"
            + ".value {"
            + " font-size: 12px;"
            + " color: green;"
            + "}"
            + "</style>"
            + "</head>"
            + "<body>"
            + "<h1>Orb of Deception</h1>"
            + "<table border=1>"
            + "<tr><td class='name'>Range</td><td class='value'>880</td></tr>"
            + "<tr><td class='name'>Cost</td><td class='value'>70/75/80/85/90 Mana</td></tr>"
            + "<tr><td class='name'>Cooldown</td><td class='value'>7</td></tr>"
            + "</table>"
            + "</body>"
            + "</html>";

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, new JLabel(text));
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

关于java - 将字体和颜色设置为一个 JLabel 中包含的字符串的不同部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20583902/

相关文章:

java - 通过http GET从Json对象中提取数据

Java GridBagLayout 布局问题

java - 使用 ContentPane 的 JComponent 功能

java - 从组合框中选择和重新选择

asp.net - 多个 UI 的接口(interface) Controller (ViewModel)

java - 如何使 JFrame 透明?

java - 如何用java制作频率直方图

java - 在函数原型(prototype)中它显示错误

javax swing - 创建窗口

java - 我的台词没有去到它们应该去的地方