用JTextPane做了一个客户端文本编辑器,进行文字着色,然后把着色后的文字保存到服务器,保存后再打开文本编辑器,只要拷贝JTextPane内的文本,就会出现很多的换行,打开是这样的情况:
var year = document.getElementById("YEAR");
var month = document.getElementById("MONTH");
var yData =[2006, 2005, 2004, 2003, 2002, 2001, 1999];
在记事本中打开,这些换行就变成了一大串黑色的竖线,在UltraEdit里面转换成十六进制显示出是很多0D后面只有一个0A(0D 0D 0D 0D 0D 0D......0A)

解决方案 »

  1.   

    // 保存控件的参数
    AbstractAction saveAction = new AbstractAction("保存") {
    public void actionPerformed(ActionEvent e) {
    String text = m_tValue.getText();
    WidgetDirector.changeProperty(m_nKey, text);
    }
    };
    addIconAction(toolbar, "save.gif", saveAction);其中WidgetDirector是一个类,在这里类里面写的保存,那就是另一个人的工作了我也不知道里面写的什么
      

  2.   

    你做一下如下处理:
    1、String text = m_tValue.getText();
    System.out.println(text);
    看一下你取到的JTextPane中的文本是什么2、用WidgetDirector给你的方法读取已经保存的文本数据
    再System.out.println(text);打印到控制台
    看看读到的是什么比较一下这两个文本是否一致
    如果不一致
    可以确定是WidgetDirector的问题
    把问题交给WidgetDirector类的开发人员解决如果一致
    再查看后面的代码
      

  3.   

    我敢肯定就是我的代码有问题,首先:JTextPane可以进行文本着色,第二,在没有着色以前是使用JTextArea没有出现上面换行的问题
      

  4.   

    String text = m_tValue.getText();
    System.out.println(text);
    打印出来的不是你想要的?
      

  5.   

    String text = m_tValue.getText();
    System.out.println(text);
    这个打印是正常的!
    同样还出现几个问题,我如果在JTextPane里面用鼠标拖拉复制文本,粘贴到记事本中就会出现问题,如果用键盘选择复制文本就会出问题,还有,一个换行应该是 0D 0A 我复制出来的文本就有很多个 0D 到下一行文本前才会出现一个 0A
    另一个,您说的"他的代码中应该写入了换行",但是在以前使用JTextArea的时候就没有这个问题,我只是把JTextArea换成JTextPane,然后进行文本着色.
      

  6.   

    是这样
    JTextPane里的文本是有格式的文本
    以文本的形式(如txt文件)保存是无法将其字体、颜色等属性保存下来的
    拷贝或者拖拉之记事本中也是一样无法显示正确格式
    格式字符都会显示成乱码建议你将JTextPane中的文本保存成rtf格式文件
    对于rtf格式文本的写入和读取
    你可以参考javax.swing.text.rtf包下相关类
    如 RTFEditorKit、StyleContext等祝你好运:)
      

  7.   

    或者
    如果存储JTextPane的格式化文本只是为了以后再读入显示
    也可以选择将JTextPane的实例序列化到本地硬盘
    需要时再反序列化回来
    这样可能跟直接一些:)
      

  8.   

    我并没有对着色后的文本进行保存,保存的还是没有着色的文本,只是在打开JTextPane的时候,点回车和鼠标的焦点切换的时后判断文本是否更改,如果更改,进行着色.也就是说,我这个文本编辑器只是一个视觉效果.
      

  9.   

    着不着色 JTextPane都已经对文本进行了格式化
    所以 你保存成纯文本文件后
    就没有办法正常读取格式了:)
      

  10.   

    我问了,服务器端是用binary格式保存的文本,不是txt
      

  11.   

    希望认识您,在这上面聊的太慢了,我的联系方式:QQ-28156841,[email protected].
    如果联系我,请您注明“csdn”。
    下面是全部代码,希望明天还可以看到您,我下班了!今天被这个问题搞的头疼!明天继续!
    非常感谢您的帮助!public class LargeDataDialog extends JDialog implements KeyListener { private JTextPane m_tValue; // 属性参数 private int m_nKey; // 当前属性 private int property; // 当前属性 // private JTextPane m_tValue; // 文本编辑框 private String regexString; // 语法正则表达式 private boolean changed = false; // 检查文档的内容是否改变 private boolean editing = false; // 判断是否正在编辑 private Style[] styles; // 编辑框样式表 private Set<String> keySet = new HashSet<String>(); // 关键字映射表 private String keywords = "break|case|catch|constructor|continue|default|do|"
    + "else|false|for|function|goto|if|in|new|null|prototype|return|switch|this|true|try|var|while|int"; private String objects = "alert|appendChild|Array|Boolean|"
    + "childNodes|createElement|createElementNS|createTextNode|"
    + "Date|document|Enumerator|Error|firstChild|Function|"
    + "getAttribute|getElementById|getElementsByTagName|Global|hasChildNodes|insertBefore|"
    + "lastChild|length|Math|nextSibling|nodeValue|Number|Object|parentNode|previousSibling|"
    + "RegExp|removeAttribute|removeChild|replaceChild|setAttribute|String|"
    + "window|XMLHttpRequest"; private String[] syntaxRegex = { "/\\*[\\s\\S]*?\\*/", // 多行注释
    "//.*", // 单行注释
    "\".*?\"", // 双引号字符串
    "\'.*?\'", // 单引号字符串
    "/.*?/", // 正则表达式
    "\\b(?:" + keywords + ")\\b", // 关键字
    "\\b(?:" + objects + ")\\b" // 内置对象
    }; public LargeDataDialog(Frame owner, String text, int key) {
    super(owner, true);
    setLayout(new BorderLayout());
    setSize(640, 480);
    setTitle("数据编辑器");
    setResizable(true);
    setLocationRelativeTo(null); // 在屏幕中央 m_nKey = key;
    // 创建工具栏
    JToolBar toolbar = new JToolBar("editor");
    toolbar.setFloatable(false);
    toolbar.setBorder(null);
    toolbar.setPreferredSize(new Dimension(240, 24));
    toolbar.setAlignmentX(Component.LEFT_ALIGNMENT); // 保存控件的参数
    AbstractAction saveAction = new AbstractAction("保存") {
    public void actionPerformed(ActionEvent e) {
    String text = m_tValue.getText();
    WidgetDirector.changeProperty(m_nKey, text);
    }
    };
    addIconAction(toolbar, "save.gif", saveAction); // 最大化窗口
    AbstractAction maxAction = new AbstractAction("全屏显示") {
    private boolean bMaximum = false; public void actionPerformed(ActionEvent e) {
    if (bMaximum)
    setBounds(192, 144, 640, 480);
    else
    setBounds(0, 0, 1024, 768);
    validate(); // 重绘窗口
    bMaximum = !bMaximum;
    }
    };
    addIconAction(toolbar, "max.gif", maxAction); // 剪贴板的操作
    AbstractAction copyAction = new AbstractAction("复制") {
    public void actionPerformed(ActionEvent e) {
    m_tValue.copy();
    }
    };
    addIconAction(toolbar, "copy.gif", copyAction); AbstractAction pasteAction = new AbstractAction("粘贴") {
    public void actionPerformed(ActionEvent e) {
    m_tValue.paste();
    }
    };
    addIconAction(toolbar, "paste.gif", pasteAction); AbstractAction cutAction = new AbstractAction("剪切") {
    public void actionPerformed(ActionEvent e) {
    m_tValue.cut();
    }
    };
    addIconAction(toolbar, "cut.gif", cutAction); // 设置工具栏的大小
    add(toolbar, BorderLayout.NORTH);
    m_tValue = new JTextPane();
    m_tValue.addKeyListener(this);
    m_tValue.addMouseListener(new attributeMouse());
    m_tValue.setText(text); Font font = new Font("Courier New", Font.PLAIN, 15);
    m_tValue.setFont(font);
    // m_tValue.setTabSize(4); // 设置Tab键的宽度为4
    add(new JScrollPane(m_tValue), BorderLayout.CENTER); // 语法着色
    StyleContext syc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(syc);
    doc.addDocumentListener(new documentListener());
    int p = m_tValue.getCaretPosition();
    try {
    doc.insertString(0, text, null);
    } catch (BadLocationException e1) {
    // TODO 自动生成 catch 块
    e1.printStackTrace();
    }
    m_tValue.setDocument(doc);
    // m_tValue.setTabSize(4);
    m_tValue.setBorder(null); // 设置默认样式
    Style defStyle = syc.getStyle(StyleContext.DEFAULT_STYLE);
    defStyle.addAttribute(StyleConstants.FontFamily, "Courier New");//
    // Courier New
    defStyle.addAttribute(StyleConstants.FontSize, 13); // 创建语法正则表达式
    final StringBuffer buffer = new StringBuffer(syntaxRegex[0]);
    for (int i = 1; i < syntaxRegex.length; i++) {
    buffer.append('|');
    buffer.append(syntaxRegex[i]);
    }
    regexString = buffer.toString(); // 创建关键字映射表
    String[] keyList = keywords.split("\\|");
    for (int i = 0; i < keyList.length; i++)
    keySet.add(keyList[i]); // 创建样式表
    styles = new Style[6];
    // 多行注释
    Style blockComment = syc.addStyle("blockComment", null);
    blockComment.addAttribute(StyleConstants.Foreground, new Color(0, 128,
    128));
    // Create and add the style
    blockComment.addAttribute(StyleConstants.FontSize, new Integer(16));
    blockComment.addAttribute(StyleConstants.FontFamily, "serif");
    styles[0] = blockComment; // 单行注释
    Style lineComment = syc.addStyle("lineComment", null);
    lineComment.addAttribute(StyleConstants.Foreground, new Color(63, 127,
    95));
    styles[1] = lineComment; // 单引号和双引号字符串
    Style quotation = syc.addStyle("quotation", null);
    quotation.addAttribute(StyleConstants.Foreground, Color.GRAY);
    styles[2] = quotation; // 正则表达式
    Style regex = syc.addStyle("regex", null);
    regex.addAttribute(StyleConstants.Foreground, Color.RED);
    styles[3] = regex; // 关键字
    Style keyword = syc.addStyle("keyword", null);
    keyword.addAttribute(StyleConstants.Foreground, new Color(127, 0, 85));
    keyword.addAttribute(StyleConstants.Bold, new Boolean(true));
    styles[4] = keyword; // 对象
    Style object = syc.addStyle("object", null);
    object.addAttribute(StyleConstants.Foreground, Color.BLUE);
    styles[5] = object; /*
     * 初始化字符串 try { doc.insertString(0, text, null);
     * SwingUtilities.invokeAndWait(new Runnable() { public void run() {
     * render(); } }); } catch (Exception e) { System.out.println("Exception
     * when constructing document: " + e); }
     */ render();
    }
      

  12.   


    // 检测文档内容变化消息
    class documentListener implements DocumentListener {
    public void changedUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) {
    changed = true;
    } public void removeUpdate(DocumentEvent e) {
    changed = true;
    }
    } // 监听鼠标事件
    class attributeMouse implements MouseListener {// 鼠标点击事件
    public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) {
    if (changed) {
    changed = false;
    render();
    }
    } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { }
    } public void keyTyped(KeyEvent e) {
    } public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();
    if (i == 10) {
    render();
    }
    } public void keyReleased(KeyEvent e) { } // 进行语法着色
    public void render() {
    DefaultStyledDocument doc = (DefaultStyledDocument) m_tValue
    .getDocument();
    Pattern p = Pattern.compile(regexString);
    try {
    Matcher m = p.matcher(doc.getText(0, doc.getLength()));
    while (m.find()) {
    int start = m.start();
    int length = m.end() - start;
    int type = getMatchType(m.group());
    doc.setCharacterAttributes(start, length, styles[type], false);
    }
    } catch (BadLocationException e) {
    }

    System.out.println("render");
    } // 取得匹配类型
    private int getMatchType(String group) {
    if (group == null || group.length() < 2)
    return -1; // 不可识别的字符
    char ch1 = group.charAt(0);
    if (ch1 == '/') {
    char ch2 = group.charAt(1);
    if (ch2 == '/')
    return 1; // 单行注释
    else if (ch2 == '*')
    return 0; // 多行注释
    else
    return 3; // 正则表达式
    } else if (ch1 == '\"' || ch1 == '\'')
    return 2; // 字符串
    if (keySet.contains(group))
    return 4; // 关键字
    return 5; // 对象
    } // 创建工具栏按钮
    private void addIconAction(JToolBar toolbar, String src, Action action) {
    URL url = getClass().getClassLoader().getResource("resource/" + src);
    if (url != null) {
    ImageIcon icon = new ImageIcon(url);
    action.putValue(Action.SMALL_ICON, icon);
    }
    JButton btn = toolbar.add(action);
    btn.setMaximumSize(new Dimension(24, 20));
    btn.setToolTipText((String) action.getValue(Action.NAME));
    btn.setBorder(null);
    }
    }