Java Reference
In-Depth Information
In Figure 8-8, the main() method in lines 17 through 37 is required in order
to run the program stub. Line 22 sets the Motif Look and Feel inside a try block
so that the UIManager does not generate an exception. Line 27 displays an
appropriate error message box if the UIManager cannot set the Look and Feel
(Box 1 from Table 8-2 on page 491).
17
public static void main ( String [] args )
18
{
19
//set the look and feel of the interface
20
try
21
{
22
UIManager.setLookAndFeel ( "com.sun.java.swing.plaf.motif.MotifLookAndFeel" ) ;
23
24
}
25
catch ( Exception e )
26
{
27
JOptionPane.showMessageDialog ( null ,
"The UIManager could not set the Look and Feel for this application." , "Error" ,
JOptionPane.INFORMATION_MESSAGE ) ;
28
}
29
30
BillPayer f = new BillPayer () ;
31
f.setDefaultCloseOperation ( JFrame.DO_NOTHING_ON_CLOSE ) ;
32
f.setSize ( 450,300 ) ;
33
f.setTitle ( "Crandall Power and Light Customer Payments" ) ;
34
f.setResizable ( false ) ;
35
f.setLocation ( 200,200 ) ;
36
f.setVisible ( true ) ;
37
}
38
39
public BillPayer ()
40
{
41
}
42
43
public void actionPerformed ( ActionEvent e )
44
{
45
}
46 }
FIGURE 8-8
Line 30 constructs an instance of the JFrame, and the lines following set the
attributes. The setResizable() method in line 34 prohibits the user from resiz-
ing the window and disables the Maximize button. The setLocation() method
in line 35 sets the location of the object relative to the parent object, which in
this case is the user's screen.
Recall from Chapter 7 that you used the setDefaultCloseOperation() method
(line 31 in Figure 8-8) to automatically close a Swing application when the user
clicked the Close button. Because the BillPayer program specifies that the Close
button should display a dialog box asking if the user really wants to exit the
program and submit the file, the constant, DO_NOTHING_ON_CLOSE , is used as
the method's argument. Therefore, the programmer can override the
windowClosing event.
Finally, you must stub in the constructor and an actionPerformed() method
because the BillPayer class uses a BillPayer() constructor method and implements
the ActionListener.
The following steps create a stub that will display the BillPayer window at
run time.
Search WWH ::




Custom Search