Java AttributeSet在使用StyleConstants.Bold时引发错误

Java AttributeSet在使用StyleConstants.Bold时引发错误,java,swing,awt,jtextpane,Java,Swing,Awt,Jtextpane,考虑以下代码: import java.awt.*;import javax.swing.*;import javax.swing.text.*; public class Main extends JFrame { private final StyleContext cont = StyleContext.getDefaultStyleContext(); private final AttributeSet normal = cont.addAttribute(cont.g

考虑以下代码:

import java.awt.*;import javax.swing.*;import javax.swing.text.*;
public class Main extends JFrame {
    private final StyleContext cont = StyleContext.getDefaultStyleContext();
    private final AttributeSet normal = cont.addAttribute(cont.getEmptySet(),StyleConstants.Foreground,Color.BLACK);
    private final AttributeSet bold = cont.addAttribute(cont.getEmptySet(),StyleConstants.Bold,Color.BLACK);
    public Main() {
        setSize(800,600);
        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout()); // this is used to make the textpane take up the entire space
        getContentPane().add(pane); // adds pane to the frame
        JTextPane textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();
        try {
            doc.insertString(0,"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",normal/*change to bold for error*/);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        pane.add(textPane,BorderLayout.CENTER);
    }
    public static void main(String[] args) {
        Main main = new Main();
        main.setVisible(true);
    }
}
当我使用
AttributeSet
normal
将文本插入我的
JTextPane
时,代码会正常运行并获得所需的结果。但是,当我尝试使用
属性集
粗体
(请参见第14行的注释)插入文本时,我得到了以下
ClassCastException

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.Color cannot be cast to java.lang.Boolean
    at javax.swing.text.StyleConstants.isBold(StyleConstants.java:399)
    at javax.swing.text.StyleContext.getFont(StyleContext.java:188)
    at javax.swing.text.DefaultStyledDocument.getFont(DefaultStyledDocument.java:943)
    at javax.swing.text.LabelView.setPropertiesFromAttributes(LabelView.java:145)
    at javax.swing.text.LabelView.sync(LabelView.java:56)
    at javax.swing.text.LabelView.getFont(LabelView.java:208)
    at javax.swing.text.GlyphPainter1.sync(GlyphPainter1.java:222)
    at javax.swing.text.GlyphPainter1.getSpan(GlyphPainter1.java:59)
    at javax.swing.text.GlyphView.getPreferredSpan(GlyphView.java:592)
    at javax.swing.text.FlowView$LogicalView.getPreferredSpan(FlowView.java:732)
    at javax.swing.text.FlowView.calculateMinorAxisRequirements(FlowView.java:233)
    at javax.swing.text.ParagraphView.calculateMinorAxisRequirements(ParagraphView.java:717)
    at javax.swing.text.BoxView.checkRequests(BoxView.java:935)
.
.
. (The exception is very long, so tell me if you need the full trace)

我做错了什么?

不要试图在一条语句中创建属性

通过执行以下操作,使代码更易于理解和维护:

SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
StyleConstants.setBackground(error, Color.YELLOW);
StyleConstants.setBold(error, true);

...

doc.insertString(0, "...", error);

不要试图在一条语句中创建属性

通过执行以下操作,使代码更易于理解和维护:

SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
StyleConstants.setBackground(error, Color.YELLOW);
StyleConstants.setBold(error, true);

...

doc.insertString(0, "...", error);