Java Unit-3 Lecture-22,23 24
Java Unit-3 Lecture-22,23 24
2
AWT, SWING, LAYOUT
Java AWT Tutorial
•Java AWT (Abstract Window Toolkit) is an API to develop GUI or
window-based applications in java.
•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();
Event object
Event handling
methods
ActionEvent
actionPerformed(..)
Button ActionListener
Steps to perform Event Handling
• 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..)
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
JFrame f; f.add(b4);
MyGridLayout(){ f.add(b5);
32
Home Work
Q1. Difference between Applet and Swing?
33
References
Text Book
• Herbert Schildt (2019), “Java The Complete Reference, Ed. 11, McGraw-Hill .
34
THANK YOU
For queries
Email: kushagra.e13465@cumail.in