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

Java Module 5

This document discusses Java AWT and Swing GUI frameworks. It covers the key classes, components, containers and their hierarchies in both frameworks. Examples are provided to demonstrate creating simple GUI applications using AWT and Swing.

Uploaded by

Shadow Monarch
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)
30 views

Java Module 5

This document discusses Java AWT and Swing GUI frameworks. It covers the key classes, components, containers and their hierarchies in both frameworks. Examples are provided to demonstrate creating simple GUI applications using AWT and Swing.

Uploaded by

Shadow Monarch
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/ 17

Object Oriented Programming Using Java 22MCA22

MODULE 5
GUI PROGRAMMING AND APPLETS
Java AWT (Abstract Window Toolkit)
• Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface
(GUI) or windows-based applications in Java.
• Java AWT components are platform-dependent
i.e. components are displayed according to the view of operating system.
• AWT is heavy weight i.e. its components are using the resources of underlying operating
system (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.

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.
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. It is basically a screen where the where the components are
placed at their specific locations. Thus it contains and controls the layout of components.
• There are four types of containers in Java AWT: Window Panel Frame Dialog
Window:

Page 1
Object Oriented Programming Using Java 22MCA22

• Window is a top-level container that represents a graphical window or dialog box.


• The Window class extends the Container class, which means it can contain other
components, such as buttons, labels, and text fields.
Panel:
• Panel is a container class in Java.
• It is a lightweight container that can be used for grouping other components together
within a window or a frame. Frame:
• The Frame is the container that contains the title bar and border and can have menu bars.
Dialog:
• A dialog box is a temporary window an application creates to retrieve user input.

Java AWT Example


• To create simple AWT example, you need a frame. There are two ways to create a GUI
using Frame in AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class (association)

AWT Example by Inheritance


• Let's see a simple example of AWT where we are inheriting Frame class.
• Here, we are showing Button component on the Frame.

Example:
import java.awt.*;
public class AWTExample1 extends Frame
{AWTExample1() {
Button b = new Button("Click Me!!");
b.setBounds(30,100,80,30);
add(b); setSize(300,300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true);
}
public static void main(String args[])
{ AWTExample1 f = new
AWTExample1();
}
}

Output

Page 2
Object Oriented Programming Using Java 22MCA22

AWT Example by Association


• Let's see a simple example of AWT where we are creating instance of Frame class.
• Here, we are creating a TextField, Label and Button component on the Frame.
Example
import java.awt.*; class AWTExample2
{
AWTExample2() {
Frame f = new Frame();
Label l = new Label("Employee id:");
Button b = new Button("Submit");
TextField t = new TextField();
l.setBounds(20, 80, 80, 30);
t.setBounds(20, 100, 80, 30);
b.setBounds(100, 100, 80, 30);
f.add(b)
f.add(l);
f.add(t);
f.setSize(400,300);
f.setTitle("Employee info");
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]) {
AWTExample2 awt_obj = new AWTExample2();
}
}

Output

Page 3
Object Oriented Programming Using Java 22MCA22

SWING
• Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract
Window Toolkit [AWT].
• Swing 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 platformindependent 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

Page 4
Object Oriented Programming Using Java 22MCA22

Swing Containers
• Containers are an integral part of SWING GUI components.
• A container provides a space where a component can be located.
• A Container in AWT is a component itself and it provides the capability to add a
component to itself.
• Following are certain noticable points to be considered.
• Sub classes of Container are called as Container.
• For example, JPanel, JFrame and JWindow.
• Container can add only a Component to itself.
• A default layout is present in each container which can be overridden using setLayout
method.
• Following is the list of commonly used containers while designed GUI using SWING.
Panel:
• JPanel is the simplest container. It provides space in which any other component can be
placed, including other panels.
Frame:
• A JFrame is a top-level window with a title and a border.
Window:
• A JWindow object is a top-level window with no borders and no menubar.

Swing Components
• A component is an independent visual control and Java Swing Framework contains a
large set of these components which provide rich functionalities and allow high level of
customization.
• They all are derived from JComponent class.
• All these components are lightweight components.
• This class provides some common functionality like pluggable look and feel, support for
accessibility, drag and drop, layout, etc.
• A container holds a group of components.
• It provides a space where a component can be managed and displayed.
• Containers are of two types:
o Top level Containers
o Lightweight Containers
• Top level Containers
– It inherits Component and Container of AWT.
– It cannot be contained within other containers.
– Heavyweight.
– Example: JFrame, JDialog, JApplet
• Lightweight Containers
– It inherits JComponent class.
– It is a general purpose container.
– It can be used to organize related components together.
– Example: JPanel

