如何创建一个可以使用RTF文件的clipbord?

很长的问题很抱歉,但这很重要。请全部阅读。

我想创建一个文本编辑器,该编辑器将与.rtf文件一起使用。 我决定将文件buffer.rtf用作应用程序中的剪贴板(当按下复制按钮时,从JTextPane中写入所选文本,而当按下粘贴按钮时,从中读取文本)。但是,我认为,我在RTFEditor.write(OutputStream out,Document doc,int pos,int len)方法中遇到了一个错误:它忽略len属性,并将所有数据从doc写入out。我对吗?下面的代码。

TextEditorScroll类需要从滚动中获取JTextPane:

package TextEditor;

import javax.swing.*;
import javax.swing.text.Document;

public class TextEditorScroll extends JScrollPane {

private JTextPane text = new JTextPane();

public TextEditorScroll (JTextPane textPane){
    super (textPane);
    this.text = textPane;
}


public String getText (JTextPane pane){
    return text.getText();
};


public JTextPane getTextPane (){
    return text;
}


public Document getStyledDocument (JTextPane text){
    return text.getStyledDocument();
}

}

在主班:

private ArrayList<TextEditorScroll> scrollPaneList = new ArrayList<>();
private File temp = new File("buffer.rtf");
private OutputStream outputWriter;
private RTFEditorKit RTF = new RTFEditorKit();
private InputStream inputReader;
private JTabbedPane tabs = new JTabbedPane();


// create a tabs with TextEditorScrolles and store the Scrolles at ArrayList...

public void copyText(){
    try {

        FileOutputStream bufferOut = new FileOutputStream(temp);
        int length = scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionEnd()-
// in few next raws I just get the attributes needs to RTF.write(...)
        scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionStart();
        System.out.println(length);
        int selectionStart = scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionStart();
        int selectionEnd = scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionEnd();

        RTF.write(bufferOut,scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getStyledDocument(),selectionEnd,length);

    }
        catch (FileNotFoundException ex){
        try {
            temp.createTempFile("textStore",".rtf");
        }catch (IOException exception){}
        }
    catch (IOException ex){}
    catch (BadLocationException ex){}
}
public void pasteText(){
    try {
        inputReader = new FileInputStream(temp);
        int selectionStart =scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionStart();
        int selectionEnd = scrollPaneList.get(tabs.getSelectedIndex()).getTextPane().getSelectionEnd();
        RTF.read(inputReader,selectionEnd-selectionStart);
        inputReader.close();
    } catch (FileNotFoundException ex){
        try{temp.createTempFile("textStore",".rtf");
        }catch (IOException exception){}

    } catch (BadLocationException ex){}
    catch (IOException ex){}
    };

copyText()的工作结果(我在MS Word中打开了buffer.rtf)

如何创建一个可以使用RTF文件的clipbord?

但我只选择了     

如何创建一个可以使用RTF文件的clipbord?

因此,我仍然需要为复制和粘贴按钮编写代码。有人可以帮忙吗?给我一个代码示例,或者图书馆库中的链接是否有适当的rtf支持?

也许我应该为此使用Clipboard和Dataflavor类,但是我不知道那时如何正确地从剪贴板写入和读取数据。因此,我也需要一个代码轴。

P.S。对不起,英语不好

a35689 回答:如何创建一个可以使用RTF文件的clipbord?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3085659.html

大家都在问