setVisible true JFrame frame1 new JFrame 86 Using Classes from the Java Library

Setvisible true jframe frame1 new jframe 86 using

This preview shows page 15 - 18 out of 39 pages.

frame1.setVisible( true ); JFrame frame1 = new JFrame();
Image of page 15
8.6 Using Classes from the Java Library 311 11 12 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, check boxes, and combo boxes to the window. The components are defined using classes. Listing 8.6 gives an example of creating a graphical user interface, as shown in Figure 8.1. L ISTING 8.6 GUIComponents.java 1 import javax.swing.*; 2 3 public class GUIComponents { 4 public static void main(String[] args) { 5 // Create a button with text OK 6 JButton jbtOK = new JButton( "OK" ); 7 8 // Create a button with text Cancel 9 JButton jbtCancel = new JButton( "Cancel" ); 10 11 // Create a label with text "Enter your name: " 12 JLabel jlblName = new JLabel( "Enter your name: " ); 13 14 // Create a text field with text "Type Name Here" 15 JTextField jtfName = new JTextField( "Type Name Here" ); 16 17 // Create a check box with text Bold 18 JCheckBox jchkBold = new JCheckBox( "Bold" ); 19 20 // Create a check box with text Italic 21 JCheckBox jchkItalic = new JCheckBox( "Italic" ); 22 23 // Create a radio button with text Red 24 JRadioButton jrbRed = new JRadioButton( "Red" ); 25 26 // Create a radio button with text Yellow 27 JRadioButton jrbYellow = new JRadioButton( "Yellow" ); 28 29 // Create a combo box with several choices 30 JComboBox jcboColor = new JComboBox( new String[]{ "Freshman" , 31 "Sophomore" , "Junior" , "Senior" }); 32 33 // Create a panel to group components 34 JPanel panel = new JPanel(); 35 panel.add(jbtOK); // Add the OK button to the panel 36 panel.add(jbtCancel); // Add the Cancel button to the panel JFrame frame2 = new JFrame(); create an object invoke a method create a button create a button create a label create a text field create a check box create a check box create a radio button create a radio button create a combo box create a panel add to panel VideoNote Use classes
Image of page 16
312 Chapter 8 Objects and Classes Point C he c k Key Point 37 panel.add(jlblName); // Add the label to the panel 38 panel.add(jtfName); // Add the text field to the panel 39 panel.add(jchkBold); // Add the check box to the panel 40 panel.add(jchkItalic); // Add the check box to the panel 41 panel.add(jrbRed); // Add the radio button to the panel 42 panel.add(jrbYellow); // Add the radio button to the panel 43 panel.add(jcboColor); // Add the combo box to the panel 44 45 JFrame frame = new JFrame(); // Create a frame 46 frame.add(panel); // Add the panel to the frame 47 frame.setTitle( "Show GUI Components" ); 48 frame.setSize( 450 , 100 ); 49 frame.setLocation( 200 , 100 ); 50 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image of page 17
Image of page 18

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture