0

Here is the simple code that has two JoptionPane. currently, it has no buttons, but I want to attach button to the second JOptionPane for Yes or NO event. Moreover, when the two JOptionPanes are closed, the Frame does not close.Is there a way to force close Frame when JoptionPanes are closed.

here is my current code

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TestJoption {

    public static void main(String[] args){

        JFrame frame = new JFrame("Game");

        JOptionPane.showMessageDialog(frame, "You Won!", "Winner", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(frame, "yes No", "play again", JOptionPane.INFORMATION_MESSAGE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}
brain storm
  • 30,124
  • 69
  • 225
  • 393
  • 1
    Take a look at [this example](http://stackoverflow.com/questions/12828389/actionlistener-on-joptionpane/12829264#12829264) – MadProgrammer Nov 20 '13 at 04:55
  • @MadProgrammer: I have posted a question about HangManExample that you helped in building up. If you have time, can you help out there.http://stackoverflow.com/questions/20086180/need-assitance-for-implementing-word-guessing-game-using-swing-in-java – brain storm Nov 20 '13 at 06:12

3 Answers3

3

You can do it like this:

JOptionPane.showConfirmDialog(frame, "yes No", "play again", JOptionPane.YES_NO_OPTION);

this is popup the box with yes and no options..

sumitsabhnani
  • 320
  • 1
  • 6
1

I think you need next code:

    JOptionPane.showMessageDialog(frame, "You Won!", "Winner", JOptionPane.INFORMATION_MESSAGE);
    int result = JOptionPane.showConfirmDialog(frame, "yes No", "play again",JOptionPane.YES_NO_OPTION);
    if(result == JOptionPane.NO_OPTION){
        frame.dispose();
    }

Also read tutorial for dialogs.

alex2410
  • 10,904
  • 3
  • 25
  • 41
0

For closing the window you can either use

frame.setVisible(false);

or you can simply call

System.exit(0);

this will terminate your process.

sumitsabhnani
  • 320
  • 1
  • 6