Java Reference
In-Depth Information
Title
Title bar
Message
Click the OK button to
close the dialog box
F IGURE 1.18
“Welcome to Java!” is displayed in a message box.
import
4
5
6 public class WelcomeInMessageDialogBox {
7
import javax.swing.JOptionPane;
main method
public static void main(String[] args) {
8
// Display Welcome to Java! in a message dialog box
JOptionPane.showMessageDialog( null , "Welcome to Java!" );
display message
9
10 }
11 }
The first three lines are block comments. The first line begins with /* and the last line ends
with */ . By convention, all other lines begin with an asterisk ( * ).
This program uses the Java class JOptionPane (line 9). Java's predefined classes are
grouped into packages. JOptionPane is in the javax.swing package. JOptionPane is
imported into the program using the import statement in line 4 so that the compiler can
locate the class without the full name javax.swing.JOptionPane .
package
Note
If you replace JOptionPane in line 9 with javax.swing.JOptionPane , you
don't need to import it in line 4. javax.swing.JOptionPane is the full name for
the JOptionPane class.
The showMessageDialog method is a static method. Such a method should be invoked
by using the class name followed by a dot operator ( . ) and the method name with arguments.
Details of methods will be discussed in Chapter 5. The showMessageDialog method can be
invoked with two arguments, as shown below.
JO p tionPane.showMessageDialog( null ,
"Welcome to Java!" );
The first argument can always be null . null is a Java keyword that will be fully dis-
cussed in Chapter 8. The second argument is a string for text to be displayed.
There are several ways to use the showMessageDialog method. For the time being, you
need to know only two ways. One is to use a statement, as shown in the example:
two versions of
showMessageDialog
JOptionPane.showMessageDialog( null , x);
where x is a string for the text to be displayed.
The other is to use a statement like this one:
JOptionPane.showMessageDialog( null , x,
y, JOptionPane.INFORMATION_MESSAGE);
 
 
Search WWH ::




Custom Search