「java文字复制粘贴」java实现文件复制粘贴
本篇文章给大家谈谈java文字复制粘贴,以及java实现文件复制粘贴对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java记事本程序如何复制,粘帖文字?
- 2、java编程中怎么复制,粘贴
- 3、java怎么实现复制粘贴功能
- 4、在java中如何实现复制,粘贴,剪切
- 5、java比较第二个字符串能否由第一个字符串复制粘贴得到
java记事本程序如何复制,粘帖文字?
大哥java运用程序你给我一个方法叫我们怎么知道你错在哪里,这又不是C过程编程一个一个function,java是面向对象的编程。这就好比你说我生病了手动不了,然后你给你的手给我看,你这手哪里出现问题了必须查看其他地方。所以说要让一个不了解该应用程序开发人员查错,最好给整个运用源码。
java编程中怎么复制,粘贴
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) br/ {System.exit(0); br/ }
}) ;
}
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();
}
}
java怎么实现复制粘贴功能
1. 往剪切板写文本数据(就是常说的String拉)
Java代码
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
2. 从指定的剪切板中获取文本内容
Java代码
protected static String getClipboardText(Clipboard clip) throws Exception{
// 获取剪切板中的内容
Transferable clipT = clip.getContents(null);
if (clipT != null) {
// 检查内容是否是文本类型
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
在java中如何实现复制,粘贴,剪切
要用到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();
}
}
java比较第二个字符串能否由第一个字符串复制粘贴得到
能。String是一个不可变的对象,因此可以将一个字符串分配给另一个字符串以进行复制。对于任何不可变的对象,我们都可以将一个变量直接分配给另一个变量。以起到在java中复制的效果。
关于java文字复制粘贴和java实现文件复制粘贴的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。