3

I'd like to change the code below to present a yes or no option when a user clicks on 'X' but I'm afraid my java newbie skills don't stretch to it yet. Any suggestions please? I'd like to keep the code below as intact as possible in order to see what needs to be done differently for future reference.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class WindowExit extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
    JOptionPane.showMessageDialog( null, "Are you sure you want to close?" );
    System.exit(0);
}
}
4

3 に答える 3

11

Use showConfirmDialog as follows:

int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", "Close?",  JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
   System.exit(0);
}
于 2011-03-08T17:20:05.273 に答える
6

Look at the docs. There is a JOptionPane.YES_NO_OPTION which you can pass as a parameter.

于 2011-03-08T17:18:19.837 に答える
2
public static int showConfirmDialog(Component parentComponent,
                                    Object message,
                                    String title,
                                    int optionType)

With an optionType of JOptionPane.YES_NO_OPTION

于 2011-03-08T17:19:09.910 に答える