📄 dndfnl.java
字号:
//
// DnDFile.java
// Dropped File Viewer Sample Code
// http://www5.big.or.jp/~tera/Labo/Java2/j2dnd.html
// Copyright(C) 2000 S.Terada, All rights reserved
//
import java.io.*;
import javax.swing.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class DnDFnL extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static FileDropAcceptableLabel lab = null;
//static final DataFlavor[] supportedFlavors = { null };
//static {
// try {
// supportedFlavors[0] = new DataFlavor(
// DataFlavor.javaRemoteObjectMimeType);
// } catch (Exception ex) {
// ex.printStackTrace();
// }
//}
/**
* This method initializes this
*/
private void initialize() {
this.setSize(383, 333);
this.setLocation(320, 300);
this.setTitle("Drag And Drop");
}
// Main
public static void main(String args[]) {
DnDFnL win = new DnDFnL();
win.setVisible(true);
}
// Constructor
public DnDFnL() {
initialize();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
lab = new FileDropAcceptableLabel();
getContentPane().add(lab);
}
// Drop Acceptor (Internal Class)
class FileDropAcceptableLabel extends JLabel implements DropTargetListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private DropTarget dropTarget = null;
// Constructor
public FileDropAcceptableLabel() {
super();
dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, this, true);
dropTarget.getClass();
}
// Drag Enter Event Handler
public void dragEnter(DropTargetDragEvent e){
e.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
}
// Drop Event Handler
public void drop(DropTargetDropEvent e) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
// get string data from drop
try {
Object source = e.getTransferable().getTransferData(DataFlavor.stringFlavor);
//System.out.println("source is: \n" + source);
JOptionPane.showMessageDialog(this, source);
} catch (Exception ex){
System.out.println(ex.getMessage());
}
// get file and name from drop
try {
if ((e.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
Transferable trans = e.getTransferable();
java.util.List lis = (java.util.List) trans.getTransferData(DataFlavor.javaFileListFlavor);
File file = (File) lis.get(0);
String sFileName = file.getAbsolutePath();
//System.out.println("Dropped File:" + sFileName);
JOptionPane.showMessageDialog(this, sFileName);
if ( sFileName.indexOf(".jpg") > 0 || sFileName.indexOf(".JPG") > 0 ||
sFileName.indexOf(".gif") > 0 || sFileName.indexOf(".GIF") > 0 ){
setIcon(new ImageIcon(sFileName));
//pack();
}
e.dropComplete(true);
} else {
e.dropComplete(false);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
e.dropComplete(false);
}
}
// The other events should be ignored
public void dragExit(DropTargetEvent e) {}
public void dragOver(DropTargetDragEvent e) {}
public void dropActionChanged(DropTargetDragEvent e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -