用法
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(XTransferableAdaptor.of("HelloWorld", DataFlavor.stringFlavor), null);
工具类如下
/**
* awt 剪贴板的工具类, 弥补了内置类型不全的问题
*
* @param data 要往剪贴板内塞的数据
* @param flavors 支持的数据类型
*/
public record XTransferableAdaptor(@Nonnull Object data, @Nonnull DataFlavor[] flavors) implements Transferable {
public static XTransferableAdaptor of(Object data, DataFlavor... flavors) {
return new XTransferableAdaptor(data, flavors);
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return Set.of(flavors).contains(flavor);
}
@Nonnull
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)) {
return data;
}
throw new UnsupportedFlavorException(flavor);
}
}
标签:flavors,剪贴板,Java,AWT,flavor,data,public,DataFlavor
From: https://www.cnblogs.com/Hakurei-Reimu-Zh/p/18656293