【问题标题】:Why can't Java close properly even though I'm using dialog.dispose()?为什么即使我使用了 dialog.dispose(),Java 也不能正确关闭?
【发布时间】:2011-10-17 02:19:30
【问题描述】:

我在下面发布了完整的代码,但基本上我有一个类,它有一个对话框,其中包含一个包含 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);
    }
}

(我试图从高处和低处寻找类似问题的解决方案,但它太笼统而无法找到。)

【问题讨论】:

    标签: java swing jdialog


    【解决方案1】:

    只要你的 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);
       }
    }
    

    【讨论】:

    • 这解决了问题,我只需要添加这行代码:frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);谢谢!
    【解决方案2】:

    dialog.dispose();之后,使用System.exit(0);

    【讨论】:

    • 这可以工作,例如 main... 但是这个类是从一个更大的应用程序中调用的。如果我使用 system.exit(0) 它不会退出整个应用程序吗?
    • 如果您希望应用程序继续运行,那么您为什么要担心 javaw.exe?
    • 因为即使在应用程序的其余部分完成运行之后,javaw.exe 仍在运行......但现在我觉得自己像个白痴,因为当我完全退出时我可以调用 system.exit(0)应用。所以谢谢你的解决方案......不过,我将首先尝试下面发布的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 2011-02-10
    • 2021-02-07
    • 2022-08-11
    • 2011-07-26
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多