2015年計(jì)算機(jī)二級(jí)考試JAVA考前練習(xí)綜合應(yīng)用題

字號(hào):

四、綜合應(yīng)用題(共18分)
    本題的功能是監(jiān)聽鼠標(biāo)的拖曳操作。窗口中有一個(gè)列表框,列表框中列出了當(dāng)前目錄的所有文件,鼠標(biāo)選中一個(gè)或多個(gè)文件后拖曳出窗口,此操作的功能是將拖曳的文件復(fù)制一份在拖曳的目的目錄下。
    import java.a(chǎn)wt.*;
    import java.a(chǎn)wt.datatransfer.*;
    import java.a(chǎn)wt.dnd.*;
    import java.a(chǎn)wt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    public class java3
    {
    public static void main(String[]args)
    {
    JFrame frame=new DragSourceFrame();
    frame.setDefauhCloseOperation(JFrame.EXIT_
    0N_CLoSE);
    frame.show();
    }
    }
    class DragSoureeFrame extends JFrame
    {
    public DragSourceFrame()
    {
    setTitle("java3");
    setSize(WlDTH,HElGHT);
    Container contentPane=getContentPane();
    File f=new File(".").getabsoluteFile();
    File[]files=f.listFiles();
    model=new DefaultListModel();
    for(int i=0;i  try
    {
    model.a(chǎn)ddElement(files[i].getCanonicalFile());
    }
    catch(IOException exception)
    {
    JOptionPane.showMessageDialog(this,exeep-
    tion);
    }
    fileList=new JList(model);
    contentPane.a(chǎn)dd(new JScrollPane(fileList),
    BorderLayout.CENTER);
    contentPane.a(chǎn)dd(new JLabel("從列表中拖曳出文
    件"),
    BorderLayout.NoRTH);
    DragSource dragSource=DragSource.getDefauh-
    DragSource();
    dragSource.createDefaultDragGestureRecognizer
    (fileList,
    DnDConstants. ACTION_COPY_0R_
    MOVE,new
    DragGestureListener()
    {
    public void dragGestureRecognized(
    DragGestureEvent event)
    {
    draggedValues=fileList.getSelectedValues();
    Transferable transferable
    =new FiteListTransferable(draggedValues);
    evenr.startDrag(null,transferable,
    new FileListDragSourceListener());
    }
    });
    }
    private class FileListDragSourceListener imple-
    ments DragSourceAdapter
    {
    public void dragDropEnd(DragSourceDropEvent e-
    vent)
    {
    if(event.getDropSuccess())
    {
    int action=event.getDropAction();
    if(action= =DnDConstants.ACTl0N MOVE)
    {
    for(int i=0;i  model.removeElement(draggedValues[i]);
    }
    }
    }
    }
    private JList fileList;
    private DefauhListModel model;
    private Object[]draggedValues;
    private static final int WIDTH=300;
    private static final int HEIGHT=200;
    }
    class FileListTransferable implements Transferable
    {
    public FileListTransferable(Object[]files)
    {
    fileList=new ArrayList(Arrays.a(chǎn)sList(files));
    }
    public DataFlavor[]getTransferDataFlavors()
    {
    return flavors;
    public boolean isDataFlavorSupported(DataFlavor
    flavor)
    {
    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. j avaFileListFlavor,
    DataFlavor. stringFlavor
    };
    private java. util. List fileList;
    四、綜合應(yīng)用題
    第1處:File f—new File(".").getAbsoluteFile()
    第2處:int i=0;i  第3處:private class FileListDragSoureeListener ex-
    tends DragSourceAdapter
    【解析】第1處是通過絕對(duì)路徑創(chuàng)建一個(gè)File對(duì)象f;第2處是files中f文件所在目錄下的所有文件名列表,此處就是遍歷這些文件名;第3處是定義了一個(gè)FileListDragSoureeListener類繼承用于接收拖動(dòng)源事件的抽象適配器類 DragSoureeAdapter。