| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖
标题:自己利用ClipBoard搞了一个小东西,献丑了!
只看楼主 加入收藏
xudongcsharp
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:129
专家分:156
注 册:2009-5-31
结帖率:53.33%
收藏
已结贴  问题点数:20 回复次数:2 
自己利用ClipBoard搞了一个小东西,献丑了!
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.ScrollPane;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

public class ClipBoard implements ActionListener, ClipboardOwner {

    private JFrame jf;
    private JButton jbcopy, jbpaste, jbcut;
    private JToolBar jt;
    private JTextArea jta;
    private Clipboard clip;
    private ScrollPane sp;

    public ClipBoard() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jf = new JFrame("clipboard");
        jta = new JTextArea();
        jbcopy = new JButton("复制");
        jbpaste = new JButton("粘贴");
        jbcut = new JButton("剪切");
        jt = new JToolBar();
        sp = new ScrollPane();
        clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 注册监听器
        jbcopy.addActionListener(this);
        jbpaste.addActionListener(this);
        jbcut.addActionListener(this);

        jt.add(jbcopy);
        jt.add(jbpaste);
        jt.add(jbcut);
        sp.add(jta);

        jbpaste.setEnabled(false);
        jbcut.setEnabled(false);

        jf.add(jt, BorderLayout.NORTH);
        jf.add(sp, BorderLayout.CENTER);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(300, 300);
        jf.setLocation((screenSize.width - 300) / 2,
                (screenSize.height - 300) / 2);
        jf.setVisible(true);
    }

    public static void main(String[] args) {
        new ClipBoard();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd.equals("复制")) {
            if (jta.getSelectedText() != null) {
                StringSelection ss = new StringSelection(jta.getSelectedText());
                clip.setContents(ss, this);
                jbpaste.setEnabled(true);
                jbcut.setEnabled(true);
            }
        } else if (cmd.equals("粘贴")) {
            Transferable content = clip.getContents(this);
            if (content != null) {
                try {
                    String dstData = (String) content
                            .getTransferData(DataFlavor.stringFlavor);
                    jta.append(dstData);
                } catch (Exception e1) {
                    System.out
                            .println("Couldn't get contents in format: "
                                    + DataFlavor.stringFlavor
                                            .getHumanPresentableName());
                }
            }
        } else if (cmd.equals("剪切")) {
            if (jta.getSelectedText() != null) {
                String con = jta.getText();
                int start = jta.getSelectionStart();
                int end = jta.getSelectionEnd();
                String selecttext=jta.getSelectedText();
                String newcon = con.substring(0, start)
                        + con.substring(end, con.length());
                jta.setText(newcon);
                StringSelection ss = new StringSelection(selecttext);
                clip.setContents(ss, this);
            }
        }
    }

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        System.out.println("Clipboard contents replaced");
    }
}

搜索更多相关主题的帖子: ClipBoard 小东西 献丑 
2010-05-19 20:28
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
收藏
得分:10 
不错,学习了。
2010-05-23 02:55
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:10 
辛苦了..

C#超级群 74862681,欢迎大家的到来!
2010-05-24 18:57
快速回复:自己利用ClipBoard搞了一个小东西,献丑了!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019837 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved