0% found this document useful (0 votes)
12 views39 pages

Advance Desktop Applications Lecture 9

Uploaded by

mirzadsami2116
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)
12 views39 pages

Advance Desktop Applications Lecture 9

Uploaded by

mirzadsami2116
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/ 39

Desktop Application Development

MOHAMMAD SALIM HAMDARD

KABUL UNIVERSITY Monday, July 29, 2024 1


Lecture 10
ABSTRACT WINDOW TOOLKIT (AWT)

KABUL UNIVERSITY Monday, July 29, 2024 2


LEARNING OUTCOMES
Graphical User Interface
AWT
Java GUI
AWT Components
Frame
Label
Button
Text Field
Password Field
Text Area
WHAT IS GRAPHICAL USER INTERFACE

The previous command line systems were awkward to use


It was not user friendly
Difficult for end users to interact with
CONTD.
With the development of Home PCs the need for GUI has increased.
ABSTRACT WINDOW TOOLKIT(AWT)

Abstract Window Toolkit(AWT) is a built-in java package which contains


classes and methods for designing user-friendly Graphical User Interface
(UGI). AWT controls are available in ‘java.awt’ package. These controls are
heavyweight and platform dependent as they use the system recourses and
their display is operating system dependent.

KABUL UNIVERSITY Monday, July 29, 2024 6


JAVA AWT CLASSES HIERARCHY
CONTD.

Components Class: In java all the elements like the button, textfiels,
scrollbar, etc are called components. In Java AWT, there are classes for each
component as shown in the above diagram but we need to add them to a
container in order to place them in a particular position on the screen.
Container Class: The Container class is sub-class of Component class. The
classes that extend Container class are known as containers such as Frame,
Dialog and Panel. It also contains components like Button, TextField, Label
etc.

KABUL UNIVERSITY Monday, July 29, 2024 8


TYPES OF CONTAINER

Window: The Window is a subclass of Container class. The Window


container have no border and menu-bars. We must use Frame ,Dialog or
another window for creating a window.
Panel: The Panel is a subclass of container Class. It does not contain
border ,title bar and menu bars. It can hold other components such as
Button, Text Field, Label etc. We need to create an instance of Panel class
and then add component.
Frame: The Frame is a subclass of Window class. It may have border, title
bar and menu bar. It can include other components like Button,
TextField ,Label etc.

KABUL UNIVERSITY Monday, July 29, 2024 9


The Dialog: The Dialog is pop-up windows. It has a border and title. It is
used to take some input from user. The Dialog class does not have minimize
and maximize buttons.

KABUL UNIVERSITY Monday, July 29, 2024 10


METHODS OF CONTAINER CLASS
Method Description
public void add(Component c) Add a component to the container

public void setSize(int width,int height) Set the width and height of component
ex. Frame

Public void setLocation(int x,int y) Set location of the container or Frame on


the computer screen

public void setLayout(LayoutManager m) Set layout manager of the component

Public void setVisible(Boolean status) Uset to set visibility of component default


value is false
KABUL UNIVERSITY Monday, July 29, 2024 11
AWT CONTROLS
The commonly used AWT controls are listed bellow:
Label
TextField
Button
CheckBox
CheckBox for creating radio button
TextArea
Choice
List(Scrolling List)
LABEL

Label is a control which display text on the window. This text can not be
edited by the user.
The commonly used constructor for label class are:
•Label()
•Label(String text)
•Label(String text, int align)
THE COMMONLY USED METHOD OF LABEL
CLASS
Method Description
getText() Gets the text of this Label

setText() Sets the text of Label

setFont() Sets the font

setBound(int x, int y, int width, int To sets the position and size of
height) component
setForeground(Color.red) To set foreground color

KABUL UNIVERSITY Monday, July 29, 2024 14


CODE

package AWTpac; l1.setForeground(Color.PINK);


