3.1 AWT classes
AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars,etc. 2 packages – java.awt
and java.awt.event
– are commonly-used.
- The
java.awt
package contains the core AWT graphics classes:- GUI Component classes, such as
Button
,TextField
, andLabel
. - GUI Container classes, such as
Frame
andPanel
. - Layout managers, such as
FlowLayout
,BorderLayout
andGridLayout
. - Custom graphics classes, such as
Graphics
,Color
andFont
.
- GUI Component classes, such as
- The
java.awt.event
package supports event handling:- Event classes, such as
ActionEvent
,MouseEvent
,KeyEvent
andWindowEvent
, - Event Listener Interfaces, such as
ActionListener
,MouseListener
,MouseMotionListener
,KeyListener
andWindowListener
, - Event Listener Adapter classes, such as
MouseAdapter
,KeyAdapter
, andWindowAdapter
.
- Event classes, such as
AWT features include:
- A rich set of user interface components
- A robust event handling model
- Graphics and imaging tools, including space, Color and font classes.
- Layout managers, for flexible window layouts that don’t depend on a particular window size or screen resolution.
- Data transfer classes, for cut and paste through the native platform clipboard
Let’s Know in Detail
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.
- Component :- 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.
- Container :- The Container is a component in AWT that can contain another components like buttons, textfields, labels etc.The commonly-used top-level containers in AWT are Frame, Dialog and Applet.
- A component must be kept in a container. You need to identify a container to hold the components. Every container has a method called add(Component c). A container (say c) can invoke c.add(aComponent) to add aComponent into itself. For example,
- Window:- The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.
- Frame :- It provides the “main window” for your GUI application. It has a title bar (containing an icon, a title, the minimize, maximize/restore-down and close buttons), an optional menu bar, and the content display area.Creating a Frame There are two ways to create a Frame. They are,
- By extending Frame class (inheritance)
- By creating the object of Frame class (association)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<strong>1. By extending Frame class (inheritance) </strong> import java.awt.*; import java.awt.event.*; public class Demoextends Frame { public Demo() { Button btn=new Button("Hello World"); add(btn); //adding a new Button. setSize(400, 500); //setting size. setTitle("StudyTonight"); //setting title. setLayout(new FlowLayout()); //set default layout for frame. setVisible(true); //set frame visibilty true. } public static void main (String[] args) { Demota = new Demo(); //creating a frame. } } |
2. By creating the object of Frame class (association)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.awt.*; public class Demo { Demo() { Frame fm=new Frame(); //Creating a frame. Label lb = new Label("welcome to java graphics"); //Creating a label fm.add(lb); //adding label to the frame. fm.setSize(300, 300); //setting frame size. fm.setVisible(true); //set frame visibilty true. } public static void main(String args[]) { Testawt ta = new Testawt(); } |