如果直接用原生系统的复制粘贴,因为在TC,如果你选中ITEM或其他东西,剪贴板就会发生变化。
所以要重写键盘事件,来重新设置剪贴板
nameTJTextField.addKeyListener(new KeyListener(){ @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { System.out.println("keycode"+e.getKeyCode()); if(e.getKeyCode()==KeyEvent.VK_CONTROL&&e.getKeyCode() == KeyEvent.VK_C ||e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_C|| (!e.isControlDown())&&e.getKeyCode() == KeyEvent.VK_C) { System.out.println("复制事件"); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); //获得系统粘贴板 String selectStr=""; if(nameTJTextField instanceof JTextField){ selectStr=((JTextField)nameTJTextField).getSelectedText(); } System.out.println("复制文本:" + selectStr); StringSelection textInfoSelected = new StringSelection(selectStr); //建立一个粘贴板内容实例. clipboard.setContents(textInfoSelected, null); //将textInfoSelected加入到粘贴板中; AIFClipboard aifclipboard = AIFPortal.getClipboard(); aifclipboard.clearClipboard(); } } @Override public void keyTyped(KeyEvent arg0) { System.out.println("33333"); } });