4

我在试图弄清楚如何在保存到系统剪贴板时保留 Java 程序中的文本格式时遇到问题。

它不适用于诸如 Microsoft 的 Wordpad 或 Lotus Symphony 之类的东西。相反,如果我在 Word 中创建一个格式化字符串并复制它,那么它可以在我尝试将其粘贴到的所有试用案例中正常工作。

我不想使用任何外部资源,例如 org.eclipse.*。

以下是我编译的一些链接,它们可能会帮助我指出正确的方向。

我觉得好像我没有使用正确的 Data Flavor? http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/datatransfer/DataFlavor.html

我发现这个链接谈了很多关于 dataflavors 的内容,但并没有说明将哪个链接用于格式化文本。我确实理解这在每个操作系统上可能都不一样,我需要检查以确保它在我正在使用的操作系统上受支持。

http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=/export/home/httpd/javaworld/javaworld/javatips/jw-javatip61.html&pagename=/javaworld/javatips/jw-javatip61。 html&pageurl=http://www.javaworld.com/javaworld/javatips/jw-javatip61.html&site=jw_core

感谢您在高级方面的所有帮助,我真的很感激!

编辑

我目前正在使用来自以下网址的一些代码:http: //lists.apple.com/archives/java-dev/2004/Jul/msg00359.html进行了一些小改动。我目前遇到的问题是我需要以两种不同的格式将数据传输到剪贴板。“text/rtf”和“text/plain”看到有些程序不支持RTF。我正在使用内部剪贴板来查看剪贴板内部的内容。我可以成功放置 RTF 或纯文本,但不能同时放置两者。当我这样做时,只会添加最后一个。非常感谢您的帮助!

总之,我不能同时使用两种不同的数据风格设置剪贴板。

import java.awt.datatransfer.*;
import java.io.*;

public class clipBoard
{
public static final String RTF_STRING = "{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Courier;}}\r  \n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis line is the default color\\line\r\n\\cf2\r\n\\tab This line is red and has a tab before it\\line\r\n\\cf1\r\n\\page This line is the default color and the first line on page 2\r\n}\r\n";
public static final DataFlavor RTF_FLAVOR = new DataFlavor("text/rtf", "Rich Formatted Text");

public static void main(String[] args){
 Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
 Transferable t = new MyTransferable(
 new ByteArrayInputStream(RTF_STRING.getBytes()), RTF_FLAVOR);
 cb.setContents(t, null);
}

static class MyTransferable implements Transferable
{
    private Object data = null;
    private DataFlavor flavor;

    public MyTransferable(Object o, DataFlavor df)
    { data = o; flavor = df; }

    public Object getTransferData (DataFlavor df) throws
    UnsupportedFlavorException, IOException
    {
        if (!flavor.isMimeTypeEqual(flavor))
            throw new UnsupportedFlavorException(df);
        return data;
    }

    public boolean isDataFlavorSupported (DataFlavor df)
    {
        return flavor.isMimeTypeEqual(df);
    }

    public DataFlavor[] getTransferDataFlavors()
    {
        DataFlavor[] ret = {flavor};
        return ret;
    }
}

}

4

1 回答 1

6

在朋友塞巴斯蒂安和洛根的大量搜索和反复试验和帮助之后,似乎已经弄清楚了。这允许在 Java 中一次将多种格式的数据保存到剪贴板,同时还保留文本的样式和格式。希望这可以帮助某人。这也是一个很好的资源。http://www.pindari.com/rtf1.html

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;

public class clipBoard{
//Creates the RTF string 
private static final String RTF_STRING = "{\\rtf1\\ansi\\deff0\r\n{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;}\r\nThis line is the default color\\line\r\n\\cf2\r\nThis line is red\\line\r\n\\cf1\r\nThis line is the default color\r\n}\r\n}";
//Creates the plain text string
private static final String PLAIN_STRING = "This line is the default color \n This line is red \n This line is the default color";
//Array of data for specific flavor
private static final Object data[] = {new ByteArrayInputStream(RTF_STRING.getBytes()),new ByteArrayInputStream(PLAIN_STRING.getBytes())};
//Plain favor
private static final DataFlavor PLAIN_FLAVOR = new DataFlavor("text/plain", "Plain Flavor");
//RTF flavor
private static final DataFlavor RTF_FLAVOR = new DataFlavor("text/rtf", "Rich Formatted Text");
//Array of data flavors
private static final DataFlavor flavors[] = {RTF_FLAVOR,PLAIN_FLAVOR};

public static void main(String[] args){
    //Create clip board object
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    //Create transferable object
    Transferable p = new MyTransferable(data,flavors);
    //Transfer to clip board
    cb.setContents(p, null);
}

static class MyTransferable implements Transferable{
    //Array of data
    private Object dataA[] = null;
    //Array of flavors
    private DataFlavor flavorA[] = null;

    //Transferable class constructor
    public MyTransferable(Object data[], DataFlavor flavors[]){
        //Set the data passed in to the local variable
        dataA = data;
        //Set the flavors passes in to the local variable
        flavorA = flavors;
    }

    public Object getTransferData (DataFlavor df) throws UnsupportedFlavorException, IOException{
        //If text/rtf flavor is requested
        if (df.getMimeType().contains("text/rtf")){
            //Return text/rtf data
            return dataA[0];
        }
        //If plain flavor is requested
        else if (df.getMimeType().contains("text/plain")){
            //Return text/plain data
            return dataA[1];
        }
        else{
            throw new UnsupportedFlavorException(df);
        }
    }

    public boolean isDataFlavorSupported (DataFlavor df){
        //If the flavor is text/rtf or tet/plain return true
        if(df.getMimeType().contains("text/rtf") || df.getMimeType().contains("text/plain")){
            return true;
        }
        //Return false
        else{
            return false;
        }
    }

    public DataFlavor[] getTransferDataFlavors(){
        //Return array of flavors
        return flavorA;
    }
 }
}
于 2013-02-19T08:04:00.157 回答