使文本在JTextPane中变为粗体并着色时出错

使文本在JTextPane中变为粗体并着色时出错

本文介绍了使文本在JTextPane中变为粗体并着色时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用将文本输出为HTML的JTextPane创建文本编辑器。我想包括选项以粗体,颜色和对齐文本。我遇到了一个问题,即更改了粗体和粗体混合文本的选择颜色。例如,如果我在编辑器中使用粗体文本不是粗体文本的文本,并且突出显示两个单词并尝试更改文本的颜色,则会更改颜色并使所有文本大胆,而不是让第二个单词不粗体。所以我不确定这个问题来自哪里,如果它与setCharacterAttributes有关,或者它是将文本输出为html的问题。这里是我的一些代码,使文本加粗:

I am trying to create a text editor using a JTextPane that outputs the text as HTML. I want to include the options to bold, color and align the text. I am having an issue with changing the color of a selection of text that is a mix of bold and not bold. For example, if I have the text in the editor "bold text not bold text" and I highlight both words and try to change the color of the text, it changes the color and makes all of the text bold, instead of leaving the second word not bold. So I'm not sure where this issue is coming from, if it has to do with setCharacterAttributes or if it's a problem with outputing the text as html. here is some of my code that makes the text bold:

    MutableAttributeSet attrs = pane.getInputAttributes();
    StyleConstants.setBold(attrs, bold);
    pane.getStyledDocument().setCharacterAttributes(p.getSelectionStart(),len,attrs,false);

下面是我更改文本颜色之前和之后html输出的示例。这就是身体内部的标签

Here is an example of what the html output looks like before and after I change the color of the text. This is just whats inside of the body tags

之前:
粗体文本不是粗体文本

将颜色更改为红色后:
粗体文本不是粗体文本

After change color to red:bold text not bold text

它有一定的作用无论选择的第一个单词是否为粗体。如果我做了同样的例子,但第二部分是大胆的,第一部分不是大胆的,那么它工作的很好。所以当文本选择的开始是粗体时,这与处理有关。

And it has something to do with whether or not the first word of the selection is bold. If I did the same example, but the 2nd part was bold and the first part was not bold, then it works fine. So it has something to do with when the beginning of the text selection is bold.

推荐答案

允许您取消文本?

创建并应用合适的 SimpleAttributeSet ,例如正常,看过。

Create and apply a suitable SimpleAttributeSet such as normal, seen here.

这篇关于使文本在JTextPane中变为粗体并着色时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:29