java 复制 粘贴_在java中如何实现复制,粘贴,剪切

在java中如何实现复制,粘贴,剪切

关注:272  答案:2  mip版

解决时间 2021-01-25 02:48

e6cb1a03ad541b3098697807b7bf1798.png

提问者挥映在沉默里的渲染

2021-01-24 22:41

请分别写出来,而且讲解一下,谢谢大家

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家你哪知我潦倒为你

2021-01-24 22:48

要用到java.awt.datatransfer包中的Clipboard类

import java.awt.*;import java.awt.event.*;

import java.awt.datatransfer.*;

public class Test extends Frame implements ActionListener

{ MenuBar menubar; Menu menu;

MenuItem copy,cut,paste;

TextArea text1,text2;

Clipboard clipboard=null;

Test()

{ clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。

menubar=new MenuBar();

menu=new Menu("Edit"); copy=new MenuItem("copy");

cut=new MenuItem ("cut"); paste=new MenuItem ("paste");

text1=new TextArea(20,20); text2=new TextArea(20,20);

copy.addActionListener(this); cut.addActionListener(this);

paste.addActionListener(this);

setLayout(new FlowLayout());

menubar.add(menu);

menu.add(copy); menu.add(cut); menu.add(paste);

setMenuBar(menubar);

add(text1);add(text2);

setBounds(100,100,200,250); setVisible(true);pack();

addWindowListener(new WindowAdapter()

{public void windowClosing(WindowEvent e)

{System.exit(0);

}

}) ;

}

public void actionPerformed(ActionEvent e)

{ if(e.getSource()==copy) //拷贝到剪贴板。

{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。

StringSelection text=new StringSelection(temp);

clipboard.setContents(text,null);

}

else if(e.getSource()==cut) //剪贴到剪贴板。

{ String temp=text1.getSelectedText(); //拖动鼠标选取文本。

StringSelection text=new StringSelection(temp);

clipboard.setContents(text,null);

int start=text1.getSelectionStart();

int end =text1.getSelectionEnd();

text1.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);

text2.append(str);

}

catch(Exception ee){}

}

}

public static void main(String args[])

{ Test win=new Test();

}

}

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼自然卷的气质

2021-01-25 00:23

跟你个剪贴板的例子吧,希望对你有帮助~

需要导入包:

import java.awt.*;

import java.awt.datatransfer.*;

将内容写入剪贴板:(相当于ctrl+c)

string temp1=text1.getselectedtext();

stringselection ss = new stringselection(temp1);

toolkit.getdefaulttoolkit().getsystemclipboard().setcontents(ss, null);

读取剪贴板:(相当于ctrl+v)

transferable t = toolkit.getdefaulttoolkit().getsystemclipboard().getcontents(null);

string readtext = (string)t.gettransferdata(dataflavor.stringflavor);

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值