Java Reference
In-Depth Information
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);
51 frame.setVisible( true );
52 }
53 }
create a frame
add panel to frame
display frame
This program creates GUI objects using the classes JButton , JLabel , JTextField ,
JCheckBox , JRadioButton , and JComboBox (lines 6-31). Then, using the JPanel class
(line 34), it then creates a panel object and adds the button, label, text field, check box, radio
button, and combo box to it (lines 35-43). The program then creates a frame and adds the
panel to the frame (line 45). The frame is displayed in line 51.
8.14
How do you create a Date for the current time? How do you display the current time?
Check
Point
8.15
How do you create a JFrame , set a title in a frame, and display a frame?
8.16
Which packages contain the classes Date , JFrame , JOptionPane , System , and
Math ?
8.7 Static Variables, Constants, and Methods
A static variable is shared by all objects of the class. A static method cannot access
instance members of the class.
Key
Point
The data field radius in the circle class is known as an instance variable . An instance vari-
able is tied to a specific instance of the class; it is not shared among objects of the same class.
For example, suppose that you create the following objects:
VideoNote
Static vs. instance
instance variable
Circle circle1 = new Circle();
Circle circle2 = new Circle( 5 );
The radius in circle1 is independent of the radius in circle2 and is stored in a differ-
ent memory location. Changes made to circle1 's radius do not affect circle2 's radius ,
and vice versa.
If you want all the instances of a class to share data, use static variables , also known as
class variables . Static variables store values for the variables in a common memory location.
Because of this common location, if one object changes the value of a static variable, all
objects of the same class are affected. Java supports static methods as well as static variables.
Static methods can be called without creating an instance of the class.
Let's modify the Circle class by adding a static variable numberOfObjects to count the
number of circle objects created. When the first object of this class is created,
numberOfObjects is 1 . When the second object is created, numberOfObjects becomes 2 .
The UML of the new circle class is shown in Figure 8.13. The Circle class defines the
instance variable radius and the static variable numberOfObjects , the instance methods
getRadius , setRadius , and getArea , and the static method getNumberOfObjects .
(Note that static variables and methods are underlined in the UML class diagram.)
static variable
static method
 
 
Search WWH ::




Custom Search