现在的位置: 首页 > 综合 > 正文

Java如何使用系统剪贴板

2018年05月18日 ⁄ 综合 ⁄ 共 2894字 ⁄ 字号 评论关闭

转载自:http://blog.chinaunix.net/space.php?uid=11298163&do=blog&id=2886202

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

public class Test50 extends Frame implements ActionListener{
    MenuBar menubar;Menu menu;
    MenuItem copy,cut,paste;
    TextArea text1,text2;
    Clipboard clipboard = null;
    Test50(){
        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,300);
        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[]){
                Test50 win = new Test50();
                }
            }


剪贴板是我们很熟悉的概念,我们经常将一个程序中的某些数据先剪切或复制到系统剪切板中,然后再将剪贴板中的数据粘贴到本程序或者其他程序中。例如我们可将WORD中的某些文本数据复制或剪切到剪贴板,然后在粘贴到程序WORD或者记事本中。
java.awt.datatransfer包提供的类只能使我们将程序中的字符串数据复制或剪切到系统剪贴板中。
1.如果准备将文本数据复制或剪切到系统剪贴板中,必须首先使用Clipboard声明一个剪贴板对象:
Clipboard clipboard;
然后让某个组件调用方法getToolkit()获取一个Toollit对象,该对象再调用getSystemClipboard()方法获取系统的剪贴板:
clipboard=getToolkit().getSystemclipboard();
现在就可以将文本数据复制或剪切到clipboard中了。首先使用StringSelection创建一个对象,把待复制或剪切的字符串temp传递给该对象,如下所示:
StringSelection text = new StringSelection(temp);
然后将对象text放入到剪贴板
clipboard.setContents(text,null);
上述语句中setContents方法的第二个参数应当设置为剪贴板的拥有者,但对于文本的复制或剪切可以不考虑剪贴板的拥有者,这个参数可取null.
2.从剪贴板取数据到JAVA程序中
clipboard可以使用getContents(Component b)方法获取剪贴板中的数据,把取回的数据当作是一个Transferable类型的数据,因此必须使用如下语

抱歉!评论已关闭.