我正在尝试使用JTextPane创建文本编辑器,但在设置所选文本的颜色时遇到问题。下面是我能想到的最好的方法(但显然是行不通的):
JMenuItem button = new JMenuItem("Set Color");
toolbar.add(button);
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent e) {
Color c = JColorChooser.showDialog(frame,"Choose a color", getBackground());
textPane.getSelectedText().StyledEditorKit.ForegroundAction("color",c);
}
});对如何让它工作有什么建议吗?或者是一种更好的方法?
谢谢
发布于 2010-11-21 03:33:38
getSelectedText()只返回包含所选文本的普通字符串;您不能使用它来修改文本的属性。
我将首先使用SimpleAttributeSet和StyleConstants生成颜色属性,然后将其应用于文本的选定部分:
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, c);
textPane.setCharacterAttributes(attr, false);https://stackoverflow.com/questions/4234508
复制相似问题