JAVA程序设计实现SimpleCutAndPaste的功能!

dw5189 2004-08-03 08:28:42
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爱好者!




...全文
229 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dw5189 2004-08-03
  • 打赏
  • 举报
回复
// Choose a cursor based on the type of drag the user initiated
Cursor cursor;
switch(e.getDragAction()) {
case DnDConstants.ACTION_COPY:
cursor = DragSource.DefaultCopyDrop;
break;
case DnDConstants.ACTION_MOVE:
cursor = DragSource.DefaultMoveDrop;
break;
default:
return; // We only support move and copys
}

// Some systems allow us to drag an image along with the
// cursor. If so, create an image of the scribble to drag
if (dragSource.isDragImageSupported()) {
Rectangle scribbleBox = dragScribble.getBounds();
Image dragImage = this.createImage(scribbleBox.width,
scribbleBox.height);
Graphics2D g = (Graphics2D)dragImage.getGraphics();
g.setColor(new Color(0,0,0,0)); // transparent background
g.fillRect(0, 0, scribbleBox.width, scribbleBox.height);
g.setColor(Color.black);
g.setStroke(linestyle);
g.translate(-scribbleBox.x, -scribbleBox.y);
g.draw(dragScribble);
Point hotspot = new Point(-scribbleBox.x, -scribbleBox.y);

// Now start dragging, using the image.
e.startDrag(cursor, dragImage, hotspot, dragScribble,this);
}
else {
// Or start the drag without an image
e.startDrag(cursor, dragScribble,this);
}
// After we've started dragging one scribble, stop looking
return;
}
}
}

/**
* This method, and the four unused methods that follow it implement the
* DragSourceListener interface. dragDropEnd() is invoked when the user
* drops the scribble she was dragging. If the drop was successful, and
* if the user did a "move" rather than a "copy", then we delete the
* dragged scribble from the list of scribbles to draw.
**/
public void dragDropEnd(DragSourceDropEvent e) {
if (!e.getDropSuccess()) return;
int action = e.getDropAction();
if (action == DnDConstants.ACTION_MOVE) {
scribbles.remove(beingDragged);
beingDragged = null;
repaint();
}
}

// These methods are also part of DragSourceListener.
// They are invoked at interesting points during the drag, and can be
// used to perform "drag over" effects, such as changing the drag cursor
// or drag image.
public void dragEnter(DragSourceDragEvent e) {}
public void dragExit(DragSourceEvent e) {}
public void dropActionChanged(DragSourceDragEvent e) {}
public void dragOver(DragSourceDragEvent e) {}

// The next five methods implement DropTargetListener

/**
* This method is invoked when the user first drags something over us.
* If we understand the data type being dragged, then call acceptDrag()
* to tell the system that we're receptive. Also, we change our border
* as a "drag under" effect to signal that we can accept the drop.
**/
public void dragEnter(DropTargetDragEvent e) {
if (e.isDataFlavorSupported(Scribble.scribbleDataFlavor) ||
e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
this.setBorder(dropBorder);
}
}

/** The user is no longer dragging over us, so restore the border */
public void dragExit(DropTargetEvent e) { this.setBorder(normalBorder); }

/**
* This is the key method of DropTargetListener. It is invoked when the
* user drops something on us.
**/
public void drop(DropTargetDropEvent e) {
this.setBorder(normalBorder); // Restore the default border

// First, check whether we understand the data that was dropped.
// If we supports our data flavors, accept the drop, otherwise reject.
if (e.isDataFlavorSupported(Scribble.scribbleDataFlavor) ||
e.isDataFlavorSupported(DataFlavor.stringFlavor)) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
}
else {
e.rejectDrop();
return;
}

// We've accepted the drop, so now we attempt to get the dropped data
// from the Transferable object.
Transferable t = e.getTransferable(); // Holds the dropped data
Scribble droppedScribble; // This will hold the Scribble object

// First, try to get the data directly as a scribble object
try {
droppedScribble =
(Scribble) t.getTransferData(Scribble.scribbleDataFlavor);
}
catch (Exception ex) { // unsupported flavor, IO exception, etc.
// If that doesn't work, try to get it as a String and parse it
try {
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
droppedScribble = Scribble.parse(s);
}
catch(Exception ex2) {
// If we still couldn't get the data, tell the system we failed
e.dropComplete(false);
return;
}
}

// If we get here, we've got the Scribble object
Point p = e.getLocation(); // Where did the drop happen?
droppedScribble.translate(p.getX(), p.getY()); // Move it there
scribbles.add(droppedScribble); // add to display list
repaint(); // ask for redraw
e.dropComplete(true); // signal success!
}

// These are unused DropTargetListener methods
public void dragOver(DropTargetDragEvent e) {}
public void dropActionChanged(DropTargetDragEvent e) {}

