Setting Font and Colors to different parts of String that is contained in one JLabel

我是研究僧i 提交于 2019-12-12 23:07:25

问题


How should I do that for this String?

Orb of Deception Range: 880 Cooldown: 7 Cost: 70/75/80/85/90 Mana

I want to have color blue and size 14 for "Orb of Deception", and "Range"/"Cooldown"/"Cost" color black size 12, and the numbers color green and size 10.

It must be contained in one JLabel.

Is this possible?


回答1:


E.G. (adjust to need)

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);
    }
}


来源:https://stackoverflow.com/questions/20583902/setting-font-and-colors-to-different-parts-of-string-that-is-contained-in-one-jl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!