码迷,mamicode.com
首页 > 编程语言 > 详细

[Java]接受拖拽文件的窗口

时间:2016-07-23 22:54:56      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

  至于这个问题,Java的awt.dnd包下提供了许多完成这一功能的类

例如DropTarget、DropTargetListener等

  先来讲一下DropTarget类,这个类完成和拖拽、复制文件等操作和Component的关联

常用的构造方法有这些:

 DropTarget(Component c, DropTargetListener dtl)

  c:要与它关联的组件

  dtl:执行事件处理的DropTargetListener

DropTarget(Component c, int ops, DropTargetListener dtl)

  ops:默认的可接受操作

  另外两个参数和上面是一样的

可接受的操作有哪些呢?DnDConstants类中有着下面几种操作(字段):

static int ACTION_COPY
          表示“复制”操作的 int 值。
static int ACTION_COPY_OR_MOVE
          表示“复制”或“移动”操作的 int 值。
static int ACTION_LINK
          表示“链接”操作的 int 值。
static int ACTION_MOVE
          表示“移动”操作的 int 值。
static int ACTION_NONE
          表示无操作的 int 值。
static int ACTION_REFERENCE
          表示“引用”操作的 int 值(等同于 ACTION_LINK)。

  接着来谈谈DropTargetListener,API帮助文档中提供比较详细的说明,在这就不过多地介绍了,

如果只是单独的使用文件的拖拽,可以使用DropTargetAdapter这一个类,对于上面这个接口中

drop(DropTargetDropEvent) 以外的所有方法都定义了 null 实现,所以是实现接口的时候就比较方便

下面给出一个简单的接受拖拽文件的程序的源代码:

 1 package 图形界面;  
 2   
 3 import javax.swing.*;
 4 import java.util.List;
 5 import java.awt.*;
 6 import java.awt.datatransfer.DataFlavor;
 7 import java.awt.datatransfer.UnsupportedFlavorException;
 8 import java.awt.dnd.*;
 9 import java.io.File;
10 import java.io.IOException;
11   
12 /** 
13  * @author Administrator 
14  */  
15 public class 拖拽文件 extends JFrame{  
16     
17     private static final long serialVersionUID = -3081282189290446349L;
18     
19     private JTextArea jta;
20     private JScrollPane jsp;
21 
22     
23     private void init_drop(){
24         
25         new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
26             
27             @Override
28             public void drop(DropTargetDropEvent dtde) {
29                 
30                 if(dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){    //判断是否支持此文件的格式
31                     
32                     dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);    //接受该文件
33                     try {
34                         @SuppressWarnings("unchecked")
35                         List<File> list = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
36                         jta.setText(jta.getText()+list.get(list.size() - 1).getAbsolutePath() + "\r\n");
37                         
38                     } catch (UnsupportedFlavorException | IOException e) {
39                         e.printStackTrace();
40                     }
41                     
42                 }else{
43                     dtde.rejectDrop();            //拒绝该拖拽文件
44                 }
45                 
46             }
47             
48         });
49         
50     }
51     
52     private void init(){
53         
54         jta = new JTextArea();
55         jsp = new JScrollPane(jta);
56         
57         jsp.setBounds(30, 30, 200, 200);
58         jta.setForeground(Color.green);
59         jta.setRows(40);
60         
61     }
62     
63     public 拖拽文件(){
64         
65         this.init();
66         this.setTitle("拖拽文件测试");
67         this.setLayout(null);
68         this.add(jsp);
69         this.init_drop();
70         this.setBounds(420, 280, 300, 320);
71         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
72         this.setResizable(false);
73         this.setVisible(true);
74         
75     }
76     
77     public static void main(String args[]){
78         new 拖拽文件();
79     }
80     
81 }  

 

[Java]接受拖拽文件的窗口

标签:

原文地址:http://www.cnblogs.com/yyf0309/p/5699552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!