用法
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);}@Overridepublic DataFlavor[] getTransferDataFlavors() {return flavors;}@Overridepublic boolean isDataFlavorSupported(DataFlavor flavor) {return Set.of(flavors).contains(flavor);}@Nonnull@Overridepublic Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {if (isDataFlavorSupported(flavor)) {return data;}throw new UnsupportedFlavorException(flavor);}}