Using JOptionPane to get input
Another way to get input from the user is using the class JoptionPane obtained from the javax.swing package. JoptionPane make it easy to create a standard dialog box that provides a value to the user or inform something.
Give the following code,
import javax.swing.JOptionPane;
public class GetInputFromKeyboard
public static void main (String [] args) (
String name = "";
name = JoptionPane.showInputDialog ( "Please enter your name");
String msg = "Hello" + name + "!";
JOptionPane.showMessageDialog (null, msg);
)
)
produce output:
Statement first,
import javax.swing.JOptionPane;
Explain that we import the class from the javax.swing package JoptionPane.
Can also write,
import javax.swing .*;
next statement,
name = JOptionPane.showInputDialog ( "Please enter your name");
creating a JOptionPane input dialog, which displays a dialog with a message, a
textfield and the OK button as shown. Results from the dialogue and String is stored
in the variable name.
Now we create a welcome message, which will be stored in the variabe msg,
String msg = "Hello" + name + "!";
Next line is a dialog that displays a message and have the OK button,
JOptionPane.showMessageDialog (null, msg);
Using JOptionPane to get input
Monday, July 6, 2009Posted by hendry at 8:26 PM
Labels: Java Desktop
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment