如何在JTextPane中轻松编辑所选文本的样式? [英] How do I easily edit the style of the selected text in a JTextPane?
问题描述
如何在JTextPane中轻松编辑所选文本的样式?似乎没有很多资源.即使您可以在这方面为我提供很好的参考资源,我也将非常感激.
How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.
此外,如何获取所选文本的当前样式?我尝试了styledDoc.getLogicalStyle(textPane.getSelectionStart());
,但似乎没有用.
Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart());
but it doesn't seem to be working.
推荐答案
在此pastebin中查看以下代码:
Take a look at the following code in this pastebin:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed
示例从这里:
http://www.java2s.com/Code/Java /Swing-JFC/JTextPaneStylesExample3.htm
您似乎可以在动作侦听器中使用以下命令来更改样式:
It looks like you can change the style using the following in an action listener:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
它将给定的偏移量和长度之间的文本样式设置为特定样式.
It sets the style of the text between the given offset and length to a specific style.
有关更多详细信息,请参见完整的pastebin.那样应该可以解决您的问题.
See the full pastebin for more details. That should fix your problem though.
这篇关于如何在JTextPane中轻松编辑所选文本的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!