import java.awt.*; l1.setFont(new Font("Trebuchet
import java.applet.*; MS",Font.BOLD| Font.ITALIC, 25));

public class LabelEx2 extends Applet { l2.setForeground(new Color(0, 250,


public void init() 154));
{ l2.setFont(new Font("Trebuchet
Label l1,l2; MS",Font.BOLD| Font.ITALIC, 25));
l1=new Label(); add(l1);
l1.setText("Welcome"); add(l2);
l2=new Label("to the world of java"); resize(320,300); // to set size of
applet
}
}

KABUL UNIVERSITY Monday, July 29, 2024 15


KABUL UNIVERSITY Monday, July 29, 2024 16
TEXTFIELD

TextField AWT control inherits TextComponent class. It is used to accept


input from the user in a single line. The various constructor to create
TextField control are as follows:
•TextField()
•TextField(int)
•TextField(String)
•TextField(String, int)

KABUL UNIVERSITY Monday, July 29, 2024 17


SONE USEFUL TEXTFIELD CLASS METHOD

Method Description
String getText() Return the text inputted in the text field
Ex. String txt=txt1.getText();

setText() Sets the text of text field ex.


Txt1.setText(“Ahmad”);

setFont() Sets the font

setBound(int x, int y, int width, int To sets the position and size of text field
height)
setEchoChar(Char) To change the text field to password field
Ex. txt1.setEchoChar(‘*’)

KABUL UNIVERSITY Monday, July 29, 2024 18


CODE
package AWTpack; lblName.setBounds(60, 70, 100, 30);
import java.awt.*; txtName.setBounds(170, 70, 140, 30);
public class TextFieldEx2{ lblPass.setBounds(60, 110, 100, 30);
Frame f; txtPass.setBounds(170, 110, 140, 30);
Label lblName,lblPass;
TextField txtName,txtPass; f.add(lblName);
TextFieldEx2() { f.add(txtName);
f=new Frame(); f.add(lblPass);
lblName=new Label("Enter Name"); f.add(txtPass);
blPass=new Label("Enter Password"); f.setSize(400,400);
f.setLocation(600,300);
txtName=new TextField(10); f.setLayout(null);
txtPass=new TextField(10); f.setVisible(true);
txtPass.setEchoChar('*'); }
public static void main(String args[])
{
new TextFieldEx2();
}
KABUL UNIVERSITY Monday, July 29, 2024 19
}
KABUL UNIVERSITY Monday, July 29, 2024 20
BUTTON

Buttons are the AWT controls which trigger some action when they are
clicked. Button are mainly used for handling events for example submitting
information to the database or validating information of a form.
The constructor used with button class are as follows:
•Button()
•Button(String)

KABUL UNIVERSITY Monday, July 29, 2024 21


BUTTON

import java.awt.Button;
Button button=new Button(“Click Me”);
button.setBounds(x,y,w,h) Y

frame.add(button);
X
H

W
CODE
package AWTpack; f.add(b);
import java.awt.*; f.setSize(400,400);
f.setLocation(600, 300);
public class ButtonEx3 { f.setLayout(null);
Frame f; f.setVisible(true);
Button b; }
ButtonEx3(){ public static void main(String args[])
f = new Frame("AWT Frame"); {
b = new Button("Click Here!"); ButtonEx3 obj=new ButtonEx3();
}
b.setBounds(50,100,80,30); }
b.setSize(130, 50);
b.setBackground(Color.black);
b.setForeground(Color.white);
b.setFont(new Font("Arial",Font.BOLD|
Font.ITALIC, 15));

KABUL UNIVERSITY Monday, July 29, 2024 23


KABUL UNIVERSITY Monday, July 29, 2024 24
CHECKBOX

Check boxes are used for making multiple selections to indicate optional
features in a form like hobbies. They have two states either on or off,
checked or unchecked, true or false.
The constructor used with Checkbox class are as follow:
•Checkbox()
•Checkbox(String)
•Checkbox(String ,boolean)
Ex. Checkbox chk=new Checkbox(“Label Text for Checkbox”, true);
add(chk);

KABUL UNIVERSITY Monday, July 29, 2024 25


SONE USEFUL CHECKBOX CLASS METHODS

Method Description
setState(boolean) Sets the state of a check box
Ex. Chk1.setState(“true”);

boolean getState() Return or read checkbox state ex.


Boolean b1=chk1.getState();

setLabel(String) Sets label of checkbox ex.


chk1.setLabel(“Reading”);

String getLabel() Return or read label of checkbox ex.


String lbl=chk1.getLabel();

KABUL UNIVERSITY Monday, July 29, 2024 26


CODE
package AWTpack; check1.setBounds(100, 100, 50, 50);
import java.awt.*; check2.setBounds(100, 150, 50,
50);
public class CheckBoxEx4 { f.add(lbl);
Frame f; f.add(check1);
Label lbl; f.add(check2);
Checkbox check1,check2; f.setSize(350,350);
CheckBoxEx4() f.setLayout(null);
{ f.setVisible(true);
f=new Frame();
lbl=new Label("Select Color!"); }
check1=new Checkbox("Red",true); public static void main(String args[])
check2=new Checkbox("Blue"); {
lbl.setFont(new Font("Tahoma", CheckBoxEx4 obj= new
Font.BOLD, 14)); CheckBoxEx4();
lbl.setBounds(100, 50, 150, 50); }

}
KABUL UNIVERSITY Monday, July 29, 2024 27
KABUL UNIVERSITY Monday, July 29, 2024 28
CHECKBOX GROUP

Checkbox class is also used to create radio button in a form. Radio button is
used for making mutually exclusive selections. It means only one option can
be selected from a set of options.
So, first we need to create object of ChexkboxGroup class and then pass it to
Checkbox as bellow:
CheckboxGroup chkg=new CheckboxGroup();
Checkbox chk=new Checkbox(String,chkg,boolean);
add(chk);

KABUL UNIVERSITY Monday, July 29, 2024 29


CODE package AWTpack;
import java.awt.*;
lbl=new Label("Select Course!");
lbl.setFont(new Font("Tahoma",
public class ChkGroupEx5 { Font.BOLD, 14));
Frame f; lbl.setBounds(30, 50, 150, 50);
CheckboxGroup chkg; chk1.setBounds(30, 100, 50, 50);
Checkbox chk1,chk2,chk3; chk2.setBounds(30, 150, 150, 50);
Label lbl; chk3.setBounds(30, 200, 200, 50);
ChkGroupEx5()
{ f.add(chk1);
f=new Frame(); f.add(chk2);
chkg=new CheckboxGroup(); f.add(chk3);
chk1=new f.add(lbl);
Checkbox("12th",chkg,false); f.setSize(350,350);
chk2=new f.setLayout(null);
Checkbox("Graduate",chkg,false); f.setVisible(true);
chk3=new Checkbox("Post }
Graduate",chkg,true); public static void main(String args[])
{
new ChkGroupEx5();

}}
KABUL UNIVERSITY Monday, July 29, 2024 30
KABUL UNIVERSITY Monday, July 29, 2024 31
CHOICE

The AWT control Choice is used to display a drop-down menu of items from
which only one can be selected at a time. It has only one constructor given
bellow:
Choice chList=new Choice();
chList.add(“Graduate”);
chList.add(“Post Graduate”);
chList.add(“PHD”);
Add(chList);

KABUL UNIVERSITY Monday, July 29, 2024 32


SOME USEFUL CHOICE CLASS METHODS

Method Description
add(String item) Add items to the Choice list
Ex. chList.add(“Graduate”);

String getItem(int index) Returns item at given index ex.


String res=chList.getItem(2);

String getSelectedItem() Returns the selected item ex.


String res=chList.getSelectedItem()

int getSelectedIndex() Returns the index of selected item


Ex. int ind=chList. getSelectedIndex()
remove(int index) Remove item at specific index
KABUL UNIVERSITY Monday, July 29, 2024 33
CODE
package AWTpack; lbl=new Label("Select Course!");
import java.awt.*; lbl.setFont(new Font("Tahoma",
public class ChoiceEx6 { Font.BOLD, 14));
Frame f; lbl.setBounds(30, 50, 150, 50);
Label lbl;
Choice chList; f.add(lbl);
ChoiceEx6() f.add(chList);
{ f.setSize(350, 350);
f=new Frame(); f.setLayout(null);
chList=new Choice(); f.setVisible(true);
chList.setBounds(30, 100, 150, 50); }
chList.add("Graduate"); public static void main(String args[])
chList.add("Post Graduate"); {
chList.add("PHD"); ChoiceEx6 obj=new ChoiceEx6();
}

KABUL UNIVERSITY Monday, July 29, 2024 34


CODE

KABUL UNIVERSITY Monday, July 29, 2024 35


LIST

The AWT List control is similar to Choice control with two difference. In list
control user can select more than one items and selected items is not
displayed on the top of the list.
The following constructors are available in List class:
•List()
•List(int)
•List(int,boolean) // here Boolean specify that multiple selection is allowed or
not it can be true or false

KABUL UNIVERSITY Monday, July 29, 2024 36


CODE
package AWTpack; lst=new List();
import java.awt.*; lst.setBounds(30, 100, 150, 50);
public class ListEx7 { lst.add("Red");
Frame f; lst.add("Blue");
List lst; lst.add("Pink");
Label lbl; lst.add("Black");
ListEx7()
{ f.add(lbl);
f=new Frame(); f.add(lst);
lbl=new Label("Select Color!"); f.setSize(350, 350);
lbl.setFont(new Font("Tahoma", f.setLayout(null);
Font.BOLD, 14)); f.setVisible(true);
lbl.setBounds(30, 50, 150, 50); }
public static void main(String args[])
{
ListEx7 obj=new ListEx7();
}
KABUL UNIVERSITY Monday, July 29, 2024 37
KABUL UNIVERSITY Monday, July 29, 2024 38
THE END

KABUL UNIVERSITY Monday, July 29, 2024 39

You might also like