0% found this document useful (0 votes)
1 views35 pages

Java Unit-3 Lecture-22,23 24

This document provides an overview of Java AWT and Swing, focusing on their components, event handling, and layout management. It outlines course objectives and outcomes related to understanding AWT, Swing, and layout design. Additionally, it includes examples of code for creating GUI applications using AWT and Swing, as well as homework questions for further study.

Uploaded by

goddragon939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views35 pages

Java Unit-3 Lecture-22,23 24

This document provides an overview of Java AWT and Swing, focusing on their components, event handling, and layout management. It outlines course objectives and outcomes related to understanding AWT, Swing, and layout design. Additionally, it includes examples of code for creating GUI applications using AWT and Swing, as well as homework questions for further study.

Uploaded by

goddragon939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT OF AIT - CSE


Bachelor of Engineering (CSE)
Programming in Java (21CSH-244)
By: Kushagra Agrawal (E13465)

Lecture -22, 23 & 24 DISCOVER . LEARN . EMPOWER


AWT, SWING, LAYOUT
Chapter Course Objectives

● To understand about AWT.


● To understand Type of Swing.
16.
● To understand about Layout.

Chapter Course Outcomes

After completion of this course, student will be able to


● Learn the basic AWT.
16. ● Learn the basic about Swing.
● Different Layout design

2
AWT, SWING, LAYOUT
Java AWT Tutorial
•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
Cont..
Container
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
Creating Frame
Create a Frame class object:
Frame f= new Frame();

Create a Frame class object and pass its title:


Frame f= new Frame(“My Frame”);

Used Frame to user defined class:


Class MyFrame extends Frame
MyFrame f=new MyFrame();
AWT Example
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30); // setting button position
add(b); //adding button into frame
setSize(300,300); //frame size 300 width and 300 height
setLayout(null); //no layout manager
setVisible(true); //now frame will be visible, by default not visible
}
public static void main(String args[]){
First f=new First();
}}
Event Delegation Model
•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.
Event Handling Model of AWT

Event object

Event handling
methods

Event source Event listener


Action Events on Buttons

ActionEvent

actionPerformed(..)

Button ActionListener
Steps to perform Event Handling

Following steps are required to perform event handling:


Register the component with the Listener
Registration Methods
For registering the component with the Listener, many classes provide the
registration methods. For example:
Button
public void addActionListener(ActionListener a){}
MenuItem
public void addActionListener(ActionListener a){}
TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
Cont..
TextArea
public void addTextListener(TextListener a){}
Checkbox
public void addItemListener(ItemListener a){}
Choice
public void addItemListener(ItemListener a){}
List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Java ActionListener Interface

• The Java ActionListener is notified whenever you click on the button or


menu item. It is notified against ActionEvent.
• The ActionListener interface is found in java.awt.event package.
• It has only one method: actionPerformed().

• actionPerformed() method
• The actionPerformed() method is invoked automatically whenever
you click on the registered component.
• public abstract void actionPerformed(ActionEvent e);
Java ActionListener Interface(cont..)

How to write ActionListener


• The common approach is to implement the ActionListener. If you
implement the ActionListener class, you need to follow 3 steps:

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:
• public void actionPerformed(ActionEvent e)
•{
• //Write the code here
•}
import java.awt.*;
Ex: import java.awt.event.*;
//1st step
public class ActionListenerExample implements ActionListener{
static TextField tf ;
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
//2nd step
b.addActionListener(new ActionListenerExample());
f.add(b);
f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
//3rd step
public void actionPerformed(ActionEvent e){
tf.setText("Welcome You.");
} }
• import java.awt.*;
• import java.awt.event.*;
• //1st step f.add(b);
• public class ActionListenerExample implements ActionListener{ f.add(tf);
f.add(tf1);
• static TextField tf,tf1 ; f.add(l);
• static Label l; f.setSize(400,400);
• public static void main(String[] args) { f.setLayout(null);
f.setVisible(true);
• Frame f=new Frame("ActionListener Example");
}
• tf=new TextField(); //3rd step
• tf.setBounds(50,50, 150,20); public void actionPerformed(ActionEvent e){
int a = Integer.parseInt(tf.getText());
• tf1=new TextField();
int b = Integer.parseInt(tf1.getText());
• tf1.setBounds(220,50, 150,20); int c = a + b;
• l = new Label(""); l.setText("Their sum is = " + String.valueOf(c));
• l.setBounds(100, 120, 85, 20); }
}
• Button b=new Button("Click Here");
• b.setBounds(50,100,60,30);
• //2nd step
• b.addActionListener(new ActionListenerExample());

