JAVA程序设计实现SimpleCutAndPaste的功能!
package com.davidflanagan.examples.datatransfer;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
/**
* This program demonstrates how to add simple copy-and-paste capabilities
* to an application.
**/
public class SimpleCutAndPaste extends Frame implements ClipboardOwner
{
/** The main method creates a frame and pops it up. */
public static void main(String[] args) {
Frame f = new SimpleCutAndPaste();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
f.pack();
f.setVisible(true);
}
/** The text field that holds the text that is cut or pasted */
TextField field;
/**
* The constructor builds a very simple test GUI, and registers this object
* as the ActionListener for the buttons
**/
public SimpleCutAndPaste() {
super("SimpleCutAndPaste"); // Window title
this.setFont(new Font("SansSerif", Font.PLAIN, 18)); // Use a big font
// Set up the Cut button
Button copy = new Button("Copy");
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { copy(); }
});
this.add(copy, "West");
// Set up the Paste button
Button paste = new Button("Paste");
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { paste(); }
});
this.add(paste, "East");
// Set up the text field that they both operate on
field = new TextField();
this.add(field, "North");
}
/**
* This method takes the current contents of the text field, creates a
* StringSelection object to represent that string, and puts the
* StringSelection onto the clipboard
**/
public void copy() {
// Get the currently displayed value
String s = field.getText();
// Create a StringSelection object to represent the text.
// StringSelection is a pre-defined class that implements
// Transferable and ClipboardOwner for us.
StringSelection ss = new StringSelection(s);
// Now set the StringSelection object as the contents of the clipboard
// Also specify that we're the clipboard owner
this.getToolkit().getSystemClipboard().setContents(ss, this);
// Highlight the text to indicate it is on the clipboard.
field.selectAll();
}
/**
* Get the contents of the clipboard, and, if we understand the type,
* display the contents. This method understands strings and file lists.
**/
public void paste() {
// Get the clipboard
Clipboard c = this.getToolkit().getSystemClipboard();
// Get the contents of the clipboard, as a Transferable object
Transferable t = c.getContents(this);
// Find out what kind of data is on the clipboard
try {
if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
// If it is a string, then get and display the string
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
field.setText(s);
}
else if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// If it is a list of File objects, get the list and display
// the name of the first file on the list
java.util.List files = (java.util.List)
t.getTransferData(DataFlavor.javaFileListFlavor);
java.io.File file = (java.io.File)files.get(0);
field.setText(file.getName());
}
}
// If anything goes wrong with the transfer, just beep and do nothing.
catch (Exception e) { this.getToolkit().beep(); }
}
/**
* This method implements the ClipboardOwner interface. It is called when
* something else is placed on the clipboard.
**/
public void lostOwnership(Clipboard c, Transferable t) {
// Un-highlight the text field, since we don't "own" the clipboard
// anymore, and the text is no longer available to be pasted.
field.select(0,0);
}
}
****绝对经典的JAVA电子教程****
本人也是JAVA的学习爱好者(大学计算机专业),为了让大家学习更又效率!为此
廉价转让本人以前购买的-----绝对经典JAVA电子教程!--------
-------------比购买书籍更便宜!绝对超值!大家可以对比!
-
SunOne-专集: (国外经典,中文)
SUN-Java 2教程(第五版)
SUN--Java2核心技术卷II--高性能(中英2版,配源码)
SUN--Java2核心技术卷I--原理篇(中英2版,配源码)
SUN--Java高效编程指南
SUN--Java技术精髓
SUN-Java与分布式系统
SUN--Java语言导学
-------------------------------------------------------
开发专家(飞思)---专集:
开发专家之Sun ONE Java 2应用开发指(配源码)
开发专家之Sun ONE Java TCP_IP应用开发详解(配源码)
开发专家之Sun ONE Java Web 服务(配源码)
开发专家之Sun ONE JSP应用开发详解(配源码)
--------------------------------------------------------
JAVA基本网络程序设计专集:
Java P2P程序设计
Java2网络协议内幕
Java网络编程实例
Java网络程序设计TCP-IP
-----------------------------------------------------
JDBC数据库专集:
Java 数据库编程宝典
分布式JAVA 2数据库系统开发指南
Java数据库应用程序编程指南
---------------------------------------------------------
OReilly--专集 (国外经典,中文)
OReillyJava网络编程 (第二版)
JavaTM Servlet 编程 第二版
O′Reilly--JavaTM经典实例
JavaTM安全 (第二版)
JavaTM技术手册 (第三版)
----------------------------------------------------
JAVA2综合类书籍:
Java 2参考大全 (第五版,国外经典中文)
JAVAV编程思想(英文原版)
Java大学教程(外国大学经典教材,翻译为中文)
Java 2 API大全 第1 ,2卷(2本) (国外经典,中文)
Java常用数值算法集
数据结构(Java语言版)(国外经典,中文)
Enterprise JavaBeans2.0程序设计
Java 2 Web 开发认证学习指南
Java 2编程21天自学通 (第二版专业参考版(国外经典,中文)
JavaScript宝典:第四版
Java手机程序设计入门与应用
Java移动通信程序设计-J2ME MIDP
Java数据结构与面向对象编程基础
例释JAVA2企业版(J2EE)程序设计
用J2EE和UML开发Java企业级应用程序
-----------------------------------------------------
告诉我你所需要的书籍名称!然后商谈价格!
如果大家需要,可以联系:
QQ: 371957156 ( 时常在线)
Email: dw5189@163.com
在我确认以后,我可以通在线QQ和Email的方式把书籍发送给你!
注意:你的Email最好为163的!
诚信创造未来!JAVA爱好者!