[英]Program is still running in Netbeans, even after closing the Application
I have found the following code from web regarding the drag and drop support in Java. 我从Web上找到了以下有关Java拖放支持的代码。 I tested the code. 我测试了代码。 Its working fine. 它的工作正常。 I am using Netbeans. 我正在使用Netbeans。 I found that even after I closed the application, the program is still in running mode in Netbeans. 我发现即使关闭应用程序后,该程序在Netbeans中仍处于运行模式。
Can anybody explain this? 有人可以解释吗? Thanks in advance. 提前致谢。
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();
}
}
Set this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for your JFrame in your constructor: DropTest2() 用于构造函数中的JFrame : DropTest2()
According to the javadoc , the method setDefaultCloseOperation(int operation)
does the following: 根据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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.