Swing拖拽效果

Swing的拖拽貌似挺复杂的效果,其实实现起来还是很容易的。只需要对控件实现DropTargetListener接口即可。
运行效果如下图:


拖拽文件到该空间后,我们获得文件基本信息,并显示,更具体的显示效果您可以自己去控制。拖拽文件后的效果:


程序完成代码:
package com.zakisoft.drug;

/**
 *   @author   zakisoft.com
 */

import java.awt.Container;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class DropTargetTest {
	private DropTargetTest() {
		JFrame frame = new DropTargetFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		new DropTargetTest();
	}
}

class DropTargetFrame extends JFrame {

	private static final long serialVersionUID = 6197629178772816152L;

	public DropTargetFrame() {
		setTitle("Swing拖拽的例子 Zakisoft.com");
		setSize(600, 400);
		setLocationRelativeTo(getOwner()); 
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		Container contentPane = getContentPane();
		JTextArea textArea = new JTextArea(
				"请拖拽文件到该区域.\n ");

		new DropTarget(textArea, new TextDropTargetListener(textArea));
		contentPane.add(new JScrollPane(textArea), "Center");
	}
}

class TextDropTargetListener implements DropTargetListener {
	public TextDropTargetListener(JTextArea ta) {
		textArea = ta;
	}

	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) {
	}

	public void dropActionChanged(DropTargetDragEvent event) {
		if (!isDragAcceptable(event)) {
			event.rejectDrag();
			return;
		}
	}

	public void drop(DropTargetDropEvent event) {
		if (!isDropAcceptable(event)) {
			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)) {
					List<?> fileList = (List<?>) transferable
							.getTransferData(d);
					Iterator<?> iterator = fileList.iterator();
					while (iterator.hasNext()) {
						File f = (File) iterator.next();
						textArea.append(f + "\n ");
					}
				} else if (d.equals(DataFlavor.stringFlavor)) {
					String s = (String) transferable.getTransferData(d);
					textArea.append(s + "\n ");
				} else if (d.isMimeTypeEqual("text/plain ")) {
					String charset = d.getParameter("charset ");
					InputStream in = (InputStream) transferable
							.getTransferData(d);

					boolean more = true;
					int ch;
					if (charset.equals("ascii ")) {
						do {
							ch = in.read();
							if (ch != 0 && ch != -1)
								textArea.append(" " + (char) ch);
							else
								more = false;
						} while (more);
					} else if (charset.equals("unicode ")) {
						boolean littleEndian = true;
						do {
							ch = in.read();
							int ch2 = in.read();
							if (ch != -1 && littleEndian)
								ch = (ch & 0xFF) | ((ch2 & 0xFF) << 8);
							if (ch == 0xFFFE)
								littleEndian = false;
							else if (ch != 0 && ch != -1)
								textArea.append(" " + (char) ch);
							else
								more = false;
						} while (more);
					}

					textArea.append("\n ");
				}
			} catch (Exception e) {
				textArea.append("Error:   " + e + "\n ");
			}
		}
		event.dropComplete(true);
	}

	public boolean isDragAcceptable(DropTargetDragEvent event) {
		return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
	}

	public boolean isDropAcceptable(DropTargetDropEvent event) {
		return (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0;
	}

	private JTextArea textArea;
}

你可能感兴趣的:(java,swing,F#)