2014-05-02 91 views
2

前propmt用戶確認退出我有這個類如何關閉的JFrame

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); 
    } 
} 

我的問題是,如果用戶按下"No" H/SE將不會再看到幀。 我希望用戶能夠回到主框架

我該如何做到這一點?

回答

3

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
1

如果(響應== JOptionPane.YES_OPTION)這是不正確的

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); 
    }