The class JFrame provides various methods to control the attributes of a window. The methods provided by the class JFrame are:
public JFrame()
public JFrame(String s)
public void setSize(int w, int h)
public void setTitle(String s)
public void setVisible(Boolean b)
public void setDefaultCloseOperation(int operation)
public void addWindowListener(WindowEvent e)
Teaching Tip
|
Descriptions of these methods can be found in Table 6-1 in the text. A more detailed description of JFrame’s methods can be found at:
|
One way to create a window you can declare an object of the type JFrame, instantiate the object, and then use the various methods listed above to manipulate the window. In this case, the object created can use the various applicable methods of the class.
Another way is to create the class containing the application program by extending the definition of the class JFrame; to build the class containing the application program “on top of” the class JFrame, utilizes the mechanism ofinheritance. Inheritance means that a new class can be derived from or based on an already existing class.
When using inheritance, the class containing your application program has more than one method. In addition to the method main, it has at least one other method that will be used to create a window object containing the required GUI components (labels, text fields, etc). This additional method is called aconstructor; it is a method of a class that is automatically executed when an object of the class is created. Typically, it is used to initialize an object and its name is always the same as the name of the class. Because inheritance is an important concept in programming languages such as Java, we the second way of creating a window is described in detail in this chapter. The definition of the class JFrame is extended by using the modifier and reserved word extends.
Teaching Tip
|
Chapter 11 discusses the principles of inheritance in detail. Constructors are covered in detail in Chapter 8.
|
The class JFrame is contained in the package javax.swing. An important property of inheritance is that the class (subclass) that extends the definition of an existing class—called a superclass—inherits all the properties of the superclass. The methods setTitle, setSize, setVisible, and setDefaultCloseOperation are methods of the class JFrame, and these methods can be inherited by its subclasses.
No comments:
Post a Comment