【发布时间】:2011-08-01 08:19:45
【问题描述】:
我一直在编写的应用程序中使用 JTextPane(或我的子类版本),因此我一直在使用 Styles。我的程序没有按照我想要的方式运行,我跟踪了我认为是 AttributeSet.containsAttributes(AttributeSet attributes) 方法中的错误的行为。我写了以下简短的程序来说明它:
import javax.swing.JTextPane;
import javax.swing.text.StyleConstants;
import javax.swing.text.Style;
public class StyleBug {
public static void main(String[] args) {
JTextPane textPane = new JTextPane();
textPane.setText("This is a test string");
Style bold = textPane.addStyle(BOLD, null);
StyleConstants.setBold(bold, true);
Style italic = textPane.addStyle(ITALIC, null);
StyleConstants.setItalic(italic, true);
int start = 5;
int end = 10;
textPane.getStyledDocument().setCharacterAttributes(start, end - start, textPane.getStyle(BOLD), false);
textPane.getStyledDocument().setCharacterAttributes(start, end - start, textPane.getStyle(ITALIC), false);
for(int i = start; i < end; i++)
System.out.println(textPane.getStyledDocument().getCharacterElement(i).getAttributes()
.containsAttributes(textPane.getStyle(BOLD))); //all print false
}
private static final String BOLD = "Bold";
private static final String ITALIC = "Italic";
}
这是一个错误,还是我在这里遗漏了什么?
【问题讨论】:
-
您指的是静态初始化吗?
-
textPane.getStyle(BOLD) 和 textPane.getStyle(ITALIC),
-
@Mikaveli,我指的错误是 containsAttributes 方法说文本不是粗体,即使我应用了具有 Bold 属性的样式。
标签: java swing coding-style jtextpane