Unit 3 - Gui Programming
Unit 3 - Gui Programming
❖ GUI BASICS
GUI, which stands for Graphical User Interface, is a user-friendly visual experience
builder for Java applications. It comprises graphical units like buttons, labels, windows,
etc. via which users can connect with an application. Abstract Window Toolkit can be
used to create GUI based applications.Now a days Java Swings are used to create GUI
based applications than AWT as Swings provide richer implementation than AWT.
● 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.
A GUI comprises an array of user interface elements. All these elements are displayed
when a user is interacting with an application and they are as follows:
● Input commands such as buttons, check boxes, dropdown lists and text fields.
● Informational components like banners, icons, labels or notification dialogs.
● Navigational units like menus, sidebars and breadcrumbs.
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.
Types of containers:
1. Window
2. Panel
3. Frame
4. Dialog
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. We need to create an instance of
Window class to create this container.
❖ Panel
The Panel is the container that doesn't contain title bar, border or menu bar. It is generic
container for holding the components. It can have other components like button, text field
etc. An instance of Panel class creates a container, in which we can add components.
import java.awt.*;
import java.applet.*;
public class PanelEx extends Applet{
public void paint(Graphics g)
{
Frame f= new Frame("Panel Example");
Panel panel=new Panel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
Button b1=new Button("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
Button b2=new Button("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);
}
}
PanelEx.html
<html>
<body>
</applet>
</body>
</html>
❖ Frame
The Frame is the container that contain title bar and border and can have menu
bars. It can have other components like button, text field, scrollbar etc. Frame is
most widely used container while developing an AWT application.
Example to create AWT Frame by creating Frame class object using applet
import java.awt.*;
import java.applet.*;
// creating a Frame
// creating a Label
// creating a Button
// creating a TextField
f.add(b);
f.add(l);
f.add(t);
f.setSize(400,300);
f.setTitle("Employee info");
// no layout
f.setLayout(null);
f.setVisible(true);
FrEx.html
<html>
<body>
<applet code="FrEx.class" width="600" height="600">
</applet>
</body>
</html>
❖ Java LayoutManagers
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:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. 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 a frame or window. The BorderLayout provides five constants for each region:
● BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
Example:
import java.awt.*;
import java.applet.*;
public class Border extends Applet
{
Frame f;
public void paint(Graphics g)
{
f = new Frame();
// creating buttons
Button b1 = new Button("NORTH"); // the button will be labeled as NORTH
Button b2 = new Button("SOUTH"); // the button will be labeled as SOUTH
Button b3 = new Button("EAST"); // the button will be labeled as EAST
Button b4 = new Button("WEST"); // the button will be labeled as WEST
Button b5 = new Button("CENTER");// the button will be labeled as CENTER
f.setLayout(new BorderLayout(20, 15));
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
}
Border.html
<html>
<body>
<applet code="Border.class" width="600" height="600">
</applet>
</body>
</html>
❖ 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.
1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit
horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a
default 5 unit horizontal and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
Example:
import java.awt.*;
import java.applet.*;
public class Flow extends Applet
{
Frame f;
public void paint(Graphics g)
{
f = new Frame();
// creating buttons
Button b1 = new Button("one");
Button b2 = new Button("two");
Button b3 = new Button("three");
Button b4 = new Button("four");
Button b5 = new Button("five");
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setSize(300, 300);
f.setVisible(true);
}
}
Flow.html
<html>
<body>
<applet code="Flow.class" width="600" height="600">
</applet>
</body>
</html>
OUTPUT
❖ GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
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:
import java.awt.*;
import java.applet.*;
public class Grid extends Applet
{
Frame f;
public void paint(Graphics g)
{
f = new Frame();
// creating buttons
Button b1 = new Button("one");
Button b2 = new Button("two");
Button b3 = new Button("three");
Button b4 = new Button("four");
Button b5 = new Button("five");
// CREATING GRID LAYOUT OF 3 ROWS AND 3 COLUMNS
f.setLayout(new GridLayout(2,3));
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setSize(300, 300);
f.setVisible(true);
}
}
Flow.html
<html>
<body>
<applet code="Grid.class" width="600" height="600">
</applet>
</body>
</html>
❖ GUI COMPONENTS
● BUTTONS
Example:
import java.awt.*;
import java.applet.*;
public class ExBtn extends Applet
{
Frame f;
public void paint(Graphics g)
{
f = new Frame();
// creating buttons
Button b1 = new Button("one");
Button b2 = new Button("two");
f.setLayout(null);
f.add(b1);
f.add(b2);
f.setSize(300, 300);
f.setVisible(true);
}
}
Flow.html
<html>
<body>
<applet code="ExBtn.class" width="600" height="600">
</applet>
</body>
</html>
● CHECKBOXES
The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or
off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to
"on".
Example:
import java.awt.*;
import java.applet.*;
public class ChkEx extends Applet
{
public void paint(Graphics g)
{
Frame f = new Frame(“Checkbox Example”);
// creating checkboxes
Checkbox c1=new Checkbox("c++");
c1.setBounds(100,150,50,50);
f.add(c1);
f.add(c2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
ChkEx.html
<html>
<body>
<applet code="ChkEx.class" width="600" height="600">
</applet>
</body>
</html>
● RADIO BUTTONS
CheckBox Group enables you to create Radio Buttons in AWT and there is no special
method for creating radio button in AWT.
Example:
import java.awt.*;
checkBox1.setBounds(100,100, 50,50);
Checkbox checkBox2 = new Checkbox("Java", cbg, true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
ExRadio.html
<html>
<body>
<applet code="ExRadio.class" width="600" height="600">
</applet>
</body>
</html>
● LABEL
The object of the Label class is a component for placing text in a container. It is used to
display a single line of read only text. The text can be changed by a programmer but a
user cannot edit it directly.
It is called a passive control as it does not create any event when it is accessed. To
create a label, we need to create the object of Label class.
● TextField
The object of a TextField class is a text component that allows a user to enter a single
line text and edit it. It inherits TextComponent class, which further inherits Component
class.
● TextArea
The object of a TextArea class is a multiline region that displays text. It allows the
editing of multiple line text. It inherits TextComponent class.
The text area allows us to type as much text as we want. When the text in the text area
becomes larger than the viewable area, the scroll bar appears automatically which helps
us to scroll the text up and down, or right and left.
import java.awt.*;
l1.setBounds(20,80,80,40);
t1.setBounds(20,120,80,40);
l2.setBounds(20,160,160,60);
ta.setBounds(20,220,200,200);
b.setBounds(90,440,80,60);
f.add(l1);
f.add(l2);
f.add(t1);
f.add(ta);
f.add(b);
f.setBackground(Color.pink);
f.setSize(600,600);
f.setLayout(null);
f.setVisible(true);
Ex.html
<html>
<body>
<applet code="Ex.class" width="800" height="800">
</applet>
</body>
</html>
● List
The object of List class represents a list of text items. With the help of the List class,
user can choose either one item or multiple items. It inherits the Component class.
In the following example, we are creating a List component with 5 rows and adding it
into the Frame.
Example:
import java.awt.*;
import java.applet.*;
l1.add("cake");
l1.add("veggis");
l1.add("soap");
l1.add("pen");
l1.add("pencil");
f.add(l1);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
ExList.html
<html>
<body>
<applet code="ExList.class" width="800" height="800">
</applet>
</body>
</html>
● Scrollbar
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a
GUI component allows us to see invisible number of rows and columns.
It can be added to top-level container like Frame or a component like Panel. The
Scrollbar class extends the Component class.
● Value: specify the starting position of the knob of Scrollbar on its track.