Java 为什么可以';即使I';我正在使用dialog.dispose()?
我在下面发布了完整的代码,但基本上我有一个类,它有一个对话框,其中包含一个包含jlist的滚动窗格。它被设置为“DISPOSE_ON_CLOSE”,我已经尝试将获取值后创建的每个变量设置为null,但是Java 为什么可以';即使I';我正在使用dialog.dispose()?,java,swing,jdialog,Java,Swing,Jdialog,我在下面发布了完整的代码,但基本上我有一个类,它有一个对话框,其中包含一个包含jlist的滚动窗格。它被设置为“DISPOSE_ON_CLOSE”,我已经尝试将获取值后创建的每个变量设置为null,但是javaw.exe将继续无限期运行,除非我强制关闭它 如果需要更多的信息,这里有一个快速的解释。此类用于创建并显示对话框,等待用户输入,然后返回所选文本。它已经做到了这一点。但由于某些原因,它在完成后继续在后台运行 这是针对应用程序的,因此让Java在后台不间断地运行并不是一个吸引人的前景。我真的
javaw.exe
将继续无限期运行,除非我强制关闭它
如果需要更多的信息,这里有一个快速的解释。此类用于创建并显示对话框,等待用户输入,然后返回所选文本。它已经做到了这一点。但由于某些原因,它在完成后继续在后台运行
这是针对应用程序的,因此让Java在后台不间断地运行并不是一个吸引人的前景。我真的不知道在这一点上还能尝试什么。下面是我的代码
package (REMOVED);
import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class PatientScrollPane implements ListSelectionListener, MouseListener {
private String currentPatient;
private JList patientList;
private JDialog dialog;
private JFrame frame;
private JScrollPane scrollPane;
private static int MAX_VISIBLE_ROW_COUNT = 15;
public void setDialog(JDialog dialog) {
this.dialog = dialog;
}
public JList getPatientList() {
return patientList;
}
public void setPatientList(JList patientList) {
this.patientList = patientList;
}
public String getCurrentPatient() {
return currentPatient;
}
public void setCurrentPatient(String currentPatient) {
this.currentPatient = currentPatient;
}
public JDialog getDialog() {
return dialog;
}
public JFrame getFrame() {
return frame;
}
public void setFrame(JFrame frame) {
this.frame = frame;
}
public JScrollPane getScrollPane() {
return scrollPane;
}
public void setScrollPane(JScrollPane scrollPane) {
this.scrollPane = scrollPane;
}
public PatientScrollPane() {
this(null);
}
public PatientScrollPane(JComponent locationRelativeToComponent) {
this(locationRelativeToComponent, new JList(fakePatientList()));
}
public PatientScrollPane(JComponent locationRelativeToComponent, JList patientList) {
this.patientList = patientList;
patientList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
frame = new JFrame("Patient List");
dialog = new JDialog(frame);
dialog.setLocationRelativeTo(locationRelativeToComponent);
patientList.addListSelectionListener(this);
patientList.addMouseListener(this);
setVisibleRowCount();
scrollPane = new JScrollPane(patientList);
dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);
dialog.pack();
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
}
private void setVisibleRowCount() {
int size = patientList.getModel().getSize();
if(size <= MAX_VISIBLE_ROW_COUNT) {
patientList.setVisibleRowCount(size);
} else {
patientList.setVisibleRowCount(MAX_VISIBLE_ROW_COUNT);
}
}
public static String[] fakePatientList() {
String[] patients = new String[20];
for(int i = 0; i < patients.length; i++) {
patients[i] = "Patient " + i;
}
return patients;
}
public static String getPatient() {
PatientScrollPane patientScrollPane = new PatientScrollPane();
String patient = patientScrollPane.getCurrentPatient();
patientScrollPane.setPatientList(null);
patientScrollPane.setDialog(null);
patientScrollPane.setCurrentPatient(null);
patientScrollPane.setFrame(null);
patientScrollPane.setScrollPane(null);
patientScrollPane = null;
return patient;
}
public static String getPatient(JComponent locationRelativeToComponent) {
PatientScrollPane patientScrollPane = new PatientScrollPane(locationRelativeToComponent);
return patientScrollPane.getCurrentPatient();
}
public static String getPatient(JComponent locationRelativeToComponent, JList patientList) {
PatientScrollPane patientScrollPane = new PatientScrollPane(locationRelativeToComponent, patientList);
String patient = patientScrollPane.getCurrentPatient();
return patient;
}
@Override
public void valueChanged(ListSelectionEvent e) {
currentPatient = (String)patientList.getSelectedValue();
}
@Override
public void mouseClicked(MouseEvent e) {
int index = patientList.locationToIndex(e.getPoint());
patientList.setSelectedIndex(index);
dialog.dispose();
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
String patient = PatientScrollPane.getPatient();
System.out.println("Chosen patient: " + patient);
}
}
包(已删除);
导入java.awt.BorderLayout;
导入java.awt.Dialog.ModalityType;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入javax.swing.JComponent;
导入javax.swing.JDialog;
导入javax.swing.JFrame;
导入javax.swing.JList;
导入javax.swing.JScrollPane;
导入javax.swing.ListSelectionModel;
导入javax.swing.WindowConstants;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
公共类PatientScrollPane实现ListSelectionListener、MouseStener{
私人病人;
私家医生;
专用JDialog对话框;
私有JFrame;
私有JScrollPane滚动窗格;
私有静态int MAX\u VISIBLE\u ROW\u COUNT=15;
公共void setDialog(JDialog对话框){
this.dialog=dialog;
}
公共JList getPatientList(){
返回病人名单;
}
公共无效setPatientList(JList patientList){
this.patientList=patientList;
}
公共字符串getCurrentPatient(){
返回病人;
}
公共无效setCurrentPatient(字符串currentPatient){
this.currentPatient=currentPatient;
}
公共JDialog getDialog(){
返回对话框;
}
公共JFrame getFrame(){
返回框;
}
公共无效集合框架(JFrame){
this.frame=frame;
}
公共JScrollPane getScrollPane(){
返回滚动窗格;
}
公共无效设置滚动窗格(JScrollPane滚动窗格){
this.scrollPane=滚动窗格;
}
公共滚动窗格(){
这个(空);
}
公共患者滚动窗格(JComponent locationRelativeToComponent){
这个(locationRelativeToComponent,新JList(fakePatientList());
}
公共PatientScrollPane(JComponent locationRelativeToComponent,JList patientList){
this.patientList=patientList;
patientList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
框架=新的JFrame(“患者列表”);
dialog=新JDialog(框架);
对话框.setLocationRelativeTo(locationRelativeToComponent);
patientList.addListSelectionListener(此);
patientList.addMouseListener(本);
setVisibleRowCount();
scrollPane=新的JScrollPane(patientList);
dialog.getContentPane().add(滚动窗格,BorderLayout.CENTER);
dialog.pack();
setDefaultCloseOperation(WindowConstants.DISPOSE\u ON\u CLOSE);
setModalityType(ModalityType.APPLICATION_MODAL);
对话框.setVisible(true);
}
私有void setVisibleRowCount(){
int size=patientList.getModel().getSize();
如果(size在dialog.dispose();
之后,使用System.exit(0);
只要JFrame存在,事件调度线程将保持运行,并将保持JVM活动,因为JFrame,顶级窗口将启动非守护进程线程
欲了解更多信息,请查看
例如
import javax.swing.JDialog;
import javax.swing.JFrame;
public class DialogAndDaemons {
public static void main(String[] args) {
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame, "Dialog");
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
这是可行的,比如main中的例子……但是这个类是从一个更大的应用程序调用的。如果我使用system.exit(0)它不会退出整个应用程序吗?如果你想让应用程序继续运行,那你为什么还要担心javaw.exe?因为即使在应用程序的其余部分完成运行之后,javaw.exe仍然在运行…但现在我觉得自己像个白痴,因为我可以调用system.exit(0)当我退出完整的应用程序时。谢谢你提供的解决方案…我将首先尝试下面发布的答案。这解决了问题,我只需要添加这行代码:frame.setDefaultCloseOperation(JFrame.exit_ON_CLOSE);谢谢!