Java Reference
In-Depth Information
where str is a String variable and stringExpression is an expression evaluating to a
string. When this statement executes, a dialog box containing stringExpression appears on
the screen prompting the user to enter the data. (The stringExpression usually informs the
user what to enter.) The data entered is returned as a string and assigned to the variable str .
Consider the following statement (suppose that name is a String variable):
name = JOptionPane.showInputDialog("Enter your name and press OK");
When this statement executes, the dialog box shown in Figure 3-7 appears on the screen.
(The arrow and the words Text Field are not part of the dialog box.)
Text Field
FIGURE 3-7 Input dialog box prompting the user to input name
The user enters the name in the white area, called a text field, as shown in Figure 3-8.
Enter name and
press OK
FIGURE 3-8 Input dialog box with user input
After you enter a name and click the OK button (or press the Enter key), the dialog box
disappears and the entered name is assigned to the variable name . In this case, the string
"Ashley Mann" is assigned to name .
Now that you know how to use an input dialog box,
let's turn to the method
showMessageDialog for output.
The syntax to use the method showMessageDialog is:
JOptionPane.showMessageDialog(parentComponent,
messageStringExpression,
boxTitleString, messageType);
Search WWH ::




Custom Search