Reputation: 31252
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);
}
}
Upvotes: 0
Views: 492
Reputation: 10994
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.
Upvotes: 1
Reputation: 320
For closing the window you can either use
frame.setVisible(false);
or you can simply call
System.exit(0);
this will terminate your process.
Upvotes: 0
Reputation: 320
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..
Upvotes: 3