Sør*_*zen 3 html java swing jtextarea
我正在尝试在我的JTextPane中加粗一行,但我没有做任何工作.我尝试用新的粗体字体来编写这一行,但它没有帮助.
Font font = new Font("Consolas", Font.BOLD, 11);
textPane.setFont(font);
textPane.setText(textPane.getText() + "\n" + getTimeStamp() + sender + ": " + message);
textPane.setFont(defaultFont);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
最简单的方法是从JTextPane获取StyledDocument,并使用setCharacterAttributes()方法.
StyledDocument对象上的setCharacterAttributes方法允许您设置特定字符范围,一组属性,可以包括BOLD.
一些示例代码可能是
// set chars 4 to 10 to Bold
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setBold(sas, true);
textPane.getStyledDocument().setCharacterAttributes(4, 6, sas, false);
Run Code Online (Sandbox Code Playgroud)