【问题标题】:How to change the color of particular characters in JLabel?如何更改 JLabel 中特定字符的颜色?
【发布时间】:2012-03-02 19:02:58
【问题描述】:

如何更改 JLabel 中特定字符的颜色?例如,

JLabel lbl = new JLabel("My Text");

我希望My 有蓝色,而Text 有红色。 有可能吗?

【问题讨论】:

    标签: java html swing fonts jlabel


    【解决方案1】:

    是的,请查看How to Use HTML in Swing Components

    JLabel lbl = new JLabel("<html><font color=blue>My</font><font color=red>Text</font></html>");
    

    【讨论】:

      【解决方案2】:

      用 html 试试:

      String labelText =
        "<html><FONT COLOR=BLUE>My</FONT>" +
        "<FONT COLOR=RED>Text</FONT></html>";
      JLabel lbl = new JLabel(labelText);
      

      【讨论】:

        【解决方案3】:

        您可以在 JLabels 中使用 HTML,因此应该可以:

        JLabel lbl = new JLabel("<html><span style='color: blue;'>My</span> <span style='color: red;'>Text</span></html>");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 2016-03-04
          • 2017-03-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多