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

                16.13 Dialog Boxes and the JOptionPane Class 669
 import javax.swing.*;
public class HelloWithAFrame extends JFrame
{
}
// end class HelloWithAFrame
       Apago PDF Enhancer
public HelloWithAFrame()
{
setTitle("Hello");
setSize(400, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// end HelloWithAFrame constructor
//***********************************************************
}
public static void main(String[] args)
{
HelloWithAFrame helloFrame = new HelloWithAFrame();
JOptionPane.showMessageDialog(helloFrame, "Hello, world!");
} // end main
 Figure 16.10 HelloWithAFrame program and its output
Suppose you don’t want to bother with centering the dialog box within a particular container. In that case, use null for showMessageDialog’s container argument. That causes the dialog box to display in the center of the screen. For example, this code fragment generates a screen-centered dialog box:
JOptionPane.showMessageDialog(
null, "Before starting the installation,\n" +
"shut down all applications.");
By the way, it’s very common to use null for showMessageDialog’s container argument, probably more common than using a non-null value.
The JOptionPane class needs the javax.swing package. If you’ve imported the javax.swing package for the JFrame class already, there’s no need to import it again.







































































   701   702   703   704   705