java - 为什么我的代码没有正确更新 jtextpane 字体颜色?

标签 java swing jtextpane

Font displayFont = new Font(Font.SANS_SERIF, Font.BOLD, 18);
        WindowManager.getInstance().getConsoleWindow().getTextArea().setFont(displayFont);
        WindowManager.getInstance().getConsoleWindow().getTextArea().setForeground(Color.BLUE);

上面是我的代码片段,负责在单击按钮时更改 jtextpane 中文本的属性。文本正确更新为更大和粗体,但它不会改变颜色,我不知道为什么。提前致谢。

最佳答案

您可能想要这样做,而不是直接在前台属性上设置:

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

Style style = textPane.addStyle("Blue", null);
StyleConstants.setForeground(style, Color.blue);

关于java - 为什么我的代码没有正确更新 jtextpane 字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36010455/

相关文章:

java - 推荐的java代码

java - java中setFocusable的使用

Java JTextPane + JScrollPane : de/activate automatic scrolling

Java JTextPane 斜体帮助

java - JTextPane 不换行?

java - 如何获取与 firebase 数据库中的特定值配对的随 secret 钥并删除该值?

java - 如何让 Google GIN 在 DevMode 下与 Google-Eclipse 插件配合使用?

java - 在 Java 中将毫秒字符串转换为日期

java - 尝试将一个类制作成二维数组

Java - 使用文本区域将 JTable 设置为不可编辑