设置选择 字体颜色的时候遇到了问题
  当我把 
JTextComponent 
中的一部分选择后,然后我打开
JColorChooser
color=chooser.SelectColor();

 textComp.setSelectedTextColor(color);
但是不行
同样我使用 textComp.setSelectionColor(color)设置接下来选择的字体颜色也不行
郁闷,,,
因为我在打开 了jcolorchoose的时候
jtextcomponent中的所选择的 文字
又变成了未选择状态了
  我想使用多线程
可以不知道怎么设计,哎...
  有哪位知道 这个怎么解决吗?

解决方案 »

  1.   

    JDK下的示例
    demo\jfc\Stylepad
      

  2.   

    他那太有局限性了
    他只限于手动一个一个的添加 
    他利用 StyledEditorKit.ForegroundAction()
     但是我想 自己去选择 所有的颜色
    能实现么?
      

  3.   

    我最希望的是 能向里面添加 HTML代码
    象  FreeTextBox 那样
      我知道这是可行的
    因为我在网上看到了一个这样的东东
    但是不知道怎么去实现 ):
      

  4.   

    我还知道可以用
    HTMLEditorKit
     插入html代码
      

  5.   

    需要说明的是:setSelectionColor仅仅是设置一个全局状态,
    即任何时候文本选定的颜色,不会设置当前所选定文字的状态javax.swing.text.StyledEditorKit.ForegroundAction,只是提供了一个简化封装,
    最终是使用StyledDocument.setCharacterAttributes来设置选定文字的状态,既:MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setForeground(attr, fg);
    setCharacterAttributes(editor, attr, false);protected final void setCharacterAttributes(JEditorPane editor, 
          AttributeSet attr, boolean replace) {
        int p0 = editor.getSelectionStart();
        int p1 = editor.getSelectionEnd();
        if (p0 != p1) {
    StyledDocument doc = getStyledDocument(editor);
    doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
        }
        StyledEditorKit k = getStyledEditorKit(editor);
        MutableAttributeSet inputAttributes = k.getInputAttributes();
        if (replace) {
    inputAttributes.removeAttributes(inputAttributes);
        }
        inputAttributes.addAttributes(attr);
    }另外弹出对话框应该不会影响选择状态(可试用一下打开对话框),
    即便影响,可在对话框前使用getSelectionStart,getSelectionEnd保存状态
      

  6.   

    对你的程序没弄懂
    1,getStyledDocument(editor);
    我看了下api 
    在  abstract  StyledEditorKit.StyledTextAction 中
      我怎么使用?
    我对extends StyledEditorKit.StyledTextAction  实现 actionpeformed 2,  还是没看懂
    阁下能否把代码写得详细点啊
      

  7.   

    在你的JTextComponent 加上一个FocusListener。
    在失去焦点的时候,重新设置选择部分可视。因为默认的时候,失去焦点的时候,选择部分内容变成不特殊显示。下面的例子中的logTextPane即JTextComponent实例。    class EditorFocusListener implements FocusListener {        /* (non-Javadoc)
             * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
             */
            public void focusGained(FocusEvent e) {
                logTextPane.getCaret().setSelectionVisible(true);
            }        /* (non-Javadoc)
             * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
             */
            public void focusLost(FocusEvent e) {
                logTextPane.getCaret().setSelectionVisible(true);
            }    }
      

  8.   

    我照楼上的说了做了
    但是 
    我 textComp.setSelectedTextColor(colorChooser.getColor())
    对我选择的文字的颜色没有起到作用!!
     怪
      

  9.   

    奇怪了
    protected final void setCharacterAttributes(JTextPane textComp,
                                                  AttributeSet attrs, boolean replace) {
            Color defaultColor=textComp.getCaretColor();
                JColorChooser jc=new JColorChooser();
                jc.showDialog(InsertHTML.this,"choose color",defaultColor);
                Color color=jc.getColor();         StyleConstants.setForeground(attr,color );
                int p0 = textComp.getSelectionStart();
                int p1 = textComp.getSelectionEnd();
                if (p0 != p1) {
                    StyledDocument doc = editor.getStyledDocument();
                    doc.setCharacterAttributes(p0, p1 - p0, attrs, replace);
                }
                StyledEditorKit k = ( StyledEditorKit)editor.getEditorKit();
                MutableAttributeSet inputAttributes = k.getInputAttributes();
                if (replace) {
                    inputAttributes.removeAttributes(inputAttributes);
                }
                inputAttributes.addAttributes(attrs);
            }我这样手动去选择颜色的时候,最后字体的颜色 变成了JTextPane()  的背景颜色
    文字都看不到了
    但是我把
    StyleConstants.setForeground(attr,color ); 改为
    StyleConstants.setForeground(attr,Color.RED );的时候可以把文字颜色设置为红色
    怪!
    而且不管我boolean 置为true or false 还是一样
      

  10.   

    我刚好有段代码import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import java.util.Properties;
    import java.io.File;
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.sql.PreparedStatement;
    import java.text.DateFormat;public class Complete {
        public String framework = "embedded";
        public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
        public String protocol = "jdbc:derby:";
        protected static Properties props;
        public Complete() {
            try {
                Class.forName(driver).newInstance();
                props = new Properties();
                props.put("user", "jesse");
                props.put("password", "jesse");
            } catch (Exception ee) {
                ee.printStackTrace(System.err);
            }    }
        public void createTable() throws SQLException {
           Connection conn = DriverManager.getConnection(protocol +
                       "joybog", props);
           try {           Statement statement = conn.createStatement();
               File file = new File("bog.sql");
               StringBuffer sqlSB = new StringBuffer();           try {
                   FileInputStream fileInputStream = new FileInputStream(file);
                   int b;
                   while ((b = fileInputStream.read()) != -1) {
                       sqlSB.append((char) b);
                   }
               } catch (IOException ee) {
               }
               System.out.println(sqlSB.toString());
              // statement.execute("DROP table bog");
               statement.execute(sqlSB.toString());
           } catch (Exception ee) {
               ee.printStackTrace(System.err);
           }finally{
               conn.close();
           }
       }
       public void getContent() throws Exception {
            Connection conn = DriverManager.getConnection(protocol +
                        "joybog", props);
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery("select *  from bog");
            while (rs.next()) {
                System.out.print(rs.getInt("id") + "  " + rs.getString("title")
                                 + " " + rs.getString("content") + " " + " " +
                                 rs.getString("date")
                                 + rs.getInt("private"));
                System.out.println("");
            }
            conn.close();
        }
        public void addArcticle(String title, String content, boolean p,
                                String media, String type) throws SQLException {
            Connection conn = DriverManager.getConnection(protocol +
                    "joybog", props);
            String sql = "insert into bog(title,content,date,private,media,type) values(?,?,?,?,?,?)";
            int pi;
          //  int id = 1;
            try {           if (p == true) {
                   pi = 1;
                } else {
                    pi = 0;
                }
                java.util.Date d = new java.util.Date();
                DateFormat df = DateFormat.getDateInstance();
                PreparedStatement ps = conn.prepareStatement(sql);
              
                ps.setString(1, title);
                ps.setString(2, content);
                ps.setString(3, df.format(d).toString());
                ps.setInt(4, pi);
                ps.setString(5, media);
                ps.setString(6, type);
                ps.execute();        } catch (Exception ee) {
                ee.printStackTrace(System.err);
            } finally {
                conn.close();
            }
        }   public static void main(String[] args)throws Exception{
           Complete cm=new Complete();
          // cm.createTable();
        cm.addArcticle("jesse's first bog title","the content",false,"alizee.avi","nothing");
           cm.getContent();
       }
    }
      

  11.   

    一个简单的示例:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.text.*;
    import javax.swing.*;public class test extends JFrame
    {
    DefaultStyledDocument doc;
    JTextPane editor;
    public test()
    {
    doc = new DefaultStyledDocument();
        editor = new JTextPane(doc);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    JButton btn = new JButton("color");
        btn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
                int p0 = editor.getSelectionStart();
                int p1 = editor.getSelectionEnd();            if (p0 != p1) {
    JColorChooser jc=new JColorChooser();
    Color color=jc.showDialog(test.this, "choose color", Color.red);
    System.out.println(color); MutableAttributeSet attr = new SimpleAttributeSet();
    StyleConstants.setForeground(attr, color);                doc.setCharacterAttributes(p0, p1 - p0, attr, false);
                }
    }
    }); add(btn, "North");
    add(editor, "Center");    setSize(600, 480);
    }
       public static void main(String args[]) {
         new test().setVisible(true);
       }
    }
      

  12.   


    太傻了
    原来是我使用的是 Color color=colorChooser.getColor()
     ):
        哎
      太不仔细了
    太不应该了
    向楼上的大哥学习....努力努力再努力!!!!
     结帐了
    呵呵