我在下面张贴完整的代码,但基本上我有一个类,它有一个对话框,包含一个滚动面板,持有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);
}
}
(我试着到处寻找解决类似问题的方法,但它有点太通用了,找不到。)
只要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);
}
}
在dialog.dispose();
之后,使用System.exit(0);