日期:2014-05-20 浏览次数:20755 次
package cuttest;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
public class TestG{
public static String getSysClipboardText()
{
String ret = "";
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
// 获取剪切板中的内容
Transferable clipTf = sysClip.getContents(null);
// 检查内容是否是文本类型
try {
ret = (String) clipTf.getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static void main(String[] agrs){
try {
System.out.println(new TestG().getSysClipboardText());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package cuttest;
import java.awt.*;
import javax.swing.*;
public class window{
public static void main(String[] args){
JFrame frame = new JFrame("window");
JLabel label = new JLabel("号码");
JTextField txt1=new JTextField(new TestG().getSysClipboardText());
JPanel contentPane = new JPanel();
frame.setBounds(200,200,200,200);
contentPane.setLayout(new BorderLayout());
contentPane.add(txt1,BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setVisible(true);
}
}
textArea.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("arg0.getModifiers()=" + e.getModifiers());
System.out.println("arg0.getKeyCode()=" + e.getKeyCode());
// CTRL+C按下
if (e.getModifiers() == 2 && e.getKeyCode() == 67) {
System.out.println("CTRL+C......");
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});