zoukankan      html  css  js  c++  java
  • 操作剪贴板

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

    public class ClipBoardTest {

    public static void main(String[] args) {
    String s = "abc\tedf\njhf";
    setClipboard(s);
    System.out.println(getClipboard());
    }

    // If a string is on the system clipboard, this method returns it;
    // otherwise it returns null.
    public static String getClipboard() {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
    .getContents(null);

    try {
    if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
    String text = (String) t
    .getTransferData(DataFlavor.stringFlavor);
    return text;
    }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }
    return null;
    }

    // This method writes a string to the system clipboard.
    // otherwise it returns null.
    public static void setClipboard(String str) {
    StringSelection ss = new StringSelection(str);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    }

    // deal with image
    // Getting and Setting an Image on the System Clipboard
    // If an image is on the system clipboard, this method returns it;
    // otherwise it returns null.
    public static Image getImageClipboard() {
    Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard()
    .getContents(null);

    try {
    if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
    return image;
    }
    } catch (UnsupportedFlavorException e) {
    } catch (IOException e) {
    }
    return null;
    }

    // Setting an image on the system clipboard requires a custom Transferable
    // object to hold the image while on the clipboard.
    // This method writes a image to the system clipboard.
    // otherwise it returns null.
    public static void setImageClipboard(Image image) {
    ImageSelection imgSel = new ImageSelection(image);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel,
    null);
    }

    // This class is used to hold an image while on the clipboard.
    public static class ImageSelection implements Transferable {
    private Image image;

    public ImageSelection(Image image) {
    this.image = image;
    }

    // Returns supported flavors
    public DataFlavor[] getTransferDataFlavors() {
    return new DataFlavor[] { DataFlavor.imageFlavor };
    }

    // Returns true if flavor is supported
    public boolean isDataFlavorSupported(DataFlavor flavor) {
    return DataFlavor.imageFlavor.equals(flavor);
    }

    // Returns image
    public Object getTransferData(DataFlavor flavor)
    throws UnsupportedFlavorException, IOException {
    if (!DataFlavor.imageFlavor.equals(flavor)) {
    throw new UnsupportedFlavorException(flavor);
    }
    return image;
    }
    }
    }

  • 相关阅读:
    java常用容器简要性能分析(List。Map。Set)
    初始化 List 的五种方法(java)【转】
    线程池方式对数组多线程随机取出分析
    Spring文件下载方式整理
    阿里云linux安装Consul启动
    Java字节流&字符流的转换
    VUE中字符串实现JSON格式化展示。
    java中URL作为参数前后端传递分析
    Java实现GBK转码到UTF-8(文件)
    python处理Excel文件
  • 原文地址:https://www.cnblogs.com/frostbelt/p/1794329.html
Copyright © 2011-2022 走看看