我发布了下面的完整代码,但基本上我有一个类,其中包含一个包含滚动窗格的对话框,其中包含一个jlist。它被设置为“DISPOSE_ON_CLOSE”,并且我已经尝试将获得的值后创建的每个变量设置为null,但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);
}
}
(我试图寻找类似问题的解决方案的高低,但它太通用了,无法找到。)
答案 0 :(得分:4)
只要您的JFrame存在,事件调度线程将继续运行并且将保持JVM活动,因为JFrame是顶级窗口启动非守护程序线程。
有关详情,请查看here。
例如,......
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);
}
}
答案 1 :(得分:3)
dialog.dispose();
之后,请使用System.exit(0);