拖动文件的问题
用过qq,飞秋的人,都应该知道,如果我要传输文件给对方的时候,我可以把文件拖动到输入框中,然后进行传输操作.
我在想,这个拖动动作,用java怎么来实现哦?
------解决方案--------------------
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.io.*;
public class test extends JFrame implements DropTargetListener{
private static final long serialVersionUID = 1L;
JLabel cmdLabel=new JLabel("你拖入的文件地址是:");
public test() {
super("frame");
}
public void runed()
{
}
public void dragEnter(DropTargetDragEvent dtde) { }
public void dragExit(DropTargetEvent dte) { }
public void dragOver(DropTargetDragEvent dtde) { }
public void drop(DropTargetDropEvent dtde) {
try {
Transferable tr = dtde.getTransferable();
if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
System.out.println("file cp");
List list = (List) (dtde.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor));
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
File f = (File) iterator.next();
this.cmdLabel.setText("你拖入的文件是:"+f.getAbsolutePath());
}
dtde.dropComplete(true);
//this.updateUI();
}else {
dtde.rejectDrop();
}
} catch (
IOException ioe) {
ioe.printStackTrace();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
}
}
public void dropActionChanged(DropTargetDragEvent dtde) { }
public static void main(String[] args){
test ts=new test();
SwingConsole.run(ts,300,200);
ts.runed();
}
}
class SwingConsole{
public static void run(final JFrame f,final int width,
final int height){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(200,200,width,height);
f.setVisible(true);
}
});
}
}
看下这个,性质差不多