有 Java 编程相关的问题?

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

Java文本字符串:只需要一个粗体字

我在文本框中有一个字符串,只希望其中一个单词用粗体。有没有一种方法可以在代码中这样做而不附加文本?这有点像是在xml/html中完成的。。。加下划线怎么样

不喜欢使用xml或html来实现这一点——更喜欢在java代码中保留字符串

谢谢


共 (2) 个答案

  1. # 1 楼答案

    我喜欢使用带有属性的JTextPane:

    JTextPane textPane = new JTextPane();
    textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
    StyledDocument doc = textPane.getStyledDocument();
    
    //  Define a keyword attribute
    
    SimpleAttributeSet keyWord = new SimpleAttributeSet();
    StyleConstants.setForeground(keyWord, Color.RED);
    StyleConstants.setBold(keyWord, true);
    
    //  Change attributes on some text
    
    doc.setCharacterAttributes(4, 3, keyWord, false);