JFrame
• The javax.swing.JFrame class is a type of container which inherits the java.awt.Frame
class.
• JFrame works like the main window where components like labels, buttons, textfields are
added to create a GUI.
• Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.

Page 5
Object Oriented Programming Using Java 22MCA22

Example
import javax.swing.JFrame;
public class JFrameExample {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
frame.setSize(300, 300); frame.setVisible(true);
}
}

Output

JDialog
• The JDialog control represents a top level window with a border and a title used to take some form
of input from the user. It inherits the Dialog class. • Unlike JFrame, it doesn't have maximize and
minimize buttons.
• Commonly used Constructors

Page 6
Object Oriented Programming Using Java 22MCA22

Example:
import javax.swing.*;
import java.awt.*;
public class DialogExample
{DialogExample() {
JFrame f= new JFrame();
JDialog d = new JDialog(f , "RNSIT", true);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String
args[]){new DialogExample();
}
}

JPanel
• The JPanel is a simplest container class. It provides space in which an application can
attach any other component.
• It inherits the JComponents class.
• It doesn't have title bar.
JPanel class declaration
public class JPanel extends JComponent implements Accessible
Commonly used Constructors:

Page 7
Object Oriented Programming Using Java 22MCA22

import java.awt.*;
import javax.swing.*;
public class PanelExample
{PanelExample() {
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1);
panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String
args[]){new PanelExample();
}
}

JButton
• We use JButton class to create a push button on the UI.
• The button can include some display text or images.
• It yields an event when clicked and double-clicked.
• We can implement a JButton in the application by calling one of its constructors.
Syntax:
JButton okBtn = new JButton(“Click”);

Page 8
Object Oriented Programming Using Java 22MCA22

Commonly used Constructors:

Example:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class testswing extends
JFrame{testswing() {
JButton bt1 = new JButton("click");
setLayout(new FlowLayout());
setSize(400, 400) add(bt1);
setVisible(true);
}
public static void main(String[]
args){new testswing();
}
}

JLabel
• In Java, Swingtoolkit contains a JLabel Class.
• It is under package javax.swing.JLabel class.
• It is used for placing text in a box.
• Only Single line text is allowed and the text can not be changed directly.
• The JLabel Contains 4 constructors.
o JLabel()
o JLabel(String s)
o JLabel(Icon i)
o JLabel(String s, Icon i, int horizontalAlignment)
Example
class SLabel {
public static void main(String args[]) {
JFrame label_f= new JFrame("Label Demo");
JLabel label_l1, label_l2;
label_l1=new JLabel("Welcome to studytonight.com");
label_l1.setBounds(50,50, 200,30);
label_l2=new JLabel("How are You?");
label_l2.setBounds(50,100, 200,30);
label_f.add(label_l1);
label_f.add(label_l2);
label_f.setSize(300,300);
label_f.setLayout(null);
label_f.setVisible(true);
}
}

Page 9
Object Oriented Programming Using Java 22MCA22

JTextField
• The object of a JTextField class is a text component that allows the editing of a single
line text. It inherits JTextComponent class.
Commonly used Constructors:

Example
import javax.swing.*;
class TextFieldExample {
public static void main(String args[]) {
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField();
t1.setBounds(50,100, 200,30);
f.add(t1);
f.setSize(400,400);

Page 10
Object Oriented Programming Using Java 22MCA22

f.setLayout(null);
f.setVisible(true);
}
}

JTextArea
 The object of a JTextArea class is a multi line region that displays text. It allows the editing
of multiple line text. It inherits JTextComponent class.
Commonly used Constructors:

Example
import javax.swing.*;
public class TextAreaExample
{TextAreaExample() {
JFrame f= new JFrame();
JTextArea area=new JTextArea();
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);

Page 11
Object Oriented Programming Using Java 22MCA22

f.setVisible(true);
}
public static void main(String args[])
{new TextAreaExample();
}
}

