java简单实现复制 粘贴 剪切功能代码分享
作者:hebedich 时间:2023-10-14 05:08:23
废话不多说,直接上代码,小伙伴们仔细看 * 释吧。
/*简单的复制 剪切 粘贴 功能
操作:
复制测试: 输入文本选择文本,点击复制,然后将光标放在右边的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();
}
}
代码很简单,使用也很方便,小伙伴们有更好的思路的话,请一定要告诉我。
标签:java,复制,粘贴,剪切
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 实战项目锤炼之嘟嘟健身房管理系统的实现流程
2021-09-24 23:58:07
![](https://img.aspxhome.com/file/2023/0/125250_0s.png)
java面试题之try中含return语句时代码的执行顺序详解
2023-11-24 07:34:16
![](https://img.aspxhome.com/file/2023/3/59913_0s.png)
Java开发工具IntelliJ IDEA安装图解
2022-06-14 02:30:20
![](https://img.aspxhome.com/file/2023/4/69934_0s.png)
带大家认识Java语法之泛型与通配符
2021-06-04 06:14:46
![](https://img.aspxhome.com/file/2023/4/67494_0s.png)
C#如何自定义multipart/form-data的解析器
2023-12-04 18:59:03
java实现超市商品库存管理平台
2022-05-25 10:09:20
Struts2 文件上传进度条的实现实例代码
2023-04-20 11:13:59
![](https://img.aspxhome.com/file/2023/0/72620_0s.png)
深入解析Java接口(interface)的使用
2022-05-07 07:50:51
C#线程同步的三类情景分析
2023-01-07 22:38:04
Android ViewPager实现图片轮播效果
2023-03-20 17:00:58
![](https://img.aspxhome.com/file/2023/7/98627_0s.jpg)
详解Spring循环依赖的解决方案
2022-05-29 13:14:57
对指定的网页进行截图的效果 C#版
2022-07-04 03:14:18
unity实现鼠标跟随(ITween)
2021-09-26 00:01:43
![](https://img.aspxhome.com/file/2023/8/79098_0s.png)
Java synchronized关键_动力节点Java学院整理
2023-11-10 11:08:53
Spring Boot+Shiro实现一个Http请求的Basic认证
2022-06-01 22:22:31
![](https://img.aspxhome.com/file/2023/5/61065_0s.png)
android系统在静音模式下关闭camera拍照声音的方法
2021-09-04 00:34:20
![](https://img.aspxhome.com/file/2023/7/137207_0s.gif)
C#基础:Dispose()、Close()、Finalize()的区别详解
2021-10-22 19:20:23
Android7.0 MessageQueue详解
2021-08-29 17:01:43
![](https://img.aspxhome.com/file/2023/0/103870_0s.jpg)
C#构造函数详解
2023-02-25 22:38:50
Ubuntu中使用VS Code与安装C/C++插件的教程详解
2023-07-26 08:21:00
![](https://img.aspxhome.com/file/2023/8/123858_0s.png)