Many applications use windows or
dialog boxes
(also called
dialogs
) to display output.
Typically, dialog boxes are windows in which programs display important messages to users.
Class
JOptionPane
provides prebuilt dialog boxes that enable programs to display windows containing messages—such windows are called
message dialogs
Click
here
for codes
Package
javax.swing
contains many classes that help you create
graphical user interfaces
(GUIs).
GUI components
facilitate data entry by a program’s user and presentation of outputs to the user.
JOptionPane
method
showMessageDialog
displays a dialog box containing a message.
Requires two arguments.
The first helps the Java application determine where to position the dialog box.
If the first argument is
null
, the dialog box is displayed at the center of your screen.
The second argument is the
String
to display in the dialog box.
JOptionPane
method
showMessageDialog
is a
static
method.
Such methods often define frequently used tasks.
Typically called by using method’s class name followed by a dot (
.
) and the method name, as in
ClassName
.
methodName
(
arguments
)
Notice that you do not create an object of class
JOptionPane
to use its
static
method
showMessageDialog
An
input- dialog
allows the user to enter data into a program.
JOptionPane
method
showInputDialog
displays an input dialog.
Contains a prompt and a field (known as a
text field
) in which the user can enter text
Method
showInputDialog
(line 11) returns a
String
containing the characters typed by the user
If you press the dialog’s
Cancel
button or press the
Esc
key, the method returns
null
static
String
method
format
returns a formatted
String.
Method
format
works like method
System.out.printf
, except that
format
returns the formatted
String
rather than displaying it in a command window.