Java-剪切和粘贴
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-剪切和粘贴相关的知识,希望对你有一定的参考价值。
package system.clipboard; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.ClipboardOwner; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.Transferable; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; { private static final long serialVersionUID = 1L; JTextArea srcText, dstText; JButton copyButton, pasteButton; public ClipBoard() { super("Clipboard Test"); setLayout(gridbag); c.gridwidth = 2; gridbag.setConstraints(srcText, c); add(srcText); copyButton.setActionCommand("copy"); copyButton.addActionListener(this); c.gridy = 1; c.gridwidth = 1; gridbag.setConstraints(copyButton, c); add(copyButton); pasteButton.setActionCommand("paste"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); c.gridx = 1; gridbag.setConstraints(pasteButton, c); add(pasteButton); c.gridx = 0; c.gridy = 2; c.gridwidth = 2; gridbag.setConstraints(dstText, c); add(dstText); pack(); } { if(cmd.equals("copy")) { // Implement Copy operation if(srcData != null) { clipboard.setContents(contents, this); pasteButton.setEnabled(true); } } else if(cmd.equals("paste")) { // Implement Paste operation if(content != null) { try { dstText.append(dstData); } { System.out.println("Couldn't get contents in format: " + DataFlavor.stringFlavor.getHumanPresentableName()); } } } } { } { ClipBoard test = new ClipBoard(); test.setVisible(true); } }
以上是关于Java-剪切和粘贴的主要内容,如果未能解决你的问题,请参考以下文章