问答题
                                     本题的功能是监听鼠标的拖曳操作。窗口中有一个列表框,列表框中列出了当前目录的所有文件.鼠标选中一个或多个文件后拖曳出窗口,此操作的功能是将拖曳的文件复制一份在拖曳的目的目录下。
  import java.awt.*;
  import java.awt.datatransfer.*;
  import java.awt.dnd.*;
  import java.awt.event.*;
  import java.io.*;
  import java.util.*;
  import javax.swing.*;
  public class java3
  {
    public static void main(String[] args)
    {
    JFrame frame=new DragSourceFrame();
    frame.setDefaultClose()peration(JFrame.EXITON_CLOSE);
    frame.show();
    }
  }
  class DragSourceFrame extends JFrame
  {
    public DragSourceFrame()
    {
    setTitle("java3");
    setSize(WIDTH,HEIGHT);
    Comainer contentPane=getContentPane();
    File f=new File(".").getabsoluteFile();
    File[] files=f.listFiles();
    model=new DefaultListModel();
    for(int i=0;i<files.length();i++)
    try
    {
    model.addElement(files[i].getCanonicalFile());
    }
    catch (IOException exception)
    {
     JOptionPane.showMessageDialog(this,excep-tion);
    }
    fileList=new JList(model);
    contentPane.add(new JScrolIPane(fileList),
    BorderLayout.CENTER);
    contentPane.add (new JLabel("从列表中拖曳出文件"),
    BorderLayout.NORTH);
    DragSource dragSource=DragSource.getDefault-DragSource();
    dragSource.createDefaultDragGestureRecognizer(fileList,
     DnDConstants.ACTION _ COPY _ OR _MOVE,new
    DragGestureListener()
    {
    public void dragGestureRecognized(
    DragGestureEvent event)
    draggedValues = fileList.getSelectedValues();
    Transferable transferable
    =new FileListTransferable(draggedValues);
    event.startDrag(null,transferable,
    new FileListDragSourceListener());
    }
    });
    }
    private class FileListDragSourceListener implements DragSourceAdapter
    public void dragDropEnd( DragSourceDropEvent event)
    {
    if (event.getDropSuccess())
    {
    int action=event.getDropAction();
    if(action==DnDConstants.ACTION_MOVE)
    {
    for(int i=O;i<draggedValues.length;i++)
    model.removeElement(draggedValues[i]);
    }
    }
   }
   }
   private JList fileList;
   private DefaultListModel model;
   private Object[] draggedValues;
   private static final int WIDTH=300;
   private static final int HEIGHT=200;
  }
  class FileListTransferable implements Transferable
  {
    public FileListTransferable(()bject[] files)
    {
    fileList=new ArrayList(Arrays.asList(files));
    }
    public DataFlavor[]getTransferDataFlavors()
    {
    return flavors;
    }
    public boolean isDataFlavorSupported(DataFlavorflavor)
    {
    return Arrays.asList(flavors).contains(flavor);
    }
    public Object getTransferData(DataFlavor flavor)throws UnsupportedFlavorException
    {
    if(flavor.equals(DataFlavor.javaFileListFlavor)return fileList;
    else if(flavor.equals(DataFlavor.stringFlavor))
    return fileList.toString();
    else
    throw new UnsupportedFlavorException(flavor);
    }
    private static DataFlavor[]flavors=
    {
    DataFlavor.javaFileListFlavor,
    DataFlavor.stringFlavor
    };
    private java.util.List fileList;
  }
                                
                                            【参考答案】
                                            
                                            
第1处:File f=new FIle(",").getAbsoluteFile() 第2处:int i=0;i<fil......
(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)