JtextPane - 无法大胆

时间:2013-11-25 03:19:47

标签: java swing jtextpane

我正在尝试 BOLD JPane中的文字,但它无效。如果 ITALIC UNDERLINE ,其工作正常。以下是我的代码。

String strText = "<b><i><u>Testing</b></i></u>";
javax.swing.text.Style style1 = jTextPane1.addStyle("I'm a Style", null);
StyleConstants.setForeground(style1, Color.BLACK);
StyleConstants.setFontSize(style1, 15);
StyleConstants.setFontFamily(style1, "Arial Unicode MS");
if(strText.contains("<b>"))
                        {
                            StyleConstants.setBold(style1, true);
                            strText = strText.replace("<b>", " ");
                            strText = strText.replace("</b>", " ");

                        }
                        if(strText.contains("<i>"))
                        {
                            StyleConstants.setItalic(style1, true);
                            strText = strText.replace("<i>", " ");
                            strText = strText.replace("</i>", " ");

                        }
                        if(strText.contains("<u>"))
                        {
                            StyleConstants.setUnderline(style1, true);
                            strText = strText.replace("<u>", " ");
                            strText = strText.replace("</u>", " ");

                        }
try {
strText = strText.trim();
doc.insertString(doc.getLength(), strText, style1);
} 

UNDERLINE ITALIC 工作正常,同时 BOLD 无法正常工作。文字不是 BOLDED 。请告诉我哪里犯了错误。

1 个答案:

答案 0 :(得分:3)

基于此example,下面的片段说明了基于相同字体和大小的三种相关样式:

image

SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "SansSerif");
StyleConstants.setFontSize(normal, 16);

SimpleAttributeSet bold = new SimpleAttributeSet(normal);
StyleConstants.setBold(bold, true);

SimpleAttributeSet italic = new SimpleAttributeSet(normal);
StyleConstants.setItalic(italic, true);

doc.insertString(doc.getLength(), s + "\n", normal);
doc.insertString(doc.getLength(), s + "\n", bold);
doc.insertString(doc.getLength(), s + "\n", italic);