Drag操作

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class DropTargetTest {
public static void main(String[] args) {
JFrame frame = new DropTargetFrame();
frame.show();
}
}

class DropTargetFrame extends JFrame {
public DropTargetFrame() {
setTitle("DropTarget ");
setSize(300, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

Container contentPane = getContentPane();
JTextArea textArea = new JTextArea(
"Drop   items   into   this   text   area.\n ");

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

class TextDropTargetListener implements DropTargetListener {
private JTextArea textArea;

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) { // 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))
            {     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   fileList   =   (java.util.List)
                                    transferable.getTransferData(d);
                              Iterator   iterator   =   fileList.iterator();
                              while   (iterator.hasNext())
                              {     File   f   =   (File)iterator.next();
                                    textArea.append(f.getAbsolutePath()   +   "\n ");
                                    textArea.append(f.getName()   +   "\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;
                                          //   if   no   byte   ordering   mark,   we   assume
                                          //   Windows   is   the   culprit
                                    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) { // 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;
}
}

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