java操作系统剪贴板内容数据

WBOY
Freigeben: 2016-07-25 09:08:14
Original
942 Leute haben es durchsucht
客户有各种稀奇的需求,现要求在使用平台过程中复制过的内容保存并展示供使用人员参考
  1. package cn.net.ssd.common.format;
  2. import java.awt.Image;
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.Clipboard;
  5. import java.awt.datatransfer.DataFlavor;
  6. import java.awt.datatransfer.StringSelection;
  7. import java.awt.datatransfer.Transferable;
  8. import java.awt.datatransfer.UnsupportedFlavorException;
  9. import java.io.IOException;
  10. public class ClipboradOperate {
  11. /**@Description:
  12. * @author zhk
  13. * @createtime 2012-7-12 上午11:33:06
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取系统剪贴板
  18. try {
  19. ImageViewer im=new ImageViewer(getImageFromClipboard());
  20. }catch (Exception e) {
  21. // TODO Auto-generated catch block
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * 从指定的剪切板中获取文本内容
  27. * 本地剪切板使用 Clipborad cp = new Clipboard("clip1"); 来构造
  28. * 系统剪切板使用 Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
  29. * 剪切板的内容 getContents(null); 返回Transferable
  30. */
  31. protected static String getClipboardText() throws Exception{
  32. Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();//获取系统剪贴板
  33. // 获取剪切板中的内容
  34. Transferable clipT = clip.getContents(null);
  35. if (clipT != null) {
  36. // 检查内容是否是文本类型
  37. if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
  38. return (String)clipT.getTransferData(DataFlavor.stringFlavor);
  39. }
  40. return null;
  41. }
  42. //往剪切板写文本数据
  43. protected static void setClipboardText(Clipboard clip, String writeMe) {
  44. Transferable tText = new StringSelection(writeMe);
  45. clip.setContents(tText, null);
  46. }
  47. // 从剪切板读取图像
  48. public static Image getImageFromClipboard() throws Exception{
  49. Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
  50. Transferable cc = sysc.getContents(null);
  51. if (cc == null)
  52. return null;
  53. else if(cc.isDataFlavorSupported(DataFlavor.imageFlavor))
  54. return (Image)cc.getTransferData(DataFlavor.imageFlavor);
  55. return null;
  56. }
  57. // 写图像到剪切板
  58. protected static void setClipboardImage2(final Image image) {
  59. Transferable trans = new Transferable(){
  60. public DataFlavor[] getTransferDataFlavors() {
  61. return new DataFlavor[] { DataFlavor.imageFlavor };
  62. }
  63. public boolean isDataFlavorSupported(DataFlavor flavor) {
  64. return DataFlavor.imageFlavor.equals(flavor);
  65. }
  66. public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
  67. if(isDataFlavorSupported(flavor))
  68. return image;
  69. throw new UnsupportedFlavorException(flavor);
  70. }
  71. };
  72. Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
  73. }
  74. }
复制代码
  1. package cn.net.ssd.common.format;
  2. /*
  3. * 查看图形的窗口
  4. */
  5. import java.awt.Frame;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Toolkit;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. /**
  13. * 用于显示图形的窗口
  14. */
  15. public class ImageViewer extends Frame {
  16. private Image image;
  17. /**
  18. * 显示一个图像
  19. * @param viewMe
  20. */
  21. public ImageViewer(Image viewMe) {
  22. image = viewMe;
  23. MediaTracker mediaTracker = new MediaTracker(this);
  24. mediaTracker.addImage(image, 0);
  25. try {
  26. mediaTracker.waitForID(0);
  27. } catch (InterruptedException ie) {
  28. ie.printStackTrace();
  29. System.exit(1);
  30. }
  31. addWindowListener(new WindowAdapter() {
  32. public void windowClosing(WindowEvent e) {
  33. System.exit(0);
  34. }
  35. });
  36. //窗口适应图像大小
  37. setSize(image.getWidth(null), image.getHeight(null));
  38. //窗口标题
  39. setTitle("Viewing Image from Clipboard");
  40. setVisible(true);
  41. }
  42. public void paint(Graphics graphics) {
  43. graphics.drawImage(image, 0, 0, null);
  44. }
  45. /**
  46. * 用于读取图像文件并生成Image对象
  47. */
  48. public static Image getImageFromFile(String fileName) {
  49. Toolkit toolkit = Toolkit.getDefaultToolkit();
  50. Image image = toolkit.getImage(fileName);
  51. return image;
  52. }
  53. }
复制代码
java操作系统剪贴板内容数据


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage