简体   繁体   English

更改JLabel的颜色字符文本?

[英]Changing color character text of a JLabel?

I'm trying changing color character text of a JLabel. 我正在尝试更改JLabel的颜色字符文本。

I have a JLabel with text: "JLABEL" and I want change only color character "L". 我有一个JLabel ,其文本为:“ JLABEL”,我只想更改颜色字符“ L”。 After to change, I want display text "JLABEL" with character "L" changed. 更改后,我想更改字符“ L”的显示文本“ JLABEL”。

How can I do this ? 我怎样才能做到这一点 ?

I'm trying this: 我正在尝试:

private void characterFormat(){  
    jlabel.setText("JLABEL");
    char[] t = jlabel.getText().toCharArray();
    String txtFormat = "";
    for(int x = 0; x < t.length; x++){
        if(t[x] == 'L'){
            txtFormat += String.format("<html><font color=RED>%s</font></html>", t[x]);
        }
    }        
    jlabel.setText(txtFormat);
}

I'm not sure if I understand your question correct. 我不确定我是否正确理解您的问题。 To just change a JLables text and highlighting the 'L' Character in red you can do the following: 要仅更改JLables文本并以红色突出显示“ L”字符,您可以执行以下操作:

String text = jlabel.getText(); 
text = "<html><body>" + text.replaceAll("L", "<span style=\"color:red\">L</span>") + "</body></html>";
jlabel.setText(text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM