Chapter 03

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Faculty of Computer Science

Subject: Advance JAVA


Lecturer: Ajmal Rahmati
Java Programming Chapter 03

CHAPTER#-03
Java Programming Chapter 03

Advance JAVA (GUI)


Java Programming Chapter 03

What is AWT?
• AWT is stand for abstract window toolkit.
• Java AWT is an API to develop Graphical User Interface (GUI) or
windows-based application in java.
• AWT is used to develop desktop application.
• Java AWT Components is platform-dependent.
• Components are displayed according to the view of operating system.
• AWT is heavy weight.
Java Programming Chapter 03

What is AWT?
• The java.awt package provide classes and interfaces for AWT API.
• The java.awt package provides classes for AWT API such as:
1. TextField
2. Label
3. TextArea
4. RadioButton
5. CheckBox
6. Choice
7. List etc.
Java Programming Chapter 03

AWT Hierarchy Classes


Java Programming Chapter 03

Graphical User Interface


• All the elements like the button, text fields, scroll bars, etc. are called
components. In Java AWT, there are classes for each component as
shown in above diagram.
• In order to place every component in a particular position on a screen,
we need to add them to a container.
Java Programming Chapter 03

AWT Components
To prepare GUI application, AWT is able to provide support for the
following components.
label
Button
CheckBox
TextField
TextArea … etc
To provide the above GUI components, java.awt pachage has provided
predefined classes.
Java Programming Chapter 03

Graphical User Interface


Java Programming Chapter 03

Graphical User Interface


Java Programming Chapter 03

AWT Containers
• A container provides a space where a component can be located.
• A container in AWT is a component itself and it adds the capability to
add components [Button, Labels, TextField] to itself.
• Sub classes of a container are called as container. For example Panel,
Frame and window.
Java Programming Chapter 03

Sub Classes of Container class in Java


Java Programming Chapter 03

Java.awt.frame Class
• Frame is a container it able to manage some other GUI
Components like button, text field, scrollbar etc …
• The Frame is the container that contain title bar and
border and can have menu bars.
• To represent Frame in GUI application, JAVA has
provided a separate predefined class in the form
java.awt.frame.
• Frame is most widely used container while developing
an AWT application.
Java Programming Chapter 03

How to Create Frame in AWT?


There are two ways to create frame in AWT:
1. By creating the object of Frame class (association)
2. By extending frame class (inheritance)
Java Programming Chapter 03

Default Setting of Frame


1. Created Frame is invisible
- setVisible(true);
2. Can`t close
- Talk latter
3. Size of Frame is 0 , 0
- setSize(300,400);
4. No Title
- setTitle(“Title is here”);
5. Background Color is white
- setBackground(Color.color-name)
Java Programming Chapter 03

How to Close AWT Frame?


this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
Java Programming Chapter 03

Java.awt.color class
• The color class is a part of Java Abstract Window Toolkit (AWT)
pachage.
• The color class creates color by using the given RGBA values
• Where RGBA stands for Red, Green, Blue,Alpha
• The value for individual component RGBA ranges from 0 255 or 0.0
to 1.0.
• The value of alpha determines the opacity of the color, where 0 or 0.0
stands fully transparent and 255 or 1.0 stands opaque.
Java Programming Chapter 03

Java.awt.Font class
• In java, Font is a class that belongs to the java.awt.Font package.
• It represents the font that are used to render the text.
• Class declaration
Public class Font implements Serializable{
---
---
}
Java Programming Chapter 03

Java AWT Button


A button is basically a control component with a label that generates an
event when pushed.

AWT Button Class Declaration


public class Button extends Component implements Accessible
Java Programming Chapter 03

Button Class Constructors


Following table shows the types of Button class constructors
Public Button():
it constructs a new button with an empty string i.e it has no label.
Example:
Button b = new Button();
Public Button (String text):
it constructs a new button with given string as its label.
Example:
Button b = new Button (“Login”)
Java Programming Chapter 03

Button Class Methods


Public void setLabel(String label)
it sets the label of button with the spechified string.
Example:
b . setLabel (“Register”);
public String getLabel()
gets the label of the button.
Example:
b . getLabel()
Java Programming Chapter 03

Button Class Methods


Public void setBounds(int x, int y, int width, int height)
it set the location of GUI components like label, textfield etc
Public void add ( Component c)
insert a component on Frame
Java Programming Chapter 03

Java AWT 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.
AWT Label Class Declaration
public class Label extends Component implements Accessible
Java Programming Chapter 03

Label class Constructors

Label()
Label(String text)

Label Class Methods


void setText(String text)
String getText()
Java Programming Chapter 03

Suggested Reading for Lecture:


Introduction to Java Programming By: Y. Daniel Liang 8th edition
java how to program By: Paul Deitel and Harvey Deitel 9th edition
Review Questions:
‫‪Thank You‬‬
‫‪For Your Patience‬‬

‫‪Thought of Day‬‬
‫هرگز تسلیم نشوید‪ ،‬امروز سخت است و فردا سخت‬
‫تر‪ ،‬اما پس فردا روز روشنی برای تان خواهد بود‬

You might also like