当前位置: 技术问答>java相关
java怎样操作粘贴板?
来源: 互联网 发布时间:2015-01-02
本文导语: | //给你一个简单的例子。 //按下paste按钮即可。 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) { ...
|
//给你一个简单的例子。
//按下paste按钮即可。
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);}
});
}
}
//按下paste按钮即可。
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);}
});
}
}