SlideShare a Scribd company logo
AWT (Abstract Window Toolkit)
Present in all Java implementations
Described in (almost) every Java textbook
Adequate for many applications
Uses the controls defined by your OS
therefore it's "least common denominator"
Difficult to build an attractive GUI
import java.awt.*;
23-03-2018 SALEEM QAISAR 1
Swing
Requires Java 2 or a separate (huge) download
More controls, and they are more flexible
Gives a choice of “look and feel” packages
Much easier to build an attractive GUI
import javax.swing.*; // Mac or PC
import com.sun.java.swing.*; // UNIX
23-03-2018 SALEEM QAISAR 2
Swing vs. AWT
Swing is bigger and slower
Swing is more flexible and better looking
Swing and AWT are incompatible--you can use either,
but you can’t mix them
Learning the AWT is a good start on learning Swing
AWT: Button b = new Button (“OK”);
Swing: Jbutton b = new Jbutton(“OK”);
23-03-2018 SALEEM QAISAR 3
To build a GUI...
Make somewhere to display things--a Frame, a
Window, or an Applet
Create controls (buttons, text areas, etc.)
Add your controls to your display area
Arrange, or lay out, your controls
Attach Listeners actions to your controls
23-03-2018 SALEEM QAISAR 4
Containers and Components
The job of a Container is to hold and display Components.
A Container is also a Component
Some common subclasses of Component are Button,
Checkbox, Label, Scrollbar, TextField, and TextArea
Some Container subclasses are Panel (and Applet),
Window, and Frame
23-03-2018 SALEEM QAISAR 5
An Applet is panel is a container
java.lang.Object
|
+----java.awt.Component
|
+----java.awt.Container
|
+----java.awt.Panel
|
+----java.applet.Applet
…so you can display things in it
23-03-2018 SALEEM QAISAR 6
To create an applet
class MyApplet extends Applet { … }
this is the only way to make an Applet
You can add components to the applet
It’s best to add components in init( )
You can paint directly on the applet, but…
…it’s better to paint on another component
Do all painting from paint(Graphics g)
23-03-2018 SALEEM QAISAR 7
Some types of components
23-03-2018 SALEEM QAISAR 8
Creating components
 Label lab = new Label (”Hi, Dave!");
 Button but = new Button ("Click me!");
 Checkbox toggle = new Checkbox (”toggle");
 TextField txt =
new TextField (”Initial text.", 20);
 Scrollbar scrolly = new Scrollbar
(Scrollbar.HORIZONTAL, initialValue,
bubbleSize, minValue, maxValue);
23-03-2018 SALEEM QAISAR 9
Adding components to the Applet
class MyApplet extends Applet {
public void init () {
add (lab); // same as this.add(lab)
add (but);
add (toggle);
add (txt);
add (scrolly);
23-03-2018 SALEEM QAISAR 10
Arranging components
Every Container has a layout manager
The default layout for a Panel is FlowLayout
An Applet is a Panel
The default layout for a Applet is FlowLayout
You could set it explicitly with
setLayout (new FlowLayout( ));
You could change it to some other layout
manager.
23-03-2018 SALEEM QAISAR 11
FlowLayout
Use add (component); to add to a component using
a FlowLayout
Components are added left-to-right
If no room, a new row is started
Exact layout depends on size of Applet
Components are made as small as possible
FlowLayout is convenient but often ugly
23-03-2018 SALEEM QAISAR 12
BorderLayout
At most five components
can be added
If you want more
components, add a Panel,
then add components to it.
setLayout (new
BorderLayout());
add (BorderLayout.NORTH, new Button(“NORTH”));
23-03-2018 SALEEM QAISAR 13
BorderLayout with five Buttons
public void init() {
setLayout (new BorderLayout ());
add (BorderLayout.NORTH, new Button ("NORTH"));
add (BorderLayout.SOUTH, new Button ("SOUTH"));
add (BorderLayout.EAST, new Button ("EAST"));
add (BorderLayout.WEST, new Button ("WEST"));
add (BorderLayout.CENTER, new Button ("CENTER"));
}
23-03-2018 SALEEM QAISAR 14
Using a Panel
panel p = new Panel();
add
(BorderLayout.SOUTH, p);
p.add (new Button (“Button
1”));
p.add (new Button (“Button
2”));
23-03-2018 SALEEM QAISAR 15
Making components active
Most components already appear to do something--
buttons click, text appears
To associate an action with a component, attach a
listener to it
Components send events, listeners listen for events
Different components may send different events, and
require different listeners
23-03-2018 SALEEM QAISAR 16
Listeners
Listeners are interfaces, not classes
Class MyButtonListener implements
ActionListener {
An interface is a group of methods that must be
supplied
When you say implements, you are promising to
supply those methods
23-03-2018 SALEEM QAISAR 17
Writing a Listener
For a Button, you need an ActionListener
b1.addActionListener(new MyButtonListener ( ));
An ActionListener must have an
actionPerformed() method
public void actionPerformed(ActionEvent e) {…}
23-03-2018 SALEEM QAISAR 18
MyButtonListener
public void init () {
...
b1.addActionListener (new MyButtonListener ());
}
class MyButtonListener implements
ActionListener {
public void actionPerformed (ActionEvent e) {
showStatus ("Ouch!");
}
}
23-03-2018 SALEEM QAISAR 19
Listeners for TextFields
 An ActionListener listens for hitting the return key
 An ActionListener demands
public void actionPerformed (ActionEvent e)
 use getText( ) to get the text
 A TextListener listens for any and all keys
 A TextListener demands
public void textValueChanged(TextEvent e)
23-03-2018 SALEEM QAISAR 20

More Related Content

Similar to Awt - Swings-- Applets In java

Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
Hemo Chella
 
swingbasics
swingbasicsswingbasics
swingbasics
Arjun Shanka
 
Swing
SwingSwing
Programming Without Coding Technology (PWCT) - How to deal with Arrays
Programming Without Coding Technology (PWCT) - How to deal with ArraysProgramming Without Coding Technology (PWCT) - How to deal with Arrays
Programming Without Coding Technology (PWCT) - How to deal with Arrays
Mahmoud Samir Fayed
 
Gui in matlab :
Gui in matlab :Gui in matlab :
Gui in matlab :
elboob2025
 
Java awt
Java awtJava awt
Programming Without Coding Technology (PWCT) - ShellExplorer Sample
Programming Without Coding Technology (PWCT) - ShellExplorer SampleProgramming Without Coding Technology (PWCT) - ShellExplorer Sample
Programming Without Coding Technology (PWCT) - ShellExplorer Sample
Mahmoud Samir Fayed
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
arnold 7490
 
Applet progming
Applet progmingApplet progming
Applet progming
VIKRANTHMALLIKARJUN
 
Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
Rakesh Madugula
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
Hemo Chella
 
Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1
Muhammad Shebl Farag
 
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
Mahmoud Samir Fayed
 
Day 5
Day 5Day 5
Meet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UIMeet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UI
ICS
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
Riccardo Cardin
 
Getting Started with Starling and Feathers
Getting Started with Starling and FeathersGetting Started with Starling and Feathers
Getting Started with Starling and Feathers
Joseph Labrecque
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
Daroko blog(www.professionalbloggertricks.com)
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
sumitjoshi01
 
Programming Without Coding Technology (PWCT) - Telephone Database
Programming Without Coding Technology (PWCT) - Telephone DatabaseProgramming Without Coding Technology (PWCT) - Telephone Database
Programming Without Coding Technology (PWCT) - Telephone Database
Mahmoud Samir Fayed
 

Similar to Awt - Swings-- Applets In java (20)

Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
 
swingbasics
swingbasicsswingbasics
swingbasics
 
Swing
SwingSwing
Swing
 
Programming Without Coding Technology (PWCT) - How to deal with Arrays
Programming Without Coding Technology (PWCT) - How to deal with ArraysProgramming Without Coding Technology (PWCT) - How to deal with Arrays
Programming Without Coding Technology (PWCT) - How to deal with Arrays
 
Gui in matlab :
Gui in matlab :Gui in matlab :
Gui in matlab :
 
Java awt
Java awtJava awt
Java awt
 
Programming Without Coding Technology (PWCT) - ShellExplorer Sample
Programming Without Coding Technology (PWCT) - ShellExplorer SampleProgramming Without Coding Technology (PWCT) - ShellExplorer Sample
Programming Without Coding Technology (PWCT) - ShellExplorer Sample
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Applet progming
Applet progmingApplet progming
Applet progming
 
Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1Getting started with GUI programming in Java_1
Getting started with GUI programming in Java_1
 
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
Programming Without Coding Technology (PWCT) - Show PDF using InternetExplore...
 
Day 5
Day 5Day 5
Day 5
 
Meet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UIMeet the Widgets: Another Way to Implement UI
Meet the Widgets: Another Way to Implement UI
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
 
Getting Started with Starling and Feathers
Getting Started with Starling and FeathersGetting Started with Starling and Feathers
Getting Started with Starling and Feathers
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
The java swing_tutorial
The java swing_tutorialThe java swing_tutorial
The java swing_tutorial
 
Programming Without Coding Technology (PWCT) - Telephone Database
Programming Without Coding Technology (PWCT) - Telephone DatabaseProgramming Without Coding Technology (PWCT) - Telephone Database
Programming Without Coding Technology (PWCT) - Telephone Database
 

Recently uploaded

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
Kavitha Krishnan
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 

Recently uploaded (20)

C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Assessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptxAssessment and Planning in Educational technology.pptx
Assessment and Planning in Educational technology.pptx
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 

Awt - Swings-- Applets In java

  • 1. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for many applications Uses the controls defined by your OS therefore it's "least common denominator" Difficult to build an attractive GUI import java.awt.*; 23-03-2018 SALEEM QAISAR 1
  • 2. Swing Requires Java 2 or a separate (huge) download More controls, and they are more flexible Gives a choice of “look and feel” packages Much easier to build an attractive GUI import javax.swing.*; // Mac or PC import com.sun.java.swing.*; // UNIX 23-03-2018 SALEEM QAISAR 2
  • 3. Swing vs. AWT Swing is bigger and slower Swing is more flexible and better looking Swing and AWT are incompatible--you can use either, but you can’t mix them Learning the AWT is a good start on learning Swing AWT: Button b = new Button (“OK”); Swing: Jbutton b = new Jbutton(“OK”); 23-03-2018 SALEEM QAISAR 3
  • 4. To build a GUI... Make somewhere to display things--a Frame, a Window, or an Applet Create controls (buttons, text areas, etc.) Add your controls to your display area Arrange, or lay out, your controls Attach Listeners actions to your controls 23-03-2018 SALEEM QAISAR 4
  • 5. Containers and Components The job of a Container is to hold and display Components. A Container is also a Component Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea Some Container subclasses are Panel (and Applet), Window, and Frame 23-03-2018 SALEEM QAISAR 5
  • 6. An Applet is panel is a container java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.Applet …so you can display things in it 23-03-2018 SALEEM QAISAR 6
  • 7. To create an applet class MyApplet extends Applet { … } this is the only way to make an Applet You can add components to the applet It’s best to add components in init( ) You can paint directly on the applet, but… …it’s better to paint on another component Do all painting from paint(Graphics g) 23-03-2018 SALEEM QAISAR 7
  • 8. Some types of components 23-03-2018 SALEEM QAISAR 8
  • 9. Creating components  Label lab = new Label (”Hi, Dave!");  Button but = new Button ("Click me!");  Checkbox toggle = new Checkbox (”toggle");  TextField txt = new TextField (”Initial text.", 20);  Scrollbar scrolly = new Scrollbar (Scrollbar.HORIZONTAL, initialValue, bubbleSize, minValue, maxValue); 23-03-2018 SALEEM QAISAR 9
  • 10. Adding components to the Applet class MyApplet extends Applet { public void init () { add (lab); // same as this.add(lab) add (but); add (toggle); add (txt); add (scrolly); 23-03-2018 SALEEM QAISAR 10
  • 11. Arranging components Every Container has a layout manager The default layout for a Panel is FlowLayout An Applet is a Panel The default layout for a Applet is FlowLayout You could set it explicitly with setLayout (new FlowLayout( )); You could change it to some other layout manager. 23-03-2018 SALEEM QAISAR 11
  • 12. FlowLayout Use add (component); to add to a component using a FlowLayout Components are added left-to-right If no room, a new row is started Exact layout depends on size of Applet Components are made as small as possible FlowLayout is convenient but often ugly 23-03-2018 SALEEM QAISAR 12
  • 13. BorderLayout At most five components can be added If you want more components, add a Panel, then add components to it. setLayout (new BorderLayout()); add (BorderLayout.NORTH, new Button(“NORTH”)); 23-03-2018 SALEEM QAISAR 13
  • 14. BorderLayout with five Buttons public void init() { setLayout (new BorderLayout ()); add (BorderLayout.NORTH, new Button ("NORTH")); add (BorderLayout.SOUTH, new Button ("SOUTH")); add (BorderLayout.EAST, new Button ("EAST")); add (BorderLayout.WEST, new Button ("WEST")); add (BorderLayout.CENTER, new Button ("CENTER")); } 23-03-2018 SALEEM QAISAR 14
  • 15. Using a Panel panel p = new Panel(); add (BorderLayout.SOUTH, p); p.add (new Button (“Button 1”)); p.add (new Button (“Button 2”)); 23-03-2018 SALEEM QAISAR 15
  • 16. Making components active Most components already appear to do something-- buttons click, text appears To associate an action with a component, attach a listener to it Components send events, listeners listen for events Different components may send different events, and require different listeners 23-03-2018 SALEEM QAISAR 16
  • 17. Listeners Listeners are interfaces, not classes Class MyButtonListener implements ActionListener { An interface is a group of methods that must be supplied When you say implements, you are promising to supply those methods 23-03-2018 SALEEM QAISAR 17
  • 18. Writing a Listener For a Button, you need an ActionListener b1.addActionListener(new MyButtonListener ( )); An ActionListener must have an actionPerformed() method public void actionPerformed(ActionEvent e) {…} 23-03-2018 SALEEM QAISAR 18
  • 19. MyButtonListener public void init () { ... b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); } } 23-03-2018 SALEEM QAISAR 19
  • 20. Listeners for TextFields  An ActionListener listens for hitting the return key  An ActionListener demands public void actionPerformed (ActionEvent e)  use getText( ) to get the text  A TextListener listens for any and all keys  A TextListener demands public void textValueChanged(TextEvent e) 23-03-2018 SALEEM QAISAR 20