gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:08 25 4
gpt4 key购买 nike

我从网上找到了以下关于 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();
}
}

最佳答案

在构造函数中为您的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.

关于java - 即使关闭应用程序后,程序仍在 Netbeans 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12345399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com