开发者

java stop people from closing

hi i have a full screen program which i dont want people to close unless they have a password i have this code at the moment

public void windowClosing(WindowEvent arg0)
{
    System.out.println("HERE");
    String inputValue = JOptionPane.showInputDialog(开发者_如何学JAVA"Please input the closeword");

    if (inputValue != "closeplz")
    {

    }
} 

in the if statement i want it to stop the method so that the program doesent close. any help would be greatly aprecheated thanks ste


You have to call

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

on (or within) the JFrame instance. Then the frame will not close unless you do it manually, though windowClosing() will still be called. Inside it, you can then conditionally call

System.exit(1);

which will end the application. Be sure to do any necessary cleanup first.


Check out Closing an Applicaton for a simple class to help you with this. You would need to provide the custom close action that prompts the user for the password.

Using your simple example the code would be:

Action ca = new AbstractAction()
{
    public void actionPerformed(ActionEvent e)
    {
        JFrame frame = (JFrame)e.getSource();

        String inputValue = JOptionPane.showInputDialog("Please input the closeword");


        if (! inputValue.equals("closeplz"))
        {
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        }
    }
};

CloseListener cl = new CloseListener(ca);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