2012-09-10 124 views
1

我从web上找到了有关Java中的拖放支持的以下代码。我测试了代码。它的工作正常。我正在使用Netbeans。我发现即使关闭了应用程序,该程序仍处于Netbeans的运行模式。程序仍然在Netbeans中运行,即使在关闭应用程序之后

有人可以解释一下吗? 在此先感谢。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.Transferable; 
import java.awt.dnd.*; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextArea; 

public class DropTest2 extends JFrame implements DropTargetListener { 

    DropTarget dt; 
    JTextArea ta; 

    public DropTest2() { 
     super("Drop Test"); 
     setSize(300, 300); 
     // addWindowListener(new BasicWindowMonitor()); 

     getContentPane().add(
       new JLabel("Drop a list from your file chooser here:"), 
       BorderLayout.NORTH); 
     ta = new JTextArea(); 
     ta.setBackground(Color.white); 
     getContentPane().add(ta, BorderLayout.CENTER); 

     // Set up our text area to recieve drops... 
     // This class will handle drop events 
     dt = new DropTarget(ta, this); 
     setVisible(true); 
    } 

    @Override 
    public void dragEnter(DropTargetDragEvent dtde) { 
     System.out.println("Drag Enter"); 
    } 

    @Override 
    public void dragExit(DropTargetEvent dte) { 
     System.out.println("Drag Exit"); 
    } 

    @Override 
    public void dragOver(DropTargetDragEvent dtde) { 
     System.out.println("Drag Over"); 
    } 

    @Override 
    public void dropActionChanged(DropTargetDragEvent dtde) { 
     System.out.println("Drop Action Changed"); 
    } 

    @Override 
    public void drop(DropTargetDropEvent dtde) { 
     try { 
      // Ok, get the dropped object and try to figure out what it is 
      Transferable tr = dtde.getTransferable(); 
      DataFlavor[] flavors = tr.getTransferDataFlavors(); 
      for (int i = 0; i < flavors.length; i++) { 
       System.out.println("Possible flavor: " + flavors[i].getMimeType()); 
       // Check for file lists specifically 
       if (flavors[i].isFlavorJavaFileListType()) { 
        // Great! Accept copy drops... 
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 
        ta.setText("Successful file list drop.\n\n"); 

        // And add the list of file names to our text area 
        java.util.List list = (java.util.List) tr.getTransferData(flavors[i]); 
        for (int j = 0; j < list.size(); j++) { 
         ta.append(list.get(j) + "\n"); 
        } 

        // If we made it this far, everything worked. 
        dtde.dropComplete(true); 
        return; 
       } // Ok, is it another Java object? 
       else if (flavors[i].isFlavorSerializedObjectType()) { 
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 
        ta.setText("Successful text drop.\n\n"); 
        Object o = tr.getTransferData(flavors[i]); 
        ta.append("Object: " + o); 
        dtde.dropComplete(true); 
        return; 
       } // How about an input stream? 
       else if (flavors[i].isRepresentationClassInputStream()) { 
        dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 
        ta.setText("Successful text drop.\n\n"); 
        ta.read(new InputStreamReader(
          (InputStream) tr.getTransferData(flavors[i])), 
          "from system clipboard"); 
        dtde.dropComplete(true); 
        return; 
       } 
      } 
      // Hmm, the user must not have dropped a file list 
      System.out.println("Drop failed: " + dtde); 
      dtde.rejectDrop(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      dtde.rejectDrop(); 
     } 
    } 

    public static void main(String args[]) { 
     new DropTest2(); 
    } 
} 

回答

1

在构造函数设置this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);的JFrameDropTest2()

按照javadocsetDefaultCloseOperation(int operation)执行以下操作方法:

Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices: 

- DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object. 
- HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects. 
- DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects. 
- EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications. 
+0

感谢Sujay :) – Dinesh

+0

@Dinesh:很高兴帮助:) – Sujay

相关问题