Inputboxes

Want to make an input box rather than using the "command line"? The complete tutorial is here: @http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html

But here is a summary: code format="java" import javax.swing.*; import java.awt.event.*;

public class SampleInputDialog{ public static void main(String[] args){

String str = JOptionPane.showInputDialog(null, "Enter some text : ", "Input", 1); if(str != null) JOptionPane.showMessageDialog(null, "You entered the text : " + str, "Output", 1); else JOptionPane.showMessageDialog(null, "You pressed the cancel button", "Duh", 1); } } code In order for it work, you need those two "import" statements before your class declaration.

Of course, if you want to do "If" statements, you'll have to use the "EQUALS" function for strings: code format="java" if ((str.equals("red"))   {    JOptionPane.showMessageDialog(null, "You guessed the color", "Wow!", JOptionPane.INFORMATION_MESSAGE);   } else    {    JOptionPane.showMessageDialog(null, "Sorry, wrong color", "Fail", JOptionPane.INFORMATION_MESSAGE);   }

code Oh, and if you want to make sure you check for lowercase and uppercase... code format="java" lowercase_str = str.toLowerCase; code Then you'll do your "if" statements on "lowercase_str" instead

One note: if you'd like to have multiple lines in your MessageDialog or InputDialog, use "\n" to say "return" in your code. This code: code format="java" JOptionPane.showMessageDialog(null, "Welcome to my Game.\n" +		         "You will have three tries to guess\n" +                          "a number between 1 and 10.\n\n" +                          "Press OK to start.", "Game", 1); code displays this box: