Page 702 - Introduction to Programming with Java: A Problem Solving Approach
P. 702

                668 Chapter 16 GUI Programming Basics
 A message dialog box:
An input dialog box:
A confirmation dialog box:
   Apago PDF Enhancer
   Figure 16.9 Three types of JOptionPane dialog boxes
Implementation
To create a message dialog box, call the showMessageDialog method like this: JOptionPane.showMessageDialog(<container>, <message>);
You need to prefix the showMessageDialog call with “JOptionPane dot” because showMessageDialog is a class method in the JOptionPane class. Remember—call instance methods using “<reference-variable> dot” syntax, and call class methods using “<class-name> dot” syntax.
In the above showMessageDialog call, the message argument specifies the text that appears in the dialog box. The container argument specifies the container that surrounds the dialog box. The dialog box displays in the center of that container.
Note the showMessageDialog call in Figure 16.10’s HelloWithAFrame program. We use helloFrame for showMessageDialog’s container argument. What type of container is it? As you can see from the code, helloFrame is an instance of the HelloWithAFrame class, and the HelloWithAFrame class extends the JFrame container. Therefore, by inheritance, helloFrame is a JFrame container. And consequently, the dialog box displays in the center of the program’s JFrame con- tainer. Verify this by looking at Figure 16.10’s output.
























































































   700   701   702   703   704