java簡單實現複製 粘貼 剪下功能代碼分享_java

來源:互聯網
上載者:User

廢話不多說,直接上代碼,小夥伴們仔細看下注釋吧。

複製代碼 代碼如下:

/*簡單的複製 剪下 粘貼 功能
 操作:  
  複製測試: 輸入文本選擇文本,點擊複製,然後將游標放在右邊的TextArea,點擊粘貼
  剪下測試:輸入文本選擇文本,然後將游標放在右邊的TextArea,點擊剪下  
*/
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
public class Demo implements ActionListener
{
 private JFrame jf;
 private JPanel p1, p2, p3;   //上中下
 private JLabel title;
 private JTextArea edit,showMsg;
 private JButton copy,paste,cut;
 Clipboard clipboard;//擷取系統剪貼簿。
 
 public Demo()
 {
  this.init();
 }
 //介面初始化
 public void init()
 {
  jf = new JFrame("複製粘貼");
  p1 = new JPanel();   //存放標題
  p2 = new JPanel(); //存放JTextArea showMsg
  p3 = new JPanel();  //存放 button
  title = new JLabel("複製粘貼剪下示範");
  edit = new JTextArea("請輸入內容",15,25);
  edit.setLineWrap(true);
  showMsg = new JTextArea(15,25);
  showMsg.setLineWrap(true);
  showMsg.setEnabled(false);
  copy = new JButton("複製");
  paste = new JButton("粘貼");
  cut = new JButton("剪下");
  clipboard = jf.getToolkit().getSystemClipboard();
  
  p1.setLayout(new FlowLayout());
  p1.setSize(599,30);
  p1.add(title);
  
  p2.setLayout(new FlowLayout());
  p2.setBackground(Color.gray);
  p2.add(edit);
  p2.add(showMsg);
  
  p3.setLayout(new FlowLayout());
  p3.add(copy);
  p3.add(paste);
  p3.add(cut);
  
  //添加事件監聽機制
  copy.addActionListener(this);
  paste.addActionListener(this);
  cut.addActionListener(this);
  
  // this.copyStr(copy);
  jf.add(p1, BorderLayout.NORTH);
  jf.add(p2, BorderLayout.CENTER);
  jf.add(p3, BorderLayout.SOUTH);
  jf.setLocation(400,200);
  jf.setSize(600,450);
  jf.setResizable(false);
  jf.setVisible(true);
 }
 
 //事件處理
 public void actionPerformed(ActionEvent e)
 {
  if(e.getSource() == copy)
  {
   String tempText = edit.getSelectedText();  //拖動滑鼠選取文本
   //建立能傳輸指定 String 的 Transferable。
   StringSelection editText =
     new StringSelection(tempText);
   /**
   將剪貼簿的當前內容設定到指定的 transferable 對象,
   並將指定的剪貼簿所有者作為新內容的所有者註冊。
   */
   clipboard.setContents(editText,null);
  }else if(e.getSource() == cut)
  {
   String tempText = edit.getSelectedText();
   StringSelection editText =
     new StringSelection(tempText);
   clipboard.setContents(editText,null);
   int start= edit.getSelectionStart();
   int end  = edit.getSelectionEnd();
   showMsg.replaceRange("",start,end) ; //從Text1中刪除被選取的文本。
  }else if(e.getSource() == paste)
  {
   Transferable contents = clipboard.getContents(this);
     DataFlavor  flavor= DataFlavor.stringFlavor;
      if( contents.isDataFlavorSupported(flavor))
      {
     try
     { 
      String str;
      str = (String)contents.getTransferData(flavor);
      showMsg.append(str);
     }catch(Exception ex)
     {
      ex.printStackTrace();
     }
      }
  }
 }
 
 public static void main(String[] args)
 {
  new Demo();
 }
}

代碼很簡單,使用也很方便,小夥伴們有更好的思路的話,請一定要告訴我。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.