java 从剪贴板读到文件,Java 从windows剪贴板复制文件内容的例子

Clipboard clipboard; //剪贴板

JTextArea jtaCopyTo=new JTextArea(5,10);//用于拷贝的文本框

JTextArea jtaPaste=new JTextArea(5,10);//用于粘贴的文本框

public ClipboardDemo(){

super("使用剪贴板的复制/粘贴程序");//调用父类构造函数

clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();//获得系统剪贴板

JButton btCopy=new JButton("拷贝");//拷贝按钮

JButton btPaste=new JButton("粘贴");//粘贴按钮

jtaCopyTo.setLineWrap(true);//设置换行

jtaPaste.setLineWrap(true);

jtaCopyTo.setBorder(BorderFactory.createTitledBorder("复制到系统剪切板"));//设置边界

jtaPaste.setBorder(BorderFactory.createTitledBorder("从系统剪切板粘贴"));

Container container=getContentPane();//得到容器

JToolBar toolBar=new JToolBar();//实例化工具栏

toolBar.add(btCopy);//增加工具栏按钮

toolBar.add(btPaste);

btCopy.addActionListener(new CopyListener());//按钮事件处理

btPaste.addActionListener(new PasteListener());

Box box=new Box(BoxLayout.X_AXIS);//实例化Box

box.add(jtaCopyTo);//增加文本框到Box上

box.add(jtaPaste);

container.add(toolBar,BorderLayout.NORTH);//增加工具栏到容器

container.add(box,BorderLayout.CENTER);//增加Box到容器

setSize(320,180);//设置窗口尺寸

setVisible(true);//设置窗口为可视

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序

}

class CopyListener implements ActionListener {//拷贝数据处理

public void actionPerformed(ActionEvent event) {

StringSelection contents=new StringSelection(jtaCopyTo.getText());//用拷贝文本框文本实例化StringSelection对象

clipboard.setContents(contents, ClipboardDemo.this);//设置系统剪贴板内容

}

}

class PasteListener implements ActionListener {//粘贴数据处理

public void actionPerformed(ActionEvent event) {

Transferable contents=clipboard.getContents(this);//得到剪贴板内容

if(contents!=null && contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {//判断内容是否为空,是否为字符串

try{

String string= (String) contents.getTransferData(DataFlavor.stringFlavor);//转换内容到字符串

jtaPaste.append(string);//插入字符串到粘贴文本框

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值