当前位置: 代码迷 >> J2SE >> JAVA可不可以监视粘贴板
  详细解决方案

JAVA可不可以监视粘贴板

热度:153   发布时间:2016-04-24 16:10:52.0
JAVA能否监视粘贴板
各位CSDN的朋友,请问JAVA能否做到监视粘贴板功能,就像Word一样,一有复制操作,就马上能提示用户,发生了粘贴板操作~谢谢

------解决方案--------------------
怎么监视不知道,这样操作
http://blog.csdn.net/tom0/archive/2004/09/17/107943.aspx
主要是java.awt.datatransfer.Clipboard和import java.awt.datatransfer.ClipboardOwner

------解决方案--------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.datatransfer.*;

public class ClipBoard {
public ClipBoard(){}

public static void main(String[] args) {
JFrame frame = new JFrame();
JButton pastebutton = new JButton( "Paste ");
JPanel panel = new JPanel();
final JTextArea area = new JTextArea( "hi ",30,50);
JScrollPane srollPane = new JScrollPane(area);

pastebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = cb.getContents(this);
try {
String s = (String)content.getTransferData(DataFlavor.stringFlavor);
area.setText(s);
}
catch (Throwable exc) {
System.err.println(e);
}
}
});

panel.add(pastebutton,BorderLayout.NORTH);
panel.add(srollPane,BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.setSize(250,100);
frame.setVisible(true);
frame. addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
}
}
  相关解决方案