Problem Logic
Practice
Solving Building
Advance Object Oriented Programming
GUI, AWT
Course Code: CSAO-267, Sem:4
Pre: CSOO-142-Sem:2
By Zohair Ahmed
1 Practice More
By Zohair Ahmed
AWT
AWT (Abstract Window Toolkit)
2 Practice More
By Zohair Ahmed
AWT (Abstract Window Toolkit)
▸AWT is heavy weight i.e. its components are using
the resources of underlying operating system (OS).
▸Java AWT calls the native platform calls the native
platform (operating systems) subroutine for
creating API components like TextField, ChechBox,
button, etc.
▸Components All the elements like the button, text
fields, scroll bars, etc. are called components.
▸In order to place every component in a particular
position on a screen, we need to add them to a
container.
▸A container itself is a component therefore we can
add a container inside container. Window, Panel,
Frame and Dialog
3
By Zohair Ahmed
Useful Methods of Component Class
Method Description
public void add(Component c) Inserts a component on this component.
public void setSize(int width,int height) Sets the size (width and height) of the component.
public void setLayout(LayoutManager m) Defines the layout manager for the component.
public void setVisible(boolean status) Changes the visibility of the component, by default
false.
By Zohair Ahmed
Create a GUI
▸By extending Frame class (inheritance)
▸By creating the object of Frame class (association)
▸setBounds(int x-axis, int y-axis, int width, int height)
// importing Java AWT class
import java.awt.*;
// extending Frame class to our class AWTExample1
public class AWTExample1 extends Frame {
import java.awt.*;
// class AWTExample2 directly creates instance of Frame class
class AWTExample2 {
// initializing using constructor
AWTExample2() {
// creating a Frame
5 Frame f = new Frame()
By Zohair Ahmed
Event and Listener (Java Event Handling)
▸It is a mechanism to control the events and to decide what should
happen after an event occur. To handle the events, Java follows
the Delegation Event model.
▸Steps: Event Classes Listener Interfaces
1. Register the component with the Listener ActionEvent ActionListener
- public void addActionListener(ActionListener a){} MouseEvent MouseListener and
MouseMotionListener
2. Handle Event MouseWheelEve MouseWheelListener
- public void actionPerformed(ActionEvent e){} nt
KeyEvent KeyListener
▸Event handling code into one of the following places
ItemEvent ItemListener
- Within class
TextEvent TextListener
• class AEvent extends Frame implements ActionListener{ }
AdjustmentEvent AdjustmentListener
- Other class
WindowEvent WindowListener
- Anonymous class
ComponentEvent ComponentListener
ContainerEvent ContainerListener
6 FocusEvent FocusListener
By Zohair Ahmed
Swing
Java Swing
7 Practice More
By Zohair Ahmed
Event and Listener (Java Event Handling)
▸Java Swing is a part of Java Foundation Classes (JFC) that is used to
create window-based applications.
▸It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
▸Unlike AWT, Java Swing provides platform-independent and
lightweight components.
▸There are two ways to create a frame:
- By creating the object of Frame class (association)
- By extending Frame class (inheritance)
Method Description
public void add(Component c) add a component on another component.
public void setSize(int width,int height) sets size of the component.
public void setLayout(LayoutManager m) sets the layout manager for the component.
public void setVisible(boolean b) sets the visibility of the component. It is by default false.
8
By Zohair Ahmed
Form Design and Events
By Zohair Ahmed