After having the basic idea of GUI, let us know how many Java AWT Components classes exist with java.awt package. This knowledge is essential to go further in the tutorial.
As you can observe from the above hierarchy, Component is the super class of all Java components and is declared as abstract. That is, we cannot create objects of Component class directly.
Following is the class signature
public abstract class Component extends Object implements ImageObserver, MenuContainer, Serializable
Properties of Java AWT Components
- A Component object represents a graphical interactive area displayable on the screen that can be used by the user.
- Any subclass of a Component class is known as a component. For example, button is a component.
- Only components can be added to a container, like frame.
Important methods of Component class
Following are the important methods, practiced very often, of Component class that can be used by all the sub classes (components).
- setEnabled(boolean): The parameter false makes the component disabled so that it does not respond to user interactions (say, clicks).
- setBounds(): Using this method, the programmer can give his own size to the component in terms of width and height and also the location where he can place the component in the container. This method is not used often as the programmer prefers to place the component in the container using layout managers.
- getWidth(): The programmer can obtain the width of the component.
- getHeight(): The programmer can obtain the height of the component.
- paint(): Used very extensively to draw graphics. This method is called implicitly when the frame is created or resized.
- repaint(): Used by the programmer to call the paint() method explicitly anywhere in the program.
- setBackground(): Used to give a background color to a component.
- setForeground(): Used to give a foreground color to a component.
- setFont(): Used to give a font to a component to display the text.
- getSize(): Returns the size of the component.
- setSize(): Used to give the size to the component.
- update(): The is method is called implicitly by the repaint(). A call to this method clears the earlier drawings existing on the container.