久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Swing 應(yīng)用程序 ->拖動(dòng)&放到桌面/文件夾

Swing application -gt; Drag amp; drop to the desktop / folder(Swing 應(yīng)用程序 -gt;拖動(dòng)amp;放到桌面/文件夾)
本文介紹了Swing 應(yīng)用程序 ->拖動(dòng)&放到桌面/文件夾的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

Mac 的 Finder、Windows 的資源管理器確實(shí)拖拽 &將 Swing 應(yīng)用程序中的特定項(xiàng)目拖放到桌面和文件夾中,如何獲得我丟棄的前一條路徑?

When Finder of Mac, Explorer of Windows did drag & drop of a specific item in the Swing application to a desktop and a folder, How to get the former path which I dropped?

我很高興教我一個(gè)必要的課程和方法.

I am happy teach me a necessary class and method.

推薦答案

這是一個(gè)小程序,但它適用于任何框架或窗口:

This is an applet, but it works with any frame or window:

public class DropApplet extends Applet implements DropTargetListener {
    // . . .
    private DropTarget dropTarget;
    // . . .

    public void init() {
        if (dropTarget == null) {
            dropTarget = new DropTarget(this, this);
        }
        // . . .
    }

    public void dragEnter(DropTargetDragEvent arg0) {
        // nothing
    }

    public void dragOver(DropTargetDragEvent arg0) {
        // nothing
    }

    public void dropActionChanged(DropTargetDragEvent arg0) {
        // nothing
    }

    public void dragExit(DropTargetEvent arg0) {
        // nothing
    }

    public void drop(DropTargetDropEvent evt) {
        final List result = new ArrayList();
        int action = evt.getDropAction();
        evt.acceptDrop(action);
        try {
            Transferable data = evt.getTransferable();
            DataFlavor flavors[] = data.getTransferDataFlavors();
            if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                List<File> list = (List<File>) data.getTransferData(
                    DataFlavor.javaFileListFlavor);
                processFiles(list);
            }
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            evt.dropComplete(true);
            repaint();
        }
    }

    private void processFiles(List<File> files) throws IOException {
        // . . .
    }
}

將文件從 Swing 應(yīng)用程序拖到桌面稍微復(fù)雜一些,并不是所有的 Swing 組件都能做到這一點(diǎn).

Dragging files from a swing application to the desktop is slightly more complicated, and not all swing components are able to do that.

這是一個(gè)包含文件的 JList 示例.您可以將文件從 finder/explorer 拖放到此列表中,然后將這些文件拖到另一個(gè)位置.

Here is an example with a JList containing files. You can drop files from the finder/explorer into this list and then drag these files to another location.

package dnd;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
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.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

public class DnDFrame extends javax.swing.JFrame implements DropTargetListener {

    private DefaultListModel listModel = new DefaultListModel();
    private DropTarget dropTarget;

    /** Creates new form DnDFrame */
    public DnDFrame() {
        initComponents();
        dropTarget = new DropTarget(list, this);
        list.setModel(listModel);
        list.setDragEnabled(true);
        list.setTransferHandler(new FileTransferHandler());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Files:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(jLabel1, gridBagConstraints);

        jScrollPane1.setViewportView(list);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(jScrollPane1, gridBagConstraints);

        pack();
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    // End of variables declaration

    public void dragEnter(DropTargetDragEvent arg0) {
        // nothing
        }

    public void dragOver(DropTargetDragEvent arg0) {
        // nothing
        }

    public void dropActionChanged(DropTargetDragEvent arg0) {
        // nothing
        }

    public void dragExit(DropTargetEvent arg0) {
        // nothing
        }

    public void drop(DropTargetDropEvent evt) {
        int action = evt.getDropAction();
        evt.acceptDrop(action);
        try {
            Transferable data = evt.getTransferable();
            if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                List<File> files = (List<File>) data.getTransferData(
                        DataFlavor.javaFileListFlavor);
                for (File file : files) {
                    listModel.addElement(file);
                }
            }
        } catch (UnsupportedFlavorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            evt.dropComplete(true);
        }
    }

    private class FileTransferHandler extends TransferHandler {

        @Override
        protected Transferable createTransferable(JComponent c) {
            JList list = (JList) c;
            List<File> files = new ArrayList<File>();
            for (Object obj: list.getSelectedValues()) {
                files.add((File)obj);
            }
            return new FileTransferable(files);
        }

        @Override
        public int getSourceActions(JComponent c) {
            return MOVE;
        }
    }

    private class FileTransferable implements Transferable {

        private List<File> files;

        public FileTransferable(List<File> files) {
            this.files = files;
        }

        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{DataFlavor.javaFileListFlavor};
        }

        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.equals(DataFlavor.javaFileListFlavor);
        }

        public Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException, IOException {
            if (!isDataFlavorSupported(flavor)) {
                throw new UnsupportedFlavorException(flavor);
            }
            return files;
        }
    }
}

這篇關(guān)于Swing 應(yīng)用程序 -&gt;拖動(dòng)&amp;放到桌面/文件夾的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 中文字幕免费 | 精品视频一区二区三区在线观看 | 9色网站| 欧美精品久久久久 | 亚洲一区二区久久 | 一级a性色生活片久久毛片波多野 | 久久国产综合 | 久久1区 | 激情五月婷婷综合 | 在线观看视频91 | 色婷婷精品 | 日韩 欧美 综合 | 精品影院 | 日韩欧美综合在线视频 | 免费国产视频在线观看 | 午夜影院网站 | 五月激情综合 | 天天操网 | 久久国产精彩视频 | 久久亚洲国产精品 | 色综合天天天天做夜夜夜夜做 | 久久综合婷婷 | 久久久久久免费精品一区二区三区 | 国产精品自拍视频 | 久久精品一区 | 亚洲精品久久久久久久久久久 | 免费黄色大片 | 亚洲国产精品99久久久久久久久 | 亚洲免费视频在线观看 | 亚洲成人网在线观看 | 91久久久久久 | 国产伦精品一区二区三区视频金莲 | 日韩高清三区 | 麻豆视频在线免费观看 | av在线一区二区三区 | 在线伊人网 | 秋霞精品 | 美女福利视频网站 | 国产片侵犯亲女视频播放 | 中文字幕av亚洲精品一部二部 | 黄色免费av |