使用 JColorChooser 设置文本颜色
我正在尝试使用 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);
}
});
关于如何让它发挥作用有什么建议吗?或者更好的方法?
谢谢
I'm trying to make a text editor with a JTextPane, but i'm having trouble with setting the selected texts color. Here is the best could come up with (but, obviously, not working):
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);
}
});
Any suggestions on how to get that to work? Or a better method of doing it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getSelectedText()
仅返回包含所选文本的普通字符串;您不能使用它来修改文本的属性。我首先使用
SimpleAttributeSet
和StyleConstants
生成颜色属性,然后将其应用到文本的选定部分:getSelectedText()
just returns a normal string containing the selected text; you cannot use it to modify the attributes of the text.I would start by using
SimpleAttributeSet
andStyleConstants
to generate the colour attribute, then apply it to the selected portion of your text: