Color selectedColor=jc.showDialog(this,"zb",jTextArea1.getSelectedTextColor());
jTextArea1.setSelectedTextColor(selectedColor);设置的是被选中文本颜色,改变选中状态后仍恢复至原颜色。不知是否是你想要的效果?

解决方案 »

  1.   

    JColorChooser jc=new JColorChooser();
    Color selectedColor=jc.showDialog(this,"zb",jTextPane1.getSelectedTextColor());
    new StyledEditorKit.ForegroundAction("no-name", selectedColor).actionPerformed(e);
    用JTextPane替代JTextArea
      

  2.   

    看不明白,为什么要用JTextPane替代JTextArea
    用JTextArea不能实现吗,怎么实现?
    还有一个问题就是我选中一段文本后,单击
    JColorChooser对话框时,选中的地方又变成没选中,
    怎么解决啊,帮帮忙啊
      

  3.   

    JTextArea can display and edit multiple lines of text. Although a text area can display text in any font, all of the text is in the same font. Use a text area to allow the user to enter unformatted text of any length or to display unformatted help information. 
    你试一下:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;public class TestForYou{
    public static void main(String[] args){
    final JFrame f = new JFrame();
    final JButton b = new JButton("button");
    final JTextPane t = new JTextPane();

    b.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    JColorChooser jc=new JColorChooser();
    Color selectedColor=jc.showDialog(f,"zb",t.getSelectedTextColor());
    new StyledEditorKit.ForegroundAction("no-name", selectedColor).actionPerformed(e);
    }
    });

    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(b,BorderLayout.NORTH);
    f.getContentPane().add(t,BorderLayout.CENTER);
    f.setSize(200,200);
    f.setVisible(true);


    }
    }
    先输入一段文本,选一部分,点Button,返回后选中文本已改色,按tab切换至textpane仍为原选中状态。
      

  4.   

    只能用JTextPane不能用JTextArea吗,
    我想用JTextArea实现怎么办?