Java- GUI- Mazenet solution

Preview:

Citation preview

GUI in Java

BySharmilee9894303344Java TrainerMazenet Solution

Objectives • GUI Introduction• Applet Programming• Applet Lifecycle• AWT • SWING • Layouts• Event Handling

Graphical User Interface

Applet Programming

Introduction• 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.

Advantage• It works at client side so less

response time.

• Secured

• It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.

Applet viewer

Hierarchy of Applet

Lifecycle methods• java.applet.Applet

public void init() public void start() public void stop() public void destroy()

• java.awt.Component public void paint()

Java AWT

Abstract Window Toolkit (AWT)

• It is platform-dependent windowing, graphics, and user-interface widget toolkit preceding Swing.

• AWT is part of the Java Foundation Classes (JFC)

• the standard API for providing a graphical user interface (GUI) for a Java program.

AWT Hierarchy

Creating an awt• To create awt example, we have two

methods– By extending frame class(Inheritance)– By creating the object of frame class

(association)

Container• It is a component in AWT

• It contain another components like buttons, textfields, labels etc.

• The classes that extends Container class are known as container such as Frame, Dialog and Panel.

Panel• The Panel is the container that

doesn't contain title bar and menu bars.

• It can have other components like button, textfield etc.

Frame• Frame is the container that contain

title bar and can have menu bars.

• It can have other components like button, textfield etc.

AWT to Swing• AWT: Abstract Windowing Toolkit

• import java.awt.*

• Swing:• import javax.swing.*

Swing

Introduction to Swing• It is used to create window-based

applications.

• It is built on the top of AWT API and entirely written in java.

• Swing provides platform-independent and lightweight components.

Swing classes

Using a GUI Component1. Create it

• Instantiate object: b = new JButton(“press me”);2. Configure it

• Properties: b.text = “press me”; [avoided in java]

• Methods: b.setText(“press me”);3. Add it

• panel.add(b);4. Listen to it

• Events: Listeners

JButton

CodeJFrame f = new JFrame(“title”);JPanel p = new JPanel( );JButton b = new JButton(“press me”);

p.add(b); // add button to panel

f.setContentPane(p); // add panel to frame

f.show();press me

Application Codeimport javax.swing.*;

class hello {public static void main(String[] args){

JFrame f = new JFrame(“title”);JPanel p = new JPanel();JButton b = new JButton(“press me”);

p.add(b); // add button to panel

f.setContentPane(p); // add panel to frame

f.show();}

}

press me

Difference between AWT & SwingSno AWT SWING1 Platform dependent Platform Independent2 Heavyweight components Light weight components3 Doesn’t support pluggable

look and feelSupports pluggable look and feel

4 Less components than swing More powerful components5 Doesn’t follow MVC Follows MVC

Layout

Layout Manager

Left to right,Top to bottom

c

n

s

ew

FlowLayout GridLayout

BorderLayout

none, programmer sets x,y,w,h

null

One at a time

CardLayout GridBagLayout

JButton

Event Handling

• Changing the state of an object is known as an event.

• For example, – click on button, – dragging mouse etc.

• The java.awt.event package provides many event classes and Listener interfaces for event handling.

Steps to perform Event Handling

1. Implement the Listener interface and overrides its methods

2. Register the component with the Listener

Thank You!!!