getting and setting text on the system clipboard.txt
来自「一个能解决初学者疑难的例子。」· 文本 代码 · 共 26 行
TXT
26 行
Getting and Setting Text on the System Clipboard
This examples defines methods for getting and setting text on the system clipboard.
// If a string is on the system clipboard, this method returns it;
// otherwise it returns null.
public static String getClipboard() {
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
try {
if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
String text = (String)t.getTransferData(DataFlavor.stringFlavor);
return text;
}
} catch (UnsupportedFlavorException e) {
} catch (IOException e) {
}
return null;
}
// This method writes a string to the system clipboard.
// otherwise it returns null.
public static void setClipboard(String str) {
StringSelection ss = new StringSelection(str);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?