我搜索并搜索了互联网以及StackOverflow。
它变成了白胡子。
我需要处理从Excel复制的剪贴板内容,并通过单击网页上的按钮将其粘贴到多个文本输入中。
我不需要在隐藏输入中执行CTRL-V,然后每隔几秒钟查看内容等等。
强制性问题:
没有办法用“东西”读取剪贴板? 告诉我“不”,然后我去睡觉
卡罗
答案 0 :(得分:0)
使用此文本
创建一个类文件import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;
public final class clipboardData implements ClipboardOwner {
public static void main(String... aArguments ){
clipboardData Clipboard = new clipboardData();
}
/**
* Empty implementation of the ClipboardOwner interface.
* @param aClipboard
* @param aContents
*/
@Override
public void lostOwnership(Clipboard aClipboard, Transferable aContents){
//do nothing
}
/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
* @param aString
*/
public void setData(String aString){
StringSelection stringSelection = new StringSelection(aString);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);
}
/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String.
*/
public String getData() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor);
if (hasTransferableText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (UnsupportedFlavorException | IOException ex){
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}
}
上述代码可通过以下功能访问:
创建一个新的剪贴板编辑器:
clipboardData clipboardData = new clipboardData()
setData函数:
clipboardData.setData(Data)
getData函数:
clipboardData.getData();