/**
* The main method. Creates a simple application using this class. Note
* the buttons for switching between draw mode and drag mode.
**/
public static void main(String[] args) {
// Create a frame and put a scribble pane in it
JFrame frame = new JFrame("ScribbleDragAndDrop");
final ScribbleDragAndDrop scribblePane = new ScribbleDragAndDrop();
frame.getContentPane().add(scribblePane, BorderLayout.CENTER);

// Create two buttons for switching modes
JToolBar toolbar = new JToolBar();
ButtonGroup group = new ButtonGroup();
JToggleButton draw = new JToggleButton("Draw");
JToggleButton drag = new JToggleButton("Drag");
draw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scribblePane.setDragMode(false);
}
});
drag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scribblePane.setDragMode(true);
}
});
group.add(draw); group.add(drag);
toolbar.add(draw); toolbar.add(drag);
frame.getContentPane().add(toolbar, BorderLayout.NORTH);

// Start off in drawing mode
draw.setSelected(true);
scribblePane.setDragMode(false);

// Pop up the window
frame.setSize(400, 400);
frame.setVisible(true);
}
}
dw5189 2004-08-03
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.datatransfer.*; // Clipboard, Transferable, DataFlavor, etc.
import java.awt.dnd.*;
import java.util.ArrayList;

/**
* This component can operate in two modes. In "draw mode", it allows the user
* to scribble with the mouse. In "drag mode", it allows the user to drag
* scribbles with the mouse. Regardless of the mode, it always allows
* scribbles to be dropped on it from other applications.
**/
public class ScribbleDragAndDrop extends JComponent
implements DragGestureListener, // For recognizing the start of drags
DragSourceListener, // For processing drag source events
DropTargetListener, // For processing drop target events
MouseListener, // For processing mouse clicks
MouseMotionListener // For processing mouse drags
{
ArrayList scribbles = new ArrayList(); // A list of Scribbles to draw
Scribble currentScribble; // The scribble in progress
Scribble beingDragged; // The scribble being dragged
DragSource dragSource; // A central DnD object
boolean dragMode; // Are we dragging or scribbling?

// These are some constants we use
static final int LINEWIDTH = 3;
static final BasicStroke linestyle = new BasicStroke(LINEWIDTH);
static final Border normalBorder = new BevelBorder(BevelBorder.LOWERED);
static final Border dropBorder = new BevelBorder(BevelBorder.RAISED);

/** The constructor: set up drag-and-drop stuff */
public ScribbleDragAndDrop() {
// Give ourselves a nice default border.
// We'll change this border during drag-and-drop.
setBorder(normalBorder);

// Register listeners to handle drawing
addMouseListener(this);
addMouseMotionListener(this);

// Create a DragSource and DragGestureRecognizer to listen for drags
// The DragGestureRecognizer will notify the DragGestureListener
// when the user tries to drag an object
dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, // What component
DnDConstants.ACTION_COPY_OR_MOVE, // What drag types?
this);// the listener

// Create and set up a DropTarget that will listen for drags and
// drops over this component, and will notify the DropTargetListener
DropTarget dropTarget = new DropTarget(this, // component to monitor
this); // listener to notify
this.setDropTarget(dropTarget); // Tell the component about it.
}

/**
* The component draws itself by drawing each of the Scribble objects.
**/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(linestyle); // Specify wide lines

int numScribbles = scribbles.size();
for(int i = 0; i < numScribbles; i++) {
Scribble s = (Scribble)scribbles.get(i);
g2.draw(s); // Draw the scribble
}
}

public void setDragMode(boolean dragMode) {
this.dragMode = dragMode;
}
public boolean getDragMode() { return dragMode; }

/**
* This method, and the following four methods are from the MouseListener
* interface. If we're in drawing mode, this method handles mouse down
* events and starts a new scribble.
**/
public void mousePressed(MouseEvent e) {
if (dragMode) return;
currentScribble = new Scribble();
scribbles.add(currentScribble);
currentScribble.moveto(e.getX(), e.getY());
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

/**
* This method and mouseMoved() below are from the MouseMotionListener
* interface. If we're in drawing mode, this method adds a new point
* to the current scribble and requests a redraw
**/
public void mouseDragged(MouseEvent e) {
if (dragMode) return;
currentScribble.lineto(e.getX(), e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) {}

/**
* This method implements the DragGestureListener interface. It will be
* invoked when the DragGestureRecognizer thinks that the user has
* initiated a drag. If we're not in drawing mode, then this method will
* try to figure out which Scribble object is being dragged, and will
* initiate a drag on that object.
**/
public void dragGestureRecognized(DragGestureEvent e) {
// Don't drag if we're not in drag mode
if (!dragMode) return;

// Figure out where the drag started
MouseEvent inputEvent = (MouseEvent) e.getTriggerEvent();
int x = inputEvent.getX();
int y = inputEvent.getY();

// Figure out which scribble was clicked on, if any by creating a
// small rectangle around the point and testing for intersection.
Rectangle r = new Rectangle (x-LINEWIDTH, y-LINEWIDTH,
LINEWIDTH*2, LINEWIDTH*2);
int numScribbles = scribbles.size();
for(int i = 0; i < numScribbles; i++) { // Loop through the scribbles
Scribble s = (Scribble) scribbles.get(i);
if (s.intersects(r)) {
// The user started the drag on top of this scribble, so
// start to drag it.

// First, remember which scribble is being dragged, so we can
// delete it later (if this is a move rather than a copy)
beingDragged = s;

// Next, create a copy that will be the one dragged
Scribble dragScribble = (Scribble) s.clone();
// Adjust the origin to the point the user clicked on.
dragScribble.translate(-x, -y);

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