0% found this document useful (0 votes)
30 views

AWT and Swing

- AWT and Swing are Java APIs for creating graphical user interfaces (GUIs). AWT is older and less robust, while Swing is more modern and lightweight. - AWT and Swing both use components like buttons, text fields, and labels that can be arranged in containers using different layout managers. Events and listeners are used to handle user interactions with these components. - A basic AWT example creates a Frame container and adds components to it, while a basic Swing example uses JFrame and creates Swing components like JButton instead of the AWT equivalents.

Uploaded by

ruchisuru20001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

AWT and Swing

- AWT and Swing are Java APIs for creating graphical user interfaces (GUIs). AWT is older and less robust, while Swing is more modern and lightweight. - AWT and Swing both use components like buttons, text fields, and labels that can be arranged in containers using different layout managers. Events and listeners are used to handle user interactions with these components. - A basic AWT example creates a Frame container and adds components to it, while a basic Swing example uses JFrame and creates Swing components like JButton instead of the AWT equivalents.

Uploaded by

ruchisuru20001
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

AWT and Swing

Graphical User Interface

• Graphical User Interface (GUI) offers user interaction via some


graphical components. For example, our underlying Operating System
also offers GUI via window, frame, Panel, Button, Textfield, TextArea,
Listbox, Combobox, Label, Checkbox etc. These all are known as
components. Using these components, we can create an interactive
user interface for an application.
• GUI provides result to end-users in response to its raised events. It is
entirely based on events. For example, clicking on a button, closing a
window, opening a window, typing something in a text area etc. These
activities are known as events. GUI makes it easier for the end user to
use an application
AWT(Abstract Window Toolkit)

• Java AWT is an API that contains large number of classes and methods
to create and manage graphical user interface ( GUI ) applications.

• Java AWT is an API to develop GUI or window based applications in


java.
Java AWT Hierarchy
• Component class
• Component class is at the top of AWT hierarchy. It is an abstract class
that encapsulates all the attributes of visual component.
• Container
• Container is a component in AWT that contains another component
like button, text field, tables etc
• Panel
• Panel class is a concrete subclass of Container. Panel does not
contain title bar, menu bar or border. It is container that is used for
holding components.
• Window class
• Window class creates a top level window. Window does not have
borders and menu bar.
• Frame
• Frame is a subclass of Window and have resizing canvas. It is a
container that contain several different components like button, title
bar, textfield, label etc. In Java, most of the AWT applications are
created using Frame window. Frame class has two different
constructors,
Useful Methods of Component class
Java AWT Example
• To create simple awt example, you need a frame.
• There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class (association)
• AWT Example by Inheritance
• AWT Example by Association
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 package provides many event
classes and Listener interfaces for event handling.
Java Event classes and Listener interfaces
Steps to perform Event Handling
• Following steps are required to perform event handling:
• 1. Register the component with the Listener
Registration Methods
• For registering the component with the Listener, many classes provide
the registration methods. For example:
AWT UI Elements:
Java MouseListener Interface
• Methods of MouseListener interface

• public abstract void mouseClicked(MouseEvent e);


• public abstract void mouseEntered(MouseEvent e);
• public abstract void mouseExited(MouseEvent e);
• public abstract void mousePressed(MouseEvent e);
• public abstract void mouseReleased(MouseEvent e);
Swing
• Swing in java is part of Java foundation class which is
lightweight and platform independent. It is used for creating
window based applications. It includes components like
button, scroll bar, text field etc.
Difference between AWT and Swing
Example
• import javax.swing.*;
• public class example{
• public static void main(String args[]) {
• JFrame a = new JFrame("example");
• JButton b = new JButton("click me");
• b.setBounds(40,90,85,20);
• a.add(b);
• a.setSize(300,300);
• a.setLayout(null);
• a.setVisible(true);
• }
• }
Layout Manager

To arrange the components inside a container we use the


layout manager. Following are several layout managers:
1.Border layout
2.Flow layout
3.Grid Bag layout
Border Layout

• .It places components in up to five places which is top,


bottom, left, right and center.
Flow Layout

• Flow Layout simply lays the components in a row one after


the other
GridBag Layout

• GridBagLayout places the components in a grid which allows the


components to span more than one cell.

1.GridLayout(): creates a grid layout with one column per component in a row.
2.GridLayout(int rows, int columns): creates a grid layout with the given rows
and columns but no gaps between the components.
3.GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout
with the given rows and columns along with given horizontal and vertical gaps.
• Jframe f = new JFrame();
• JButton b1 = new JButton("NORTH");;

• f.add(b1, BorderLayout.NORTH);
How to write ActionListener

• 1) Implement the ActionListener interface in the class:


• public class ActionListenerExample Implements ActionListener
• 2) Register the component with the Listener:
• component.addActionListener(instanceOfListenerclass);
• 3) Override the actionPerformed() method:
1.public void actionPerformed(ActionEvent e){
2. //Write the code here
3.}

You might also like