際際滷

際際滷Share a Scribd company logo
GUI Programming with Java
Java Provides 2 Frameworks for building GUI-based applications.
Those are
 AWT-(Abstract Window Toolkit)
 Swing AWT-(Abstract Window Toolkit)
 AWT (Abstract Window Toolkit) is an API to develop GUI or window-
based applications in java.
 The java.awt package provides classes for AWT API such as TextField,
Label, TextArea, Checkbox, Choice, List etc.
 AWT components are platform-dependent i.e. components are displayed
according to the view of operating system.
GUI.pdf
 Component: Component is an abstract class that contains various classes
such as Button, Label,Checkbox,TextField, Menu and etc.
 Container: The Container is a component in AWT that can contain
another components like buttons, textfields, labels etc. The Container class
extends Frame and Panel.
 Window: The window is the container that have no borders and menu
bars. You must use frame for creating a window.
 Frame: The Frame is the container that contain title bar and can have
menu bars. It can have other components like button, textfield etc.
 Panel: The Panel is the container that doesn't contain title bar and menu
bars. It can have other components like button, textfield etc.
GUI.pdf
To create simple awt example, you need a frame.
There are two ways to create a frame in AWT.
 By extending Frame class (inheritance)
Ex: class Example extends Frame
{
..
}
 By creating the object of Frame class (association)
Ex: class Example
{
Frame obj=new Frame();
..
}
import java.awt.*;
public class AwtExample { public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("Simple Example");
}
}
Output: Javac AwtExample.java
Java AwtExample
To create simple awt example, you need a frame.
There are two ways to create a frame in AWT.
 By extending Frame class (inheritance)
Ex: class Example extends Frame { .. }
 By creating the object of Frame class (association)
Ex: class Example { Frame obj=new Frame(); .. }
Swing
Swing is a framework or API that is used to create GUI (or) window-based
applications.It is an advanced version of AWT (Abstract Window Toolkit)
API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight
components.
The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
GUI.pdf
GUI.pdf
GUI.pdf
To create simple swing example, you need a frame.
In swing, we use JFrame class to create a frame. There are two ways to
create a frame in swing.
 By extending JFrame class (inheritance)
Ex: class Example extends JFrame
{
..
}
 By creating the object of JFrame class (association)
Ex: class Example
{
JFrame obj=new JFrame();
..
}
A Simple Swing Example We can write the code of swing inside the main() or
constructor. In Main() Method:
SwingExample.java
import javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("Simple Swing Example");
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Applet
An applet is a Java program that runs in a Web browser.
(or)
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.
Any applet in Java is a class that extends the java.applet.Applet class.
Advantage of Applet
There are many advantages of applet.
They are as follows:  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.
GUI.pdf
As displayed in the diagram, Applet class extends Panel.
Panel class extends Container, which is the subclass of Component.
Where Object class is base class for all the classes in java.
JApplet class is extension of Applet class.
Lifecycle of Applet: There are 5 lifecycle methods of Applet, Those are public
void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized.
It is used to start the Applet.
public void paint(Graphics g): is invoked immediately after the start()
method, and this method helps to create Applets GUI such as a colored
background, drawing and writing.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.
GUI.pdf
Remember:
java.applet.Applet class provides 4 methods (init,start,stop & destroy) and
java.awt.Graphics class provides 1 method ( paint) to createApplet.
Simple example of Applet:
To execute an Applet, First Create an applet and compile it just like a simple java
program. First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString(Welcome to Applet",50,150);
}
}
Compile: D:> javac First.java
After successful compilation, we get First.class file.
After that create an html file and place the applet code in html file.
Displaying Graphics in Applet:
java.awt.Graphics class provides many methods for graphics programming.
The Commonly used methods of Graphics class:
 drawString(String str, int x, int y): is used to draw the specified string.
 drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and
height.
 fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and
specified width and height.
 drawOval(int x, int y, int width, int height): is used to draw oval with the specified width
and height.
 fillOval(int x, int y, int width, int height): is used to fill oval with the default color and
specified width and height.
 drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and
(x2, y2).
 setColor(Color c): is used to set the graphics current color to the specified color.
 setFont(Font font): is used to set the graphics current font to the specified font.
