Java 将html文档加载到JTextPane会使我的编辑器在粘贴时表现得非常糟糕

Java 将html文档加载到JTextPane会使我的编辑器在粘贴时表现得非常糟糕,java,swing,paste,jtextpane,Java,Swing,Paste,Jtextpane,我正在做编辑。我使用以下代码将html文档从路径添加到texteditor try { filename="filepath"; StringBuffer fileData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(filename)); char[] buf = new char[1024]; int numRead

我正在做编辑。我使用以下代码将html文档从路径添加到texteditor

try {       
    filename="filepath";
    StringBuffer fileData = new StringBuffer(1000);
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    char[] buf = new char[1024];
    int numRead=0;
    while((numRead=reader.read(buf)) != -1){
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
    }
    reader.close();
    result = fileData.toString();
    jtextpane.setContentType("text/html");
    jtextpane.setText(result);
} catch (Exception ex) {
    jtextpane.setText(".,1..."+ex.toString());
}
直到我第一次不使用此文件加载时,我的编辑器工作正常。但添加此代码后,我的粘贴按钮无法正常工作。它正在新行中粘贴。在这种情况下,当我删除SETCONTENTTYPE时,粘贴工作正常。但我无法删除它。我必须将html文件加载到编辑器中。请帮忙。
提前感谢。

如果您想在文本编辑器中打开html文档,您应该使用JEditorPane组合,如果需要,可以使用JScrollPane。下面是需要try/catch块的示例代码:

private void visualiserLog() {
    JEditorPane docEP = new JEditorPane();
    docEP.setEditable(true);
    File f = new File(/path/to/file.html);
    java.net.URL fileURL = null;
    try {
        fileURL = f.toURI().toURL(); // Transform path into URL
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    }

    try {
        docEP.setPage(fileURL); // Load the file to the editor
    }
    catch (IOException e) {
        e.printStackTrace();
    }

    // Initialize scroll pane (if you need it)
    JScrollPane docSP = new JScrollPane(docEP, 
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    docSP.setPreferredSize(new Dimension(800,700));

    // Set up a frame to layout the editor panel
    JFrame frame = new JFrame("HTML File");
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.setBounds(0,0,800,700);
    // If you don't use ScrollPane, you must swap docSP for docEP
    frame.getContentPane().add(docSP,BorderLayout.CENTER); 
    frame.setVisible(true);
}
我认为可以正确地将文件设置到编辑器中。之后,您应该放置复制函数和必要的侦听器


问候

那么,您是说,每当出现此代码或在运行此代码后,“粘贴”就会停止工作?您的“粘贴”按钮是如何工作的?代码是什么?粘贴不起作用,但它会将数据粘贴到新行中。下面是我的粘贴代码:button.addActionListenernew DefaultEditorKit.PasteAction;