如何使 JOptionsPane(没有面板)用 X 关闭程序

How to make the JOptionsPane (Without a panel) close the program with X

所以我知道在这个网站上和整个互联网上有很多类似的问题,但我一直无法找到确切的答案或满足我想要完成的任务的答案。

我的程序中有多个 JOptionPane。所有这些 JOptionsPane 的顶角都有一个 "X"。目前它们的功能与任何其他 JOptionPane 的默认按钮一样。

我希望程序在用户单击 "X" 时退出。使用任何其他按钮,屏幕将关闭,但使用 "X" 我想引发 System.exit(0) 类型的事件。

我尝试使用 if 语句来实现这一点,例如:

int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, options, "default");

        if(result==JOptionPane.CLOSED_OPTION){
            System.exit(0);
        }

但我发现无论用户点击哪个按钮,整个程序都会退出。即使他们点击了我的其他 JButton 之一,它有一个动作侦听器,程序仍然会退出。我将 post 此特定 GUI 的更完整图片:

public static void displayGUI(){//Method to display the GUI. 

        final JButton buttonCreate = new JButton("Create Return");
        final JButton buttonConfirm = new JButton("Confirm");

        buttonConfirm.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){


                if(output.getSize()>0){
                    JOptionPane.getRootFrame().dispose();   
                }else if(verifyBatch==true){
                    JOptionPane.getRootFrame().dispose();
                }else if(verifyBatch==false && output.getSize()==0){
                    JOptionPane.showMessageDialog(null, "You haven't added any transactions to confirm" +
                            " and you have no previously completed batches!");
                }
            }           
        });

        buttonCreate.addActionListener(new ActionListener(){

            public void actionPerformed(final ActionEvent ae){

                if(verifyBatch==true){

                    initialScreenDecisions="NONE";//The user did not choose to add any entry details to the output list.
                    MainWriter.finishedCounter=true;//The boolean counter to trigger that the return is finished goes to true.
                    while(MainWriter.entryDetails.size()>0){//Removes all entry details from the input list.
                        MainWriter.entryDetails.remove(0);
                    }

                    while(output.size()>0){//Removes all entry details from the output list..
                        output.remove(0);
                    }

                    JOptionPane.getRootFrame().dispose();

                }else{

                    JOptionPane.showMessageDialog(null, "There are no completed batches!");     

                }
            }
        });

        //Creates a JOptionPane for the first GUI featuring 7 buttons and 2 lists..

        final Object[] options = new Object[] {buttonConfirm,buttonCreate};

        int result = JOptionPane.showOptionDialog(null, getPanel(),"Return Builder", JOptionPane.OK_CANCEL_OPTION, 
                JOptionPane.PLAIN_MESSAGE, null, options, "default");

        if(result==JOptionPane.CLOSED_OPTION){
            System.exit(0);
        }
    }

那么从这个例子来看,如何让"X"并且只有"X"退出整个程序呢?我还有许多其他 JOptionsPane 需要实施类似措施。

with "X" I want to cause a System.exit(0) type of event.

首先,JVM 会在您最后一次 window (JOptionPane) 关闭后自动退出。这大概就是

的原因

Even if they click one of my other JButtons that has an action listener the program still quits.

其次,用户通常不会期望关闭一个对话框就会存在程序,从GUI设计的角度考虑。您通常有父级 JFrame 并且对话框对其进行补充,它们不是 GUI 的 "driving force"。

如果我创建一个框架只是为了停止 JVM 的 auto-closing,您会看到 JOptionPane 确实为按下 "X" 提供了不同的 return 值对于其他选项:

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setVisible(true);
    int result = JOptionPane.showConfirmDialog(null, "AA");
    System.out.println(result);
    if (result == JOptionPane.CLOSED_OPTION)
        System.exit(0);
}