Java-剪切和粘贴

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-剪切和粘贴相关的知识,希望对你有一定的参考价值。

  1. package system.clipboard;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.datatransfer.Clipboard;
  6. import java.awt.datatransfer.ClipboardOwner;
  7. import java.awt.datatransfer.DataFlavor;
  8. import java.awt.datatransfer.StringSelection;
  9. import java.awt.datatransfer.Transferable;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JTextArea;
  16.  
  17. public class ClipBoard extends JFrame implements ClipboardOwner, ActionListener
  18. {
  19. private static final long serialVersionUID = 1L;
  20.  
  21. JTextArea srcText, dstText;
  22. JButton copyButton, pasteButton;
  23. Clipboard clipboard = getToolkit().getSystemClipboard();
  24.  
  25. public ClipBoard()
  26. {
  27. super("Clipboard Test");
  28.  
  29. GridBagLayout gridbag = new GridBagLayout();
  30.  
  31. setLayout(gridbag);
  32.  
  33. srcText = new JTextArea(8, 32);
  34. c.gridwidth = 2;
  35. c.anchor = GridBagConstraints.CENTER;
  36. gridbag.setConstraints(srcText, c);
  37. add(srcText);
  38.  
  39. copyButton = new JButton("Copy Above");
  40. copyButton.setActionCommand("copy");
  41. copyButton.addActionListener(this);
  42. c.gridy = 1;
  43. c.gridwidth = 1;
  44. gridbag.setConstraints(copyButton, c);
  45. add(copyButton);
  46.  
  47. pasteButton = new JButton("Paste Below");
  48. pasteButton.setActionCommand("paste");
  49. pasteButton.addActionListener(this);
  50. pasteButton.setEnabled(false);
  51. c.gridx = 1;
  52. gridbag.setConstraints(pasteButton, c);
  53. add(pasteButton);
  54.  
  55. dstText = new JTextArea(8, 32);
  56. c.gridx = 0;
  57. c.gridy = 2;
  58. c.gridwidth = 2;
  59. gridbag.setConstraints(dstText, c);
  60. add(dstText);
  61.  
  62. pack();
  63. }
  64.  
  65. public void actionPerformed(ActionEvent evt)
  66. {
  67. String cmd = evt.getActionCommand();
  68.  
  69. if(cmd.equals("copy"))
  70. {
  71. // Implement Copy operation
  72. String srcData = srcText.getText();
  73.  
  74. if(srcData != null)
  75. {
  76. StringSelection contents = new StringSelection(srcData);
  77. clipboard.setContents(contents, this);
  78. pasteButton.setEnabled(true);
  79. }
  80. }
  81. else if(cmd.equals("paste"))
  82. {
  83. // Implement Paste operation
  84. Transferable content = clipboard.getContents(this);
  85. if(content != null)
  86. {
  87. try
  88. {
  89. String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
  90. dstText.append(dstData);
  91. }
  92. catch(Exception e)
  93. {
  94. System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName());
  95. }
  96. }
  97. }
  98. }
  99.  
  100. public void lostOwnership(Clipboard clipboard, Transferable contents)
  101. {
  102. System.out.println("Clipboard contents replaced");
  103. }
  104.  
  105. public static void main(String[] args)
  106. {
  107. ClipBoard test = new ClipBoard();
  108. test.setVisible(true);
  109. }
  110. }

以上是关于Java-剪切和粘贴的主要内容,如果未能解决你的问题,请参考以下文章

vim在系统剪切板的复制与粘贴

在 Python 中剪切和粘贴文件或目录 [重复]

如何使用 C# 在文件观察器中捕获剪切和粘贴操作

C#复制图文信息到剪切板再粘贴到word

selenium中的对输入框文本进行全选,复制,粘贴,剪切和删除的操作

可以以编程方式使用新的WP7剪切和粘贴功能吗?