[java]Clipboard:实现复制粘贴

Clipboard clipboard=diaryFrame.getToolkit().getSystemClipboard();  //系统剪贴板
 String temp;  //复制内容

 

//对“复制”菜单的处理
  else if (e.getSource()==diaryFrame.getmEditCopy()) {  
   temp=diaryFrame.getJta().getSelectedText();
   StringSelection stringSelection=new StringSelection(temp);
   clipboard.setContents(stringSelection,null);
   System.out.println("复制的内容:"+temp);
  }
  //对“粘贴”菜单的处理
  else if (e.getSource()==diaryFrame.getmEidtPaste()) {  
   Transferable transferable=clipboard.getContents(this);
   DataFlavor flavor=DataFlavor.stringFlavor;
   if(transferable.isDataFlavorSupported(flavor)){
    try {
     int start=diaryFrame.getJta().getSelectionStart();  
     int end=diaryFrame.getJta().getSelectionEnd();   
     diaryFrame.getJta().replaceRange("",start,end);   
     String str;  
     int n=diaryFrame.getJta().getCaretPosition();   
     str=(String)transferable.getTransferData(flavor);   
     diaryFrame.getJta().insert(str,n);
    }catch(Exception ee){
     ee.printStackTrace();
    }
   }
  }

转载于:https://www.cnblogs.com/hin24/p/3677973.html

你可能感兴趣的:([java]Clipboard:实现复制粘贴)