如何在Java中为选定的文本设置颜色?

问题描述:

请任何人帮助您仅设置所选文本的颜色...我已经创建了一个简单文本编辑器...但是,我无法设置所选文本内容的颜色...一次,我已经选择了颜色会影响整个文本区域,而不是选定区域.

Please any one help for set the color for selected text only... I has Create a Simple Text Editor... But, I can't set the color for selected text contents... once ,I has select the color it will affect the whole text area instead of selected area.

请帮助任何人,

预先感谢.

for Example : 

现在我仅选择Kumar.因此,将仅为所选文本选择颜色..但是,我的问题是也更改了未选中文本的颜色.

Now I select the Kumar only..So,will select the color for that selected text only..But,my problem is change the color for unselected text also..

如何解决???????

How to resolve it???????

您使用什么组件显示文本? 如果它是JTextArea,则不可能. 您需要一个允许使用不同样式的组件.例如,JTextPane中的StyledDocument. 有关更多信息,请参见如何使用编辑器窗格和文本窗格

With what component do you display your text? If it is JTextArea, then it is not possible. You need a component that allows different styles. For example a StyledDocument in a JTextPane. For more information see How to Use Editor Panes and Text Panes