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
, andยLabel
. - GUI Container classes, such asย
Frame
ย andยPanel
. - Layout managers, such asย
FlowLayout
,ยBorderLayout
ย andยGridLayout
. - Custom graphics classes, such asย
Graphics
,ยColor
ย andยFont
.
- GUI Component classes, such asย
- Theย
java.awt.event
ย package supports event handling:- Event classes, such asย
ActionEvent
,ยMouseEvent
,ยKeyEvent
ย andยWindowEvent
, - Event Listener Interfaces, such asย
ActionListener
,ยMouseListener
,ย MouseMotionListener
,ยKeyListener
ย andยWindowListener
, - Event Listener Adapter classes, such asย
MouseAdapter
,ยKeyAdapter
, andยWindowAdapter
.
- 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(); } |