有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用颜色代码以编程方式格式化字符串

我有十六个代表java的字符对(例如§c,每个代码以§开头,后跟0-9或a-f)。awt。颜色这里有两个例子:

  1. §a是颜色。getHSBColor(0.333f、1.000f、1.000f)
  2. §6是颜色。getHSBColor(0.167f、1.000f、0.502f)

我需要一种方法将字符串的颜色设置为这些颜色的任意组合

Example: §aThis text is green, but §6this text is gold.
Formatted: <green>This text is green, but <gold>this text is gold

请注意<&燃气轮机;标记只是示例,我需要使用以下方法在JTextPane中实际设置颜色:

public void append(Color c, String s)
{
    try
    {
        SimpleAttributeSet sas = new SimpleAttributeSet();
        StyleConstants.setForeground(sas, c);

        this.getDocument().insertString(getDocument().getLength(), s, sas);
    }
    catch (BadLocationException ex) { ex.printStackTrace();}
}

到目前为止,我的方法还没有奏效。我已经看到了几个密切相关的问题,但没有一个与根据“颜色代码”更改文本有关

Tl;dr-我需要一种方法,用于使用颜色代码以编程方式将格式化文本插入JTextPane


共 (0) 个答案