本文介绍了将Font和Colors设置为一个JLabel中包含的String的不同部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于该String,我应该怎么做?

How should I do that for this String?

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

我想将"Orb of Deception"的颜色设置为蓝色,尺寸为14,将"Range"/"Cooldown"/"Cost"的颜色设置为黑色,尺寸12,将数字设置为绿色,将尺寸设置为10.

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.

它必须包含在一个JLabel中.

这可能吗?

推荐答案

例如(根据需要进行调整)

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

这篇关于将Font和Colors设置为一个JLabel中包含的String的不同部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:26