Unit4 Java
Unit4 Java
Event handling in java, Event types, Mouse and key events, GUI Basics, Panels,
Frames, Layout Managers: Flow Layout, Border Layout, Grid Layout, GUI
components like Buttons, Check Boxes, Radio Buttons, Labels, Text Fields, Text
Areas, Combo Boxes, Lists, Scroll Bars, Sliders, Windows, Menus, Dialog Box,
Applet and its life cycle, Introduction to swing, Exceptional handling mechanism.
GUI Basics
Java AWT contains large number of classes and methods to create and manage
graphical user interface ( GUI )
Containers in java
A Container is a subclass of Component that can contain other components.
There are four types of containers available in AWT:
Window,
Frame,
Dialog
Panel.
Narasimha Murthy G K Page 1
Course II_BCA NEP Syllabus Krupanidhi College
Panel: Panel does not contain title bar, menu bar or border. It is a generic container for
holding components.
Frame: A frame has title, border and menu bars. It can contain several components like
buttons, text fields, scrollbars etc. This is most widely used container while developing
an application in AWT.
import java.awt.*;
public class AwtProgram1
{
public AwtProgram1()
{
Frame f = new Frame();
Button btn=new Button("Hello World");
btn.setBounds(80, 80, 100, 50);
f.add(btn); //adding a new Button.
f.setSize(300, 250); //setting size.
f.setTitle("JavaTPoint"); //setting title.
f.setLayout(null); //set default layout for frame.
f.setVisible(true); //set frame visibility true.
}
import java.awt.*;
public class SimpleExample extends Frame{
SimpleExample()
{
Button b=new Button("Button!!");
AWT Button
In Java, AWT contains a Button Class. It is used for creating a labeled button which can
perform an action.
Example program
import java.awt.*;
public class ButtonDemo1
{
public static void main(String[] args)
{
Frame f1=new Frame("Button Demo");
Button b1=new Button("Press Here");
b1.setBounds(80,200,80,50);
f1.add(b1);
f1.setSize(500,500);
f1.setLayout(null);
f1.setVisible(true);
}
}
AWT Label
In Java, AWT contains a Label Class. It is used for placing text in a container. Only Single
line text is allowed and the text cannot be changed directly.
Example program
import java.awt.*;
class LabelDemo1
Label lab1,lab2;
lab1.setBounds(50,50,200,30);
lab2.setBounds(50,100,200,30);
f.add(lab1);
f.add(lab2);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
AWT TextField
In Java, AWT contains a TextField Class. It is used for displaying single line text.
import java.awt.*;
class TextFieldDemo1
{
public static void main(String args[])
{
Frame f= new Frame("TextField Demo");
TextField t1,t2;
t1=new TextField("Welcome ");
t1.setBounds(60,100, 230,40);
t2=new TextField("This tutorial is of Java");
t2.setBounds(60,150, 230,40);
f.add(text1);
f.add(text2);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
AWT TextArea
In Java, AWT contains aTextArea Class. It is used for displaying multiple-line text.
Example Program
import java.awt.*;
public class TextAreaDemo1
{
TextAreaDemo1()
{
Frame f= new Frame();
TextArea area=new TextArea("Welcome ");
area.setBounds(30,40, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaDemo1();
}
}
AWT Checkbox
In Java, AWT contains a Checkbox Class. It is used when we want to select only one
option i.e true or false. When the checkbox is checked then its state is "on" (true) else it
is "off"(false).
Example program
import java.awt.*;
CheckboxDemo1(){
ckbox1.setBounds(100,100, 60,60);
ckbox2.setBounds(100,150, 60,60);
f.add(ckbox1);
f.add(ckbox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new CheckboxDemo1();
AWT List
List class is used to create a list with multiple values, allowing a user to select any of
the values. When a value is selected from List, an ItemEvent is generated, which is
handled by implementing ItemListener interface.
Constructors of List
Example Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
ListEx1()
{
jf= new Frame("List");
list= new List(7);
label1 = new Label("Select your favorite sports from the list :");
list.add("Badminton");
list.add("Hockey");
list.add("Tennis");
list.add("Football");
list.add("Cricket");
list.add("Formula One");
list.add("Rugby");
jf.add(label1);
jf.add(list);
jf.setLayout(new FlowLayout());
jf.setSize(260,220);
jf.setVisible(true);
}
Output
Menu bar
A menu bar can be created using MenuBar class.
A menu bar may contain one or multiple menus, and these menus are created
using Menu class.
A menu may contain one of multiple menu items and these menu items are created
using MenuItem class.
Example Program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
MenuEx1()
{
frame = new Frame("MenuBar, Menu and MenuItems");
menu1.add(mItem2);
menu1.add(mItem3);
frame.setSize(330,250);
frame.setVisible(true);
}
Applet
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It
is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
HelloWorld.java
Now you have to create an HTML File that Includes the Applet.
Using a text editor, create a file named Hello.html in the same directory that
contains HelloWorld.class.
Example
GraphicsDemo.java
Syntax
1. public abstract boolean drawImage(Image img, int x, int y, ImageObserver
observer): is used draw the specified image.
Myappelt.html
Event Handling
Event handling has three main components,
Events : An event is a change of state of an object.
Events Source : Event source is an object that generates an event.(producer of an
event)
Listeners : A listener is an object that listens to the event. A listener gets notified
when an event occurs. Listener is responsible for generating response to an event.
Example Program
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
An event of type KeyEvent class is generated when a source such as, a key on the
keyboard is pressed in a textfield or in a textarea.
The class which processs the keyEvent should implement Key Listener Interface
The method of the keyListner Interface are
Layout Managers
In Java, Layout Managers is an object that determines the way that Components are
arranged in a container
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. javax.swing.BoxLayou
java.awt.FlowLayout
The flow layout manager arranges the components one after another from left-to-right
and top-to-bottom manner. The flow layout manager gives some space between
components.
There are 3 types of constructor in the Flow Layout. They are as following:
1. FlowLayout()
2. FlowLayout(int align)
3. FlowLayout(int align, int hgap, int vgap)
import java.awt.*;
import javax.swing.*;
frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
frame1.add(box8);
frame1.add(box9);
frame1.add(box10);
frame1.setLayout(new FlowLayout(FlowLayout.LEFT));
frame1.setSize(400,400);
frame1.setVisible(true);
}
public static void main(String[] args)
{
new FlowDemo1();
}
}
Output
BorderLayout
BorderLayout arranges the components in the five regions. Four sides are referred to
as north, south, east, and west. The middle part is called the center. Each region can
Constructors:
//Driver Code
public static void main(String[] args)
{
//Calling the constructor
new border();
}
}
OutPut
Grid Layout
Grid Layout is used, when we want to arrange the components in a rectangular grid.
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.
Example Program
import java.awt.*;
import javax.swing.*;
JFrame frame1;
GridDemo1(){
frame1=new JFrame();
frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
frame1.add(box8);
frame1.add(box9);
frame1.setLayout(new GridLayout(3,3));
frame1.setSize(500,500);
frame1.setVisible(true);
}
public static void main(String[] args) {
new GridDemo1();
}
}
output
JavaSwing
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.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.