Layout Managers
• The LayoutManagers are used to arrange components in a particular manner.
• The Java LayoutManagers facilitates us to control the positioning and size of the
components in GUI forms.
• LayoutManager is an interface that is implemented by all the classes of layout managers.
• There are the following classes that represent the layout managers
o java.awt.BorderLayout
o java.awt.FlowLayout
o java.awt.GridLayout
o java.awt.CardLayout
o java.awt.GridBagLayout
o javax.swing.BoxLayout
o javax.swing.GroupLayout
o javax.swing.ScrollPaneLayout
o javax.swing.SpringLayout etc.

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 a frame or window. The BorderLayout provides five constants for each region:
o public static final int NORTH
o public static final int SOUTH
o public static final int EAST
o public static final int WEST
o public static final int CENTER

Page 12
Object Oriented Programming Using Java 22MCA22

Example
import java.awt.*;
import javax.swing.*;
public class Border {
JFrame f; Border()
{
f = new JFrame();
JButton b1 = new JButton("NORTH");
JButton b2 = new JButton("SOUTH");
JButton b3 = new JButton("EAST");
JButton b4 = new JButton("WEST");
JButton b5 = new JButton("CENTER");
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[]
args){new Border();
}
}

GridLayout
• The Java GridLayout class is used to arrange the components in a 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 along with given horizontal and vertical gaps.

Page 13
Object Oriented Programming Using Java 22MCA22

import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{JFrame frameObj;
GridLayoutExample() {
frameObj = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
frameObj.add(btn1);
frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4);
frameObj.setLayout(new GridLayout(2,2));
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
public static void main(String argvs[])
{new GridLayoutExample();
}
}

FlowLayout
• The Java FlowLayout class is used to arrange the components in a line, one after another
(in
• a flow). It is the default layout of the applet or panel.
• Fields of FlowLayout class
o public static final int LEFT
o public static final int RIGHT
o public static final int CENTER
o public static final int LEADING
o 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.
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{JFrame frameObj;
FlowLayoutExample()
{
frameObj = new JFrame();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
frameObj.add(b1);
frameObj.add(b2);

Page 14
Object Oriented Programming Using Java 22MCA22

frameObj.add(b3);
frameObj.add(b4);
frameObj.setLayout(new FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
public static void main(String
argvs[]){new
FlowLayoutExample();
}
}

Applet Fundamentals
• Applet is a special type of program that is embedded in the webpage to generate the
dynamic content.
• It runs inside the browser and works at client side.
• Java Plug-in software is responsible to manage the life cycle of an applet.
• Lifecycle of Java Applet
o init
o start
o paint
o stop
o destroy
• init(): is used to initialized the Applet. It is invoked only once.
• start(): is invoked after the init() method or browser is maximized. It is used to start the
Applet.
• paint( ) is also called when the applet begins execution. Whatever the cause, whenever
the applet must redraw its output, paint( ) is called. The paint( ) method has one
parameter of type Graphics.
• stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is
minimized.
• destroy( ) : The destroy( ) method is called when the environment determines that your
applet needs to be removed completely from memory.
• The stop( ) method is always called before destroy( ).

Page 15
Object Oriented Programming Using Java 22MCA22

Example of Applet by html file: //First.java


import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{public void paint(Graphics g) {
g.drawString(“Hello World", 20, 20);
}
}
//RunHelloWorld.html
<html>
<body>
<applet code="First" width=“200" height=“60"></applet>
</body>
</html>

How to run:
javac First.java
appletviewer RunHelloWorld.html

• This applet begins with two import statements.


• The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with
the user (either directly or indirectly) through the AWT, not through the console-based
I/O classes.
• The AWT contains support for a window-based, graphical user interface.
• The second import statement imports the applet package, which contains the class
Applet.
• Every applet that you create must be a subclass of Applet.
• paint( ) is called each time that the applet must redisplay its output.
• Inside paint( ) is a call to drawString( ), which is a member of the Graphics class.
• The call to drawString( ) in the applet causes the message “A Simple Applet” to be
displayed beginning at location 20,20.
• Here are the key points that you should remember now:
• Applets do not need a main( ) method.
• Applets must be run under an applet viewer or a

Java-compatible browser.
• User I/O is not accomplished with Java’s stream I/O classes. Instead, applets use the
interface provided by the AWT or Swing.

Page 16
Object Oriented Programming Using Java 22MCA22

Difference between Application and Applets

Passing Parameter in Applet


• We can get any information from the HTML file as a parameter.
• For this purpose, Applet class provides a method named getParameter().
Syntax:
public String getParameter(String parameterName)
Example UseParam.java

import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet {
public void paint(Graphics g) {
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}

Myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" hei ght="300">
<param name="msg" value="Welcome to applet ">
</applet>
</body>
</html>

Page 17

You might also like