欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java簡單實(shí)現(xiàn)復(fù)制 粘貼 剪切功能代碼分享

 更新時(shí)間:2023年05月02日 16:43:44   投稿:hebedich  
本文給大家分享了一段java編寫的簡單實(shí)現(xiàn)復(fù)制粘貼剪切功能的代碼,需要的小伙伴可以直接拿走使用。如有更好的方案,也可以告之本人。

廢話不多說,直接上代碼,小伙伴們仔細(xì)看下注釋吧。

/*簡單的復(fù)制 剪切 粘貼 功能
?操作:??
??復(fù)制測試: 輸入文本選擇文本,點(diǎn)擊復(fù)制,然后將光標(biāo)放在右邊的TextArea,點(diǎn)擊粘貼
??剪切測試:輸入文本選擇文本,然后將光標(biāo)放在右邊的TextArea,點(diǎn)擊剪切??
*/
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;//獲取系統(tǒng)剪貼板。
?public Demo()
?{
??this.init();
?}
?//界面初始化
?public void init()
?{
??jf = new JFrame("復(fù)制粘貼");
??p1 = new JPanel();?? //存放標(biāo)題
??p2 = new JPanel();?//存放JTextArea showMsg
??p3 = new JPanel();? //存放 button
??title = new JLabel("復(fù)制粘貼剪切演示");
??edit = new JTextArea("請輸入內(nèi)容",15,25);
??edit.setLineWrap(true);
??showMsg = new JTextArea(15,25);
??showMsg.setLineWrap(true);
??showMsg.setEnabled(false);
??copy = new JButton("復(fù)制");
??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);
??//添加事件監(jiān)聽機(jī)制
??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();? //拖動鼠標(biāo)選取文本
???//創(chuàng)建能傳輸指定 String 的 Transferable。
???StringSelection editText =
?????new StringSelection(tempText);
???/**
???將剪貼板的當(dāng)前內(nèi)容設(shè)置到指定的 transferable 對象,
???并將指定的剪貼板所有者作為新內(nèi)容的所有者注冊。
???*/
???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();
?}
}

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

相關(guān)文章

最新評論