ClipBoard¸¦ »ç¿ëÇÑ ÅØ½ºÆ® ºÙÀ̱â
ClipboardTest.java
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class ClipboardTest extends Frame implements ClipboardOwner{
private Clipboard clipboard;
private TextField copyFrom;
private TextArea copyTo;
private Button copy, paste;
public static void main(String[] args){
ClipboardTest cbt = new ClipboardTest();
cbt.setSize(400,400);
cbt.setVisible(true);
}
public ClipboardTest(){
clipboard = getToolkit().getSystemClipboard();
copyFrom = new TextField(20);
copyTo = new TextArea(3, 20);
copy = new Button("Copy To System Clipboard");
paste = new Button("Paste From System Clipboard");
this.setLayout(new FlowLayout());
add(copyFrom);
add(copy);
add(paste);
add(copyTo);
copy.addActionListener (new CopyListener());
paste.addActionListener(new PasteListener());
}
class CopyListener implements ActionListener{
public void actionPerformed(ActionEvent event){
StringSelection contents = new StringSelection(copyFrom.getText());
clipboard.setContents(contents, ClipboardTest.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 = (String) contents.getTransferData(DataFlavor.stringFlavor);
copyTo.append(string);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public void lostOwnership(Clipboard clip, Transferable transferable){
System.out.println("Lost ownership");
}
}
C:\AWT>javac ClipboardTest.java
C:\AWT>java ClipboardTest
¿ÞÂÊ TextBoxÀÇ ±ÛÀ» Ä«ÇÇÇØ¼ ¿À¸¥ÂÊ TextArea¿¡ º¸¿©ÁÝ´Ï´Ù.
jabookÀúÀÚ¸íÇÔ |
Á¦¸ñ:¼Ò¼³°°Àº¹Ì´ÏÄÚµå ÀÛ¼ºÀÚ:Àںϸâ¹ö ±è´ë¼º |