Domanda

I have this class

package com.javacodegeeks.snippets.desktop;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class Main1 {

    public static void main(String args[]) {

        JFrame frame = new JFrame("Window Listener");

        WindowListener listener = new WindowAdapter() {

            public void windowClosing(WindowEvent w) {
                int response = JOptionPane.showConfirmDialog(null, " Are you sure you want isuue the ticket?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        };

        frame.addWindowListener(listener);

        frame.setSize(300, 300);

        frame.setVisible(true);
    }
}

My problem is if the user press "No" h/se will not see frame again. I want the user to be able to go back to the main frame

How do I do this?

È stato utile?

Soluzione

Do

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Altri suggerimenti

if (response == JOptionPane.YES_OPTION) this is not correct

    int confirm = JOptionPane.showConfirmDialog(null,"are you sure you want to Exit","Exit",JOptionPane.YES_NO_OPTION, 1);
    if(confirm == 0) // you have to do this {
    close();
    System.exit(0);
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top