Example: GraphicsDemo.java import java.applet.Applet; import java.awt.*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
}
}
GUI.pdf
Components of Applet:
The components of AWT are the components of Applet,i.e we can use AWT components
(Button,TextField,Checkbox, TextArea,Choice & etc.) in applet.
As we perform event handling in AWT or Swing, we can perform it in applet also.
Let's see the simple example of components and event handling in applet that prints a
message by click on the button.
Example: AppletComponents.
java import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AppletComponents extends Applet implements ActionListener
{
Button b; TextField tf; public void init()
{
tf=new TextField();
tf.setBounds(80,40,150,20);
b=new Button("Click");
b.setBounds(80,120,80,30);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome"); } }
GUI.pdf
JApplet Class: As we prefer Swing to AWT.
Now we can use JApplet that can have all the controls of swing.
The JApplet class extends the Applet class. The components of swing are the
components of JApplet,i.e we can use swing components
(JButton,JTextField,JCheckBox, JTextArea,JList & etc.) in JApplet.
GUI.pdf
GUI.pdf

More Related Content

Similar to GUI.pdf (20)

PPT
introduction to JAVA awt programmin .ppt
bgvthm
PPT
Applet ppt for higher understanding education
BhanuPriya93439
PDF
Abstract Window Toolkit
RutvaThakkar1
PPTX
Lecture 2 Introduction to AWT (1).ppt.hello
mayurDharmik1
PPT
Basic of Applet
suraj pandey
PPT
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
PPT
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
PPT
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
PPTX
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
PPTX
AdvancedJava.pptx
DrPrabakaranPerumal
PPTX
Java Applet presentation............pptx
ZaildarHussainFaisal
PPTX
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
PPT
GUI Programming In Java
yht4ever
PPT
Unit4 AWT, Swings & Layouts power point presentation
SNIGDHAAPPANABHOTLA
PPT
13457272.ppt
aptechaligarh
PPTX
Applets
Nuha Noor
PPTX
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
PPT
Applets - lev' 2
Rakesh T
PPT
08graphics
Waheed Warraich
introduction to JAVA awt programmin .ppt
bgvthm
Applet ppt for higher understanding education
BhanuPriya93439
Abstract Window Toolkit
RutvaThakkar1
Lecture 2 Introduction to AWT (1).ppt.hello
mayurDharmik1
Basic of Applet
suraj pandey
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
1.Abstract windowing toolkit.ppt of AJP sub
YugandharaNalavade
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
AdvancedJava.pptx
DrPrabakaranPerumal
Java Applet presentation............pptx
ZaildarHussainFaisal
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
GUI Programming In Java
yht4ever
Unit4 AWT, Swings & Layouts power point presentation
SNIGDHAAPPANABHOTLA
13457272.ppt
aptechaligarh
Applets
Nuha Noor
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
Applets - lev' 2
Rakesh T
08graphics
Waheed Warraich

More from Poornima E.G. (10)

PPTX
INTRODUCTION AND BASICS OF Android NOTES.pptx
Poornima E.G.
PPT
Introduction to Pattern Recognition NOTES.ppt
Poornima E.G.
PPTX
Android Application managing activites.pptx
Poornima E.G.
PDF
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
Poornima E.G.
DOCX
VTU MCA 2022 JAVA Exceeption Handling.docx
Poornima E.G.
PPT
Process and Thread
Poornima E.G.
PPT
Operating System basics Introduction
Poornima E.G.
PPTX
csharp_dotnet_adnanreza.pptx
Poornima E.G.
PDF
Millimeter wave mobile communications for 5 g Cellular
Poornima E.G.
PDF
Semantic open io t service platform technology
Poornima E.G.
INTRODUCTION AND BASICS OF Android NOTES.pptx
Poornima E.G.
Introduction to Pattern Recognition NOTES.ppt
Poornima E.G.
Android Application managing activites.pptx
Poornima E.G.
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
Poornima E.G.
VTU MCA 2022 JAVA Exceeption Handling.docx
Poornima E.G.
Process and Thread
Poornima E.G.
Operating System basics Introduction
Poornima E.G.
csharp_dotnet_adnanreza.pptx
Poornima E.G.
Millimeter wave mobile communications for 5 g Cellular
Poornima E.G.
Semantic open io t service platform technology
Poornima E.G.
Ad

Recently uploaded (20)

PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
PPTX
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
PDF
I3PM Case study smart parking 2025 with uptoIP速 and ABP
MIPLM
PPTX
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
How to Configure Taxes in Company Currency in Odoo 18 Accounting
Celine George
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
I3PM Case study smart parking 2025 with uptoIP速 and ABP
MIPLM
ENGLISH 8 REVISED K-12 CURRICULUM QUARTER 1 WEEK 1
LeomarrYsraelArzadon
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
Ad

GUI.pdf

  • 1. GUI Programming with Java Java Provides 2 Frameworks for building GUI-based applications. Those are AWT-(Abstract Window Toolkit) Swing AWT-(Abstract Window Toolkit) AWT (Abstract Window Toolkit) is an API to develop GUI or window- based applications in java. The java.awt package provides classes for AWT API such as TextField, Label, TextArea, Checkbox, Choice, List etc. AWT components are platform-dependent i.e. components are displayed according to the view of operating system.
  • 3. Component: Component is an abstract class that contains various classes such as Button, Label,Checkbox,TextField, Menu and etc. Container: The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The Container class extends Frame and Panel. Window: The window is the container that have no borders and menu bars. You must use frame for creating a window. Frame: The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc. Panel: The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.
  • 5. To create simple awt example, you need a frame. There are two ways to create a frame in AWT. By extending Frame class (inheritance) Ex: class Example extends Frame { .. } By creating the object of Frame class (association) Ex: class Example { Frame obj=new Frame(); .. }
  • 6. import java.awt.*; public class AwtExample { public static void main(String[] args) { Frame f=new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Simple Example"); } } Output: Javac AwtExample.java Java AwtExample
  • 7. To create simple awt example, you need a frame. There are two ways to create a frame in AWT. By extending Frame class (inheritance) Ex: class Example extends Frame { .. } By creating the object of Frame class (association) Ex: class Example { Frame obj=new Frame(); .. }
  • 8. Swing Swing is a framework or API that is used to create GUI (or) window-based applications.It is an advanced version of AWT (Abstract Window Toolkit) API and entirely written in java. Unlike AWT, Java Swing provides platform-independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
  • 12. To create simple swing example, you need a frame. In swing, we use JFrame class to create a frame. There are two ways to create a frame in swing. By extending JFrame class (inheritance) Ex: class Example extends JFrame { .. } By creating the object of JFrame class (association) Ex: class Example { JFrame obj=new JFrame(); .. }
  • 13. A Simple Swing Example We can write the code of swing inside the main() or constructor. In Main() Method: SwingExample.java import javax.swing.*; public class SwingExample { public static void main(String[] args) { JFrame f=new JFrame("Simple Swing Example"); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } }
  • 14. Applet An applet is a Java program that runs in a Web browser. (or) 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. Any applet in Java is a class that extends the java.applet.Applet class. Advantage of Applet There are many advantages of applet. They are as follows: 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.
  • 16. As displayed in the diagram, Applet class extends Panel. Panel class extends Container, which is the subclass of Component. Where Object class is base class for all the classes in java. JApplet class is extension of Applet class. Lifecycle of Applet: There are 5 lifecycle methods of Applet, Those are public void init(): is used to initialized the Applet. It is invoked only once. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. public void paint(Graphics g): is invoked immediately after the start() method, and this method helps to create Applets GUI such as a colored background, drawing and writing. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. public void destroy(): is used to destroy the Applet. It is invoked only once.
  • 18. Remember: java.applet.Applet class provides 4 methods (init,start,stop & destroy) and java.awt.Graphics class provides 1 method ( paint) to createApplet.
  • 19. Simple example of Applet: To execute an Applet, First Create an applet and compile it just like a simple java program. First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet { public void paint(Graphics g) { g.drawString(Welcome to Applet",50,150); } } Compile: D:> javac First.java After successful compilation, we get First.class file. After that create an html file and place the applet code in html file.
  • 20. Displaying Graphics in Applet: java.awt.Graphics class provides many methods for graphics programming. The Commonly used methods of Graphics class: drawString(String str, int x, int y): is used to draw the specified string. drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height. fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height. drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height. fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height. drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). setColor(Color c): is used to set the graphics current color to the specified color. setFont(Font font): is used to set the graphics current font to the specified font.
  • 21. Example: GraphicsDemo.java import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); } }
  • 23. Components of Applet: The components of AWT are the components of Applet,i.e we can use AWT components (Button,TextField,Checkbox, TextArea,Choice & etc.) in applet. As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple example of components and event handling in applet that prints a message by click on the button. Example: AppletComponents. java import java.applet.*; import java.awt.*; import java.awt.event.*; public class AppletComponents extends Applet implements ActionListener { Button b; TextField tf; public void init() { tf=new TextField(); tf.setBounds(80,40,150,20); b=new Button("Click"); b.setBounds(80,120,80,30); add(b);add(tf); b.addActionListener(this); setLayout(null); } public void actionPerformed(ActionEvent e) { tf.setText("Welcome"); } }
  • 25. JApplet Class: As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class. The components of swing are the components of JApplet,i.e we can use swing components (JButton,JTextField,JCheckBox, JTextArea,JList & etc.) in JApplet.