2012年8月23日
package radar;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DragFrame extends JFrame {
private boolean startDrag = false;
private Point p = null;
public static void main(String[] args) {
DragFrame df = new DragFrame();
// df.setUndecorated(true);
df.setAlwaysOnTop(true);
df.setSize(400, 400);
df.setVisible(true);
}
public DragFrame() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = true;
p = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
startDrag = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point p1 = e.getPoint();
Point p2 = getLocation(null);
p2.x += p1.x - p.x;
p2.y += p1.y - p.y;
setLocation(p2);
}
});
}
}
posted @
2012-08-23 20:18 争一代雄风 阅读(132) |
评论 (0) |
编辑 收藏
package com.util;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
public class DragJFrame extends MouseAdapter {
Point loc = null;
Point tmp = null;
boolean isDragged = false;
Frame frame=null;
JFrame jFrame=null;
Component compoent;
/*public DragFrame(Component compoent,Frame frame){
this.compoent=compoent;
this.frame=frame;
this.setDragable(this.compoent,this.frame);
System.out.println("frame");
}*/
/**
* compoent
*/
public DragJFrame(Component compoent,JFrame jFrame){
this.compoent=compoent;
this.jFrame=jFrame;
this.setDragable(this.compoent,this.jFrame);
System.out.println("jframe");
}
private void setDragable(Component compoent, final JFrame jFrame) {
compoent.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
isDragged = false;
}
public void mousePressed(java.awt.event.MouseEvent e) {
tmp = new Point(e.getX(), e.getY());
isDragged = true;
}
});
compoent.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent e) {
if (isDragged) {
loc = new Point(jFrame.getLocation().x + e.getX() - tmp.x,
jFrame.getLocation().y + e.getY() - tmp.y);
jFrame.setLocation(loc);
}
}
});
}
}
posted @
2012-08-23 20:15 争一代雄风 阅读(128) |
评论 (0) |
编辑 收藏
2012年8月21日
在网上找了个代码:
package com.zwh.droptarget;
/**
@version 1.02 2004-08-25
@author Cay Horstmann
*/
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
/**
* This is a test class to test drag and drop behavior. Drop items into the text
* area to see the MIME types of the drop target.
*/
public class DropTargetTest {
public static void main(String[] args) {
JFrame frame = new DropTargetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
* This frame contains a text area that is a simple drop target.
*/
class DropTargetFrame extends JFrame {
public DropTargetFrame() {
setTitle("DropTarget");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JTextArea textArea = new JTextArea("Drop items into this text area.\n");
new DropTarget(textArea, new TextDropTargetListener(textArea));
add(new JScrollPane(textArea), "Center");
}
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
}
/**
* This listener displays the properties of a dropped object.
*/
class TextDropTargetListener implements DropTargetListener {
/**
* Constructs a listener.
*
* @param aTextArea
* the text area in which to display the properties of the
* dropped object.
*/
public TextDropTargetListener(JTextArea aTextArea) {
textArea = aTextArea;
}
public void dragEnter(DropTargetDragEvent event) {
int a = event.getDropAction();
if ((a & DnDConstants.ACTION_COPY) != 0)
textArea.append("ACTION_COPY\n");
if ((a & DnDConstants.ACTION_MOVE) != 0)
textArea.append("ACTION_MOVE\n");
if ((a & DnDConstants.ACTION_LINK) != 0)
textArea.append("ACTION_LINK\n");
if (!isDragAcceptable(event)) {
event.rejectDrag();
return;
}
}
public void dragExit(DropTargetEvent event) {
}
public void dragOver(DropTargetDragEvent event) {
// you can provide visual feedback here
}
public void dropActionChanged(DropTargetDragEvent event) {
if (!isDragAcceptable(event)) {
event.rejectDrag();
return;
}
}
public void drop(DropTargetDropEvent event) {
if (!isDropAcceptable(event)) {
//拒绝 Drop。
event.rejectDrop();
return;
}
event.acceptDrop(DnDConstants.ACTION_COPY);
Transferable transferable = event.getTransferable();
DataFlavor[] flavors = transferable.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++) {
DataFlavor d = flavors[i];
textArea.append("MIME type=" + d.getMimeType() + "\n");
try {
if (d.equals(DataFlavor.javaFileListFlavor)) {
java.util.List<File> fileList = (java.util.List<File>) transferable
.getTransferData(d);
for (File f : fileList) {
textArea.append(f + "\n");
// System.out.println("是个文件夹");
}
} else if (d.equals(DataFlavor.stringFlavor)) {
String s = (String) transferable.getTransferData(d);
textArea.append(s + "\n");
}
} catch (Exception e) {
textArea.append(e + "\n");
}
}
textArea.append("\n");
event.dropComplete(true);
}
public boolean isDragAcceptable(DropTargetDragEvent event) {
// usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
}
public boolean isDropAcceptable(DropTargetDropEvent event) {
// usually, you check the available data flavors here
// in this program, we accept all flavors
return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
}
private JTextArea textArea;
}
posted @
2012-08-21 21:03 争一代雄风 阅读(551) |
评论 (0) |
编辑 收藏