Alm*_*ani 14 java swing jtextpane
任何人都可以帮我简单的日志,我必须在第一行添加选择颜色的JTextPane日志消息(绿色确定,红色失败).怎么做到这一点?
Snu*_*kus 32
这将打印出两种不同颜色的"BLAH BLEG".
public class Main {
public static void main(String[] args) {
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.addStyle("I'm a Style", null);
StyleConstants.setForeground(style, Color.red);
try { doc.insertString(doc.getLength(), "BLAH ",style); }
catch (BadLocationException e){}
StyleConstants.setForeground(style, Color.blue);
try { doc.insertString(doc.getLength(), "BLEH",style); }
catch (BadLocationException e){}
JFrame frame = new JFrame("Test");
frame.getContentPane().add(textPane);
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
看这里:样式教程
并查看标记为"使用文本窗格的示例 "部分,以获取有关如何动态更改颜色的一个很好的示例.