Friday, September 28, 2012

How to Create a Simple Yes/No Dialog with a User

One of the students today in class asked me if there was some easy way to limit the choice of responses from the user when starting the Last-Coin Game. Here is a short program which demonstrates how the built-in dialog features of java can be used.

You are welcome to use this feature in your program for the Last-Coin Game (or you can come up with your own).


import javax.swing.JOptionPane;


public class YesNoDemo {

public static void main(String[] args) {


int response = JOptionPane.showConfirmDialog(null, "Click Yes or No",
                                       "Do you want to go first?", JOptionPane.YES_NO_OPTION);

if (response == JOptionPane.YES_OPTION)
System.out.println("User wants to go first");
else
System.out.println("User wants computer to go first");

}

}

No comments:

Post a Comment