18
Java Swing
JAVA Swing
•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.
•The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
Difference between AWT and Swing
Hierarchy of Java Swing classes
Simple Java Swing Example
import javax.swing.*;
public class FirstSwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Layout
Java LayoutManagers
• The LayoutManagers are used to arrange components in a particular
manner.
• LayoutManager is an interface that is implemented by all the classes
of layout managers.
• There are following classes that represents the layout managers:
 java.awt.BorderLayout
 java.awt.FlowLayout
 java.awt.GridLayout
 java.awt.CardLayout
 java.awt.GridBagLayout
 javax.swing.BoxLayout
 javax.swing.GroupLayout
 javax.swing.ScrollPaneLayout
 javax.swing.SpringLayout etc.
Java BorderLayout
The BorderLayout is used to arrange the components in five
regions: north, south, east, west and center. Each region (area)
may contain one component only. It is the default layout of
frame or window. The BorderLayout provides five constants for
each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

Constructors of BorderLayout class:


BorderLayout(): creates a border layout but with no gaps
between the components.
BorderLayout(int hgap, int vgap): creates a border layout with
the given horizontal and vertical gaps between the components.
Border Layout Ex:
import java.awt.*; f.add(b1,BorderLayout.NORTH);
import javax.swing.*; f.add(b2,BorderLayout.SOUTH);
public class Border { f.add(b3,BorderLayout.EAST);
JFrame f; f.add(b4,BorderLayout.WEST);
Border(){ f.add(b5,BorderLayout.CENTER);
f=new JFrame(); f.setSize(300,300);
JButton b1=new JButton("NORTH"); f.setVisible(true);
JButton b2=new JButton("SOUTH"); }
JButton b3=new JButton("EAST"); public static void main(String[] args) {
JButton b4=new JButton("WEST"); new Border();
JButton b5=new JButton("CENTER"); } }
Java GridLayout
The GridLayout is used to arrange the components in rectangular grid. One
component is displayed in each rectangle.
Constructors of GridLayout class:
GridLayout(): creates a grid layout with one column per component in a
row.
GridLayout(int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout
with the given rows and columns alongwith given horizontal and vertical
gaps.
Grid Layout Ex: f.add(b1);
import java.awt.*;
import javax.swing.*; f.add(b2);

public class MyGridLayout{ f.add(b3);

JFrame f; f.add(b4);

MyGridLayout(){ f.add(b5);

f=new JFrame(); f.add(b6);

JButton b1=new JButton("1"); f.add(b7);

JButton b2=new JButton("2"); f.add(b8);

JButton b3=new JButton("3"); f.add(b9);

JButton b4=new JButton("4");


JButton b5=new JButton("5"); f.setLayout(new GridLayout(3,3));

JButton b6=new JButton("6"); //setting grid layout of 3 rows and 3 columns

JButton b7=new JButton("7"); f.setSize(300,300);

JButton b8=new JButton("8"); f.setVisible(true);

JButton b9=new JButton("9"); }


public static void main(String[] args) {
new MyGridLayout();
} }
Java FlowLayout
The FlowLayout is used to arrange the components in a line, one after another
(in a flow). It is the default layout of applet or panel.
Fields of FlowLayout class
public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
Constructors of FlowLayout class
FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a
default 5 unit horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
Java FlowLayout Ex:
import java.awt.*; f.add(b1);
f.add(b2);
import javax.swing.*;
f.add(b3);
f.add(b4);
public class MyFlowLayout{
f.add(b5);
JFrame f;
MyFlowLayout(){ f.setLayout(new FlowLayout(FlowLayout.RIGHT));
f=new JFrame(); //setting flow layout of right alignment

JButton b1=new JButton("1"); f.setSize(300,300);


JButton b2=new JButton("2"); f.setVisible(true);
JButton b3=new JButton("3"); }
JButton b4=new JButton("4"); public static void main(String[] args) {
new MyFlowLayout();
JButton b5=new JButton("5");
}
}
Summary
. Discussed about AWT.
. Discussed about Swing components and container.
. Discussed different layouts.

32
Home Work
Q1. Difference between Applet and Swing?

Q2.Difference between Container and Components?

33
References

Online Video Link


• https://nptel.ac.in/courses/106/105/106105191/
• https://www.coursera.org/courses?query=java
• https://www.coursera.org/specializations/object-oriented-programming
• https://www.youtube.com/watch?v=aqHhpahguVY

Text Book
• Herbert Schildt (2019), “Java The Complete Reference, Ed. 11, McGraw-Hill .

34
THANK YOU

For queries
Email: kushagra.e13465@cumail.in

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy