[go: up one dir, main page]

0% found this document useful (0 votes)
112 views6 pages

Bca-501 Unit-Ii - Java Awt

The document discusses Java AWT (Abstract Window Toolkit), including that it is an API to develop GUI applications in Java and is platform-dependent. It describes common AWT components and classes like Container, Window, Panel, and Frame. It also covers event handling in AWT using listener interfaces and registering components with listeners.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views6 pages

Bca-501 Unit-Ii - Java Awt

The document discusses Java AWT (Abstract Window Toolkit), including that it is an API to develop GUI applications in Java and is platform-dependent. It describes common AWT components and classes like Container, Window, Panel, and Frame. It also covers event handling in AWT using listener interfaces and registering components with listeners.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BY :- ANKIT KUMAR BALIYAN

DEPARTMENT OF COMPUTER APPLICATION

UNIT-II
(AWT- ABSTRACT WINDOW TOOLKIT)

Java AWT
Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based
applications in java.

Java AWT components are platform-dependent i.e. components are displayed according to
the view of operating system. AWT is heavyweight i.e. its components are using the
resources of OS.

The java.awt package provides classes for AWT api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.

Java AWT Hierarchy


The hierarchy of Java AWT classes are given below

Container
BY :- ANKIT KUMAR BALIYAN
DEPARTMENT OF COMPUTER APPLICATION
The Container is a component in AWT that can contain another components like buttons,
textfields, labels etc. The classes that extends Container class are known as container such
as Frame, Dialog and Panel

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.

Panel

The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.

Frame

The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.

Useful Methods of Component class

Method Description

public void add(Component c) inserts a component on this component.

public void setSize(int sets the size (width and height) of the
width,int height) component.

public void defines the layout manager for the


setLayout(LayoutManager m) component.

public void setVisible(boolean changes the visibility of the component,


status) by default false.
BY :- ANKIT KUMAR BALIYAN
DEPARTMENT OF COMPUTER APPLICATION

Java AWT Example

To create simple awt example, you need a frame. There are two ways to create a frame in
AWT.

o By extending Frame class (inheritance)

o By creating the object of Frame class (association)

AWT Example by Inheritance

1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10. }
11. public static void main(String args[]){
12. First f=new First();
13. }}

AWT Example by Association


1. import java.awt.*;
2. class First2{
3. First2(){
4. Frame f=new Frame();
5. Button b=new Button("click me");
6. b.setBounds(30,50,80,30);
7. f.add(b);
BY :- ANKIT KUMAR BALIYAN
DEPARTMENT OF COMPUTER APPLICATION
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. }
12. public static void main(String args[]){
13. First2 f=new First2();
14. }}

Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on button,
dragging mouse etc. The java.awt.event package provides many event classes and
Listener interfaces for event handling.

15. Java Event classes and Listener interfaces

Event Classes Listener Interfaces

ActionEvent ActionListener

MouseEvent MouseListener and MouseMotionListener

MouseWheelEvent MouseWheelListener

KeyEvent KeyListener

ItemEvent ItemListener

TextEvent TextListener

AdjustmentEvent AdjustmentListener

WindowEvent WindowListener

ComponentEvent ComponentListener

ContainerEvent ContainerListener

FocusEvent FocusListener
BY :- ANKIT KUMAR BALIYAN
DEPARTMENT OF COMPUTER APPLICATION

Registration Methods
For registering the component with the Listener, many classes provide the registration
methods. For example:

o Button
o public void addActionListener(ActionListener a){}
o MenuItem
o public void addActionListener(ActionListener a){}
o TextField
o public void addActionListener(ActionListener a){}
o public void addTextListener(TextListener a){}
o TextArea
o public void addTextListener(TextListener a){}
o Checkbox
o public void addItemListener(ItemListener a){}
o Choice
o public void addItemListener(ItemListener a){}
o List
o public void addActionListener(ActionListener a){}
o public void addItemListener(ItemListener a){}

Java event handling by implementing ActionListener

1. import java.awt.*;
2. import java.awt.event.*;
3. class AEvent extends Frame implements ActionListener{
4. TextField tf;
5. AEvent(){
6.
7. //create components
8. tf=new TextField();
BY :- ANKIT KUMAR BALIYAN
DEPARTMENT OF COMPUTER APPLICATION
9. tf.setBounds(60,50,170,20);
10. Button b=new Button("click me");
11. b.setBounds(100,120,80,30);
12.
13. //register listener
14. b.addActionListener(this);//passing current instance
15.
16. //add components and set size, layout and visibility
17. add(b);add(tf);
18. setSize(300,300);
19. setLayout(null);
20. setVisible(true);
21. }
22. public void actionPerformed(ActionEvent e){
23. tf.setText("Welcome");
24. }
25. public static void main(String args[]){
26. new AEvent();
27. }
28. }

You might also like