您可以使用系統剪貼板。這將允許您從Java應用程序複製並粘貼到本地應用程序!
創建使用Clipboard
的新ActionListener
。
public class ClipboardActionListener implements ActionListener
{
private final JTextField;
public ClipboardActionListener(JTextField text)
{
this.text= text;
}
@Override
public void actionPerformed(ActionEvent e)
{
Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(text.getText());
system.setContents(selection, selection)
}
}
並註冊創建文本字段時的按鍵筆觸。
KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
textField.registerKeyboardAction(new ClipboardActionListener(textField), copy, JComponent.WHEN_FOCUSED);