即使在关闭应用程序之后,程序仍在Netbeans中运行

时间:2012-09-10 04:38:45

标签: java drag-and-drop netbeans-7

我在网上找到了以下关于Java中拖放支持的代码。我测试了代码。它的工作正常。我正在使用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 个答案:

答案 0 :(得分:1)

在构造函数中为 JFrame 设置this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DropTest2()

根据javadoc,方法setDefaultCloseOperation(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.