Java 如何找到JTextPane文档的默认属性?

Java 如何找到JTextPane文档的默认属性?,java,swing,jtextpane,Java,Swing,Jtextpane,我正在使用JTextPane JTextPane pane = new JTextPane(); String content = "I'm a line of text that will be displayed in the JTextPane"; StyledDocument doc = pane.getStyledDocument(); SimpleAttributeSet aSet = new SimpleAttributeSet(); 如果我将此aSet添加到文本窗格的文档中,如

我正在使用JTextPane

JTextPane pane = new JTextPane();
String content = "I'm a line of text that will be displayed in the JTextPane";
StyledDocument doc = pane.getStyledDocument();
SimpleAttributeSet aSet = new SimpleAttributeSet();
如果我将此
aSet
添加到文本窗格的文档中,如下所示:

doc.setParagraphAttributes(0, content.length(), aSet, false);
doc.setParagraphAttributes(0, content.length(), aSet, true);
什么都看不见。没什么大不了的,因为我还没有为
aSet
设置任何自定义属性。但是,如果我允许
aSet
替换
doc
的当前段落属性,如下所示:

doc.setParagraphAttributes(0, content.length(), aSet, false);
doc.setParagraphAttributes(0, content.length(), aSet, true);
发生了很多事情。如何获取关于JTextPane文档的默认值的信息?特别是我的问题是,当我为
aSet
定义自定义字体并将其设置为替换当前属性时,字体显示为粗体<代码>StyleConstants.setBold(aSet,false)没有帮助。

我已经查看了,以查看哪些数据结构包含您想要的信息。这是对该代码的修改,用于打印每个段落的属性

int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call

Element section = doc.getDefaultRootElement();
int index0 = section.getElementIndex(offset);
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
for (int i = index0; i <= index1; i++)
{
    Element paragraph = section.getElement(i);
    AttributeSet attributeSet = paragraph.getAttributes();
    Enumeration keys = attributeSet.getAttributeNames();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object attribute = attributeSet.getAttribute(key);
        //System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold"
        System.out.println(attribute.getClass());
        System.out.println(attribute);
    }
}
关于您的特殊问题,请看a,我已经能够将一段文字设置为粗体:

StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sc.addAttribute(aSet,  StyleConstants.Bold,  true);
在这种情况下,
aSet
的类是
javax.swing.text.StyleContext$SmallAttributeSet
,它是不可变的(不实现
MutableAttributeSet
)。就您的情况而言,大致如下:

aSet.addAttribute(StyleConstants.Bold, true);
应该有用。

我已经查看了,以查看哪些数据结构包含了您想要的信息。这是对该代码的修改,用于打印每个段落的属性

int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call

Element section = doc.getDefaultRootElement();
int index0 = section.getElementIndex(offset);
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
for (int i = index0; i <= index1; i++)
{
    Element paragraph = section.getElement(i);
    AttributeSet attributeSet = paragraph.getAttributes();
    Enumeration keys = attributeSet.getAttributeNames();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object attribute = attributeSet.getAttribute(key);
        //System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold"
        System.out.println(attribute.getClass());
        System.out.println(attribute);
    }
}
关于您的特殊问题,请看a,我已经能够将一段文字设置为粗体:

StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sc.addAttribute(aSet,  StyleConstants.Bold,  true);
在这种情况下,
aSet
的类是
javax.swing.text.StyleContext$SmallAttributeSet
,它是不可变的(不实现
MutableAttributeSet
)。就您的情况而言,大致如下:

aSet.addAttribute(StyleConstants.Bold, true);
应该有用。

我已经查看了,以查看哪些数据结构包含了您想要的信息。这是对该代码的修改,用于打印每个段落的属性

int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call

Element section = doc.getDefaultRootElement();
int index0 = section.getElementIndex(offset);
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
for (int i = index0; i <= index1; i++)
{
    Element paragraph = section.getElement(i);
    AttributeSet attributeSet = paragraph.getAttributes();
    Enumeration keys = attributeSet.getAttributeNames();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object attribute = attributeSet.getAttribute(key);
        //System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold"
        System.out.println(attribute.getClass());
        System.out.println(attribute);
    }
}
关于您的特殊问题,请看a,我已经能够将一段文字设置为粗体:

StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sc.addAttribute(aSet,  StyleConstants.Bold,  true);
在这种情况下,
aSet
的类是
javax.swing.text.StyleContext$SmallAttributeSet
,它是不可变的(不实现
MutableAttributeSet
)。就您的情况而言,大致如下:

aSet.addAttribute(StyleConstants.Bold, true);
应该有用。

我已经查看了,以查看哪些数据结构包含了您想要的信息。这是对该代码的修改,用于打印每个段落的属性

int offset, length; //The value of the first 2 parameters in the setParagraphAttributes() call

Element section = doc.getDefaultRootElement();
int index0 = section.getElementIndex(offset);
int index1 = section.getElementIndex(offset + ((length > 0) ? length - 1 : 0));
for (int i = index0; i <= index1; i++)
{
    Element paragraph = section.getElement(i);
    AttributeSet attributeSet = paragraph.getAttributes();
    Enumeration keys = attributeSet.getAttributeNames();
    while (keys.hasMoreElements())
    {
        Object key = keys.nextElement();
        Object attribute = attributeSet.getAttribute(key);
        //System.out.println("key = " + key); //For other AttributeSet classes this line is useful because it shows the actual parameter, like "Bold"
        System.out.println(attribute.getClass());
        System.out.println(attribute);
    }
}
关于您的特殊问题,请看a,我已经能够将一段文字设置为粗体:

StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aSet = sc.addAttribute(aSet,  StyleConstants.Bold,  true);
在这种情况下,
aSet
的类是
javax.swing.text.StyleContext$SmallAttributeSet
,它是不可变的(不实现
MutableAttributeSet
)。就您的情况而言,大致如下:

aSet.addAttribute(StyleConstants.Bold, true);

应该有用。

要设置aSet的属性,我使用
StyleConstants.setFontFamily(aSet,“Times New Roman”),这很好:)。您的回答告诉我,我认为粗体文本是颜色属性(前景)从默认值
[r=51,g=51,b=51]
重置为
[r=0,g=0,b=0]
。非常感谢,非常有帮助@用户2651804很高兴我能帮忙!请注意,
StyleConstants.setXXX(aMutableSet,value)
方法存在于comodity中,它们只调用
aMutableSet.addAttribute(StyleConstants.XXX,value)
。我想,这些旨在让我们的生活更轻松的东西最终会使新java程序员的生活变得复杂……为了设置aSet的属性,我使用
StyleConstants.setFontFamily(aSet,“Times new Roman”),这很好:)。您的回答告诉我,我认为粗体文本是颜色属性(前景)从默认值
[r=51,g=51,b=51]
重置为
[r=0,g=0,b=0]
。非常感谢,非常有帮助@用户2651804很高兴我能帮忙!请注意,
StyleConstants.setXXX(aMutableSet,value)
方法存在于comodity中,它们只调用
aMutableSet.addAttribute(StyleConstants.XXX,value)
。我想,这些旨在让我们的生活更轻松的东西最终会使新java程序员的生活变得复杂……为了设置aSet的属性,我使用
StyleConstants.setFontFamily(aSet,“Times new Roman”),这很好:)。您的回答告诉我,我认为粗体文本是颜色属性(前景)从默认值
[r=51,g=51,b=51]
重置为
[r=0,g=0,b=0]
。非常感谢,非常有帮助@用户2651804很高兴我能帮忙!请注意,
StyleConstants.setXXX(aMutableSet,value)
方法存在于comodity中,它们只调用
aMutableSet.addAttribute(StyleConstants.XXX,value)
。我想,这些旨在让我们的生活更轻松的东西最终会使新java程序员的生活变得复杂……为了设置aSet的属性,我使用
StyleConstants.setFontFamily(aSet,“Times new Roman”),这很好:)。您的回答告诉我,我认为粗体文本是颜色属性(前景)从默认值
[r=51,g=51,b=51]
重置为
[r=0,g=0,b=0]
。非常感谢,非常有帮助@用户2651804很高兴我能帮忙!请注意,
StyleConstants.setXXX(aMutableSet,value)
方法存在于comodity中,它们只调用
aMutableSet.addAttribute(StyleConstants.XXX,value)
。我想,这些旨在让我们的生活更轻松的东西最终会使新java程序员的生活变得复杂。。。