【问题标题】:Why doesn't my code correctly update the jtextpane font colour?为什么我的代码没有正确更新 jtextpane 字体颜色?
【发布时间】:2016-03-15 20:01:08
【问题描述】:
Font displayFont = new Font(Font.SANS_SERIF, Font.BOLD, 18);
        WindowManager.getInstance().getConsoleWindow().getTextArea().setFont(displayFont);
        WindowManager.getInstance().getConsoleWindow().getTextArea().setForeground(Color.BLUE);

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

【问题讨论】:

  • 也许 setForeground 不是正确的调用方法
  • 我不确定可以尝试哪些其他方法?当我调用构造函数时,setForeground 似乎在其他地方工作,但是当我尝试使用它来更改初始颜色时它不起作用
  • @Cypher236 看看我的解决方案,让我知道它是否能解决您的问题。
  • @Cypher236 我试过了,setForeground() 能够以不同的颜色显示所有文本。如果您只想更改特定文本的颜色,可以使用StyledDocument
  • @user3437460 好的,谢谢,我想我可能已经修复了它,现在稍微调整一下您的代码 - 感谢您的帮助!

标签: java swing jtextpane


【解决方案1】:

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

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

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

【讨论】:

  • @Cypher236 嗯,我看看能不能写个小演示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多