7.20. Chapter Summary

 
[Page 247 ( continued )]

7.18. (Optional GUI) Creating Windows

When you develop programs to create graphical user interfaces, you will use Java classes such as JFrame , JButton , JRadioButton , JComboBox , and JList to create frames , buttons, radio buttons , combo boxes, lists, and so on. Listing 7.14 is an example that creates two windows using the JFrame class. The output of the program is shown in Figure 7.28.

Figure 7.28. The program creates two windows using the JFrame class.
(This item is displayed on page 248 in the print version)


Listing 7.14. TestFrame.java (Using Java API Classes)
(This item is displayed on pages 247 - 248 in the print version)
 1   import   javax.swing.JFrame; 2 3   public class   TestFrame { 4   public static void   main(String[] args) { 5  JFrame frame1 =   new   JFrame();  6 frame1.setTitle(   "Window 1"   ); 

[Page 248]
 7 frame1.setSize(   200   ,   150   ); 8 frame1.setLocation(   200   ,   100   ); 9 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 10 frame1.setVisible(   true   ); 11 12  JFrame frame2 =   new   JFrame();  13 frame2.setTitle(   "Window 2"   ); 14 frame2.setSize(   200   ,   150   ); 15 frame2.setLocation(   410   ,   100   ); 16 frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 frame2.setVisible(   true   ); 18 } 19 } 

This program creates two objects of the JFrame class (lines 5, 12) and then uses the methods setTitle , setSize , setLocation , setDefaultCloseOperation , and setVisible to set the properties of the objects. The setTitle method sets a title for the window (lines 6, 13). The setSize method sets the window's width and height (lines 7, 14). The setLocation method specifies the location of the window's upper-left corner (lines 8, 15). The setDefaultCloseOperation method terminates the program when the frame is closed (lines 9, 16). The setVisible method displays the window. You can add graphical user interface components , such as buttons, labels, text fields, combo boxes, lists, and menus , to the window. The components are defined using classes. GUI programming will be introduced in Part 3, "GUI Programming."

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net