SlideShare uma empresa Scribd logo
1 de 33
Java
GUI building with the AWT
AWT (Abstract Window Toolkit)








Присутній у всіх реалізаціях
JavaDescribed in most Java textbooks
Достатній для багатьох додатків
Використання елементів управління, визначених
вашим OS
Важко побудувати зрозумілий інтерфейс
import java.awt.*;
import java.awt.event.*;

2
Swing










Same concepts as AWT
Не працює в старших реалізаціях Java(Java 1.1 і
в більш ранішних)
Багато елементів управління і вони більш
гнучкі
Gives a choice of “look and feel” packages
Набагато простіше створювати привабливий
графічний інтерфейс
import javax.swing.*;

3
Swing vs. AWT








Swing більший, повільніший і більш складних
Swing є більш гнучким і краще виглядає
Swing і AWT несумісні - ви можете
використовувати будь-який із них, але ви не
можете змішувати їх
Вивчення AWT є хорошим початком для
вивчення Swing
Багато елементів управління просто
перейменовані


AWT: Button b = new Button ("OK");
Swing: JButton b = new JButton("OK");
4
To build a GUI...








Make somewhere to display things—usually a
Frame or Dialog (for an application), or an Applet
Create some Components, such as buttons, text
areas, panels, etc.
Add your Components to your display area
Arrange, or lay out, your Components
Attach Listeners to your Components



Interacting with a Component causes an Event to occur
A Listener gets a message when an interesting event
occurs, and executes some code to deal with it

5
Containers and Components


The job of a Container is to hold and display
Components



Some common subclasses of Component are Button,
Checkbox, Label, Scrollbar, TextField, and
TextArea



A Container is also a Component




This allows Containers to be nested

Some Container subclasses are Panel (and Applet),
Window, and Frame
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 an Applet
7
Example: A "Life" applet
Container (Applet)
Containers (Panels)
Component (Canvas)
Components (Buttons)
Components (TextFields)
Components (Labels)

8
Applets







An application has a
public static void main(String args[ ]) method, but
an Applet usually does not
An Applet's main method is in the Browser
To write an Applet, you extend Applet and override
some of its methods
The most important methods are init( ), start( ), and
paint(Graphics g)

9
To create an applet


public class MyApplet extends Applet { … }








this is the only way to make an Applet

You can add components to the applet
The best place to add components is in init( )
You can paint directly on the applet, but…
…it’s better to paint on a contained component
Do all painting from paint(Graphics g)

10
Some types of components
Button

Label

Scrollbar

Choice
TextField

Checkbox

List

TextArea

Button

Checkbox

CheckboxGroup
11
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);

12
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);
...

13
Creating a Frame



When you create an Applet, you get a Panel “for free”
When you write a GUI for an application, you need to
create and use a Frame:








Frame frame = new Frame();
frame.setTitle("My Frame");
frame.setSize(300, 200); // width, height
... add components ...
frame.setVisible(true);

Or:



class MyClass extends Frame {
...
setTitle("My Frame"); // in some instance method
14
Arranging components








Every Container has a layout manager
The default layout for a Panel is FlowLayout
An Applet is a Panel
Therefore, 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

15
FlowLayout








Use add(component); to add to a component when
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

16
Complete example: FlowLayout
import java.awt.*;
import java.applet.*;
public class FlowLayoutExample extends Applet
{
public void init () {
setLayout (new FlowLayout ()); // default
add (new Button ("One"));
add (new Button ("Two"));
add (new Button ("Three"));
add (new Button ("Four"));
add (new Button ("Five"));
add (new Button ("Six"));
}
}
17
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 (new Button("NORTH"), BorderLayout.NORTH);
18
BorderLayout with five Buttons
public void init() {
setLayout (new BorderLayout ());
add (new Button ("NORTH"), BorderLayout.NORTH);
add (new Button ("SOUTH"), BorderLayout.SOUTH);
add (new Button ("EAST"), BorderLayout.EAST);
add (new Button ("WEST"), BorderLayout.WEST);
add (new Button ("CENTER"), BorderLayout.CENTER);
}

19
Complete example: BorderLayout
import java.awt.*;
import java.applet.*;
public class BorderLayoutExample extends Applet {
public void init () {
setLayout (new BorderLayout());
add(new Button("One"), BorderLayout.NORTH);
add(new Button("Two"), BorderLayout.WEST);
add(new Button("Three"), BorderLayout.CENTER);
add(new Button("Four"), BorderLayout.EAST);
add(new Button("Five"), BorderLayout.SOUTH);
add(new Button("Six"), BorderLayout.SOUTH);
}
}
20
Using a Panel
Panel p = new Panel();
add (p, BorderLayout.SOUTH);
p.add (new Button ("Button 1"));
p.add (new Button ("Button 2"));

21
GridLayout


The GridLayout manager
divides the container up into
a given number of rows and
columns:
new GridLayout(rows, columns)



All sections of the grid are equally sized and as large as
possible

22
Complete example: GridLayout
import java.awt.*;
import java.applet.*;
public class GridLayoutExample extends Applet {
public void init () {
setLayout(new GridLayout(2, 3));
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
add(new Button("Five"));
}
}

23
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

24
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

25
Writing a Listener


For a Button, you need an ActionListener
b1.addActionListener
(new MyButtonListener ( ));



An ActionListener must have an
actionPerformed(ActionEvent) method
public void actionPerformed(ActionEvent e) {
…
}

26
MyButtonListener

public void init () {
...
b1.addActionListener (new MyButtonListener ());
}
class MyButtonListener implements ActionListener {
public void actionPerformed (ActionEvent e) {
showStatus ("Ouch!");
}
}
27
Listeners for TextFields




An ActionListener listens for someone hitting the
Enter key
An ActionListener requires this method:
public void actionPerformed (ActionEvent e)



You can use getText( ) to get the text



A TextListener listens for any and all keys
A TextListener requires this method:



public void textValueChanged(TextEvent e)

28
AWT and Swing


AWT Buttons vs. Swing JButtons:





Containers:






Swing uses the AWT layout managers, plus a couple of its own

Listeners:




A Frame is a Window is a Container is a Component
A JFrame is a Frame, etc.

Layout managers:




Swing uses AWT Containers

AWT Frames vs. Swing JFrames:




A Button is a Component
A JButton is an AbstractButton, which is a JComponent, which is a
Container, which is a Component

Swing uses many of the AWT listeners, plus a couple of its own

Bottom line: Not only is there a lot of similarity between AWT and Swing,
but Swing actually uses much of the AWT
29
Summary I: Building a GUI






Create a container, such as Frame or Applet
Choose a layout manager
Create more complex layouts by adding Panels; each
Panel can have its own layout manager
Create other components and add them to whichever
Panels you like

30
Summary II: Building a GUI




For each active component, look up what kind of
Listeners it can have
Create (implement) the Listeners







often there is one Listener for each active component
Active components can share the same Listener

For each Listener you implement, supply the methods
that it requires
For Applets, write the necessary HTML

31
Vocabulary











AWT – The Abstract Window Toolkit provides basic graphics
tools (tools for putting information on the screen)
Swing – A much better set of graphics tools
Container – a graphic element that can hold other graphic
elements (and is itself a Component)
Component – a graphic element (such as a Button or a
TextArea) provided by a graphics toolkit
listener – A piece of code that is activated when a particular kind
of event occurs
layout manager – An object whose job it is to arrange
Components in a Container

32
The End

33

Mais conteúdo relacionado

Mais procurados

Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletraksharao
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingPayal Dungarwal
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and ComponentsSohanur63
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swingadil raja
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTPayal Dungarwal
 
Complete java swing
Complete java swingComplete java swing
Complete java swingjehan1987
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solutionMazenetsolution
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Javasuraj pandey
 

Mais procurados (20)

Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of applet
 
Advance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.SwingAdvance Java Programming (CM5I) 2.Swing
Advance Java Programming (CM5I) 2.Swing
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Awt components
Awt componentsAwt components
Awt components
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
Event handling
Event handlingEvent handling
Event handling
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
Awt components
Awt componentsAwt components
Awt components
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Swing
SwingSwing
Swing
 
Java swing
Java swingJava swing
Java swing
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 

Semelhante a 25 awt

Awt - Swings-- Applets In java
Awt - Swings-- Applets In javaAwt - Swings-- Applets In java
Awt - Swings-- Applets In javaMD SALEEM QAISAR
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdfJEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdfMarlouFelixIIICunana
 
GUI (graphical user interface)
GUI (graphical user interface)GUI (graphical user interface)
GUI (graphical user interface)rishi ram khanal
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on SwingsMohanYedatkar
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Mikkel Flindt Heisterberg
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swingPrashant Mehta
 
Gui in matlab :
Gui in matlab :Gui in matlab :
Gui in matlab :elboob2025
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.pptTabassumMaktum
 
Using prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesUsing prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesDavid Voyles
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxjonathancapitulo2
 
Advanced java lab swing mvc awt
Advanced java lab swing mvc awtAdvanced java lab swing mvc awt
Advanced java lab swing mvc awtvishal choudhary
 

Semelhante a 25 awt (20)

Gui
GuiGui
Gui
 
Ingles 2do parcial
Ingles   2do parcialIngles   2do parcial
Ingles 2do parcial
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Awt - Swings-- Applets In java
Awt - Swings-- Applets In javaAwt - Swings-- Applets In java
Awt - Swings-- Applets In java
 
03_GUI.ppt
03_GUI.ppt03_GUI.ppt
03_GUI.ppt
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdfJEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
 
GUI (graphical user interface)
GUI (graphical user interface)GUI (graphical user interface)
GUI (graphical user interface)
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
java presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swingsjava presentation on Swings chapter java presentation on Swings
java presentation on Swings chapter java presentation on Swings
 
Java session13
Java session13Java session13
Java session13
 
Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)Plug yourself in and your app will never be the same (1 hr edition)
Plug yourself in and your app will never be the same (1 hr edition)
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swing
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Gui in matlab :
Gui in matlab :Gui in matlab :
Gui in matlab :
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.ppt
 
Using prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile servicesUsing prime[31] to connect your unity game to azure mobile services
Using prime[31] to connect your unity game to azure mobile services
 
Computer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptxComputer Programming NC III - Java Swing.pptx
Computer Programming NC III - Java Swing.pptx
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Advanced java lab swing mvc awt
Advanced java lab swing mvc awtAdvanced java lab swing mvc awt
Advanced java lab swing mvc awt
 

Último

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 

Último (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

25 awt

  • 2. AWT (Abstract Window Toolkit)       Присутній у всіх реалізаціях JavaDescribed in most Java textbooks Достатній для багатьох додатків Використання елементів управління, визначених вашим OS Важко побудувати зрозумілий інтерфейс import java.awt.*; import java.awt.event.*; 2
  • 3. Swing       Same concepts as AWT Не працює в старших реалізаціях Java(Java 1.1 і в більш ранішних) Багато елементів управління і вони більш гнучкі Gives a choice of “look and feel” packages Набагато простіше створювати привабливий графічний інтерфейс import javax.swing.*; 3
  • 4. Swing vs. AWT      Swing більший, повільніший і більш складних Swing є більш гнучким і краще виглядає Swing і AWT несумісні - ви можете використовувати будь-який із них, але ви не можете змішувати їх Вивчення AWT є хорошим початком для вивчення Swing Багато елементів управління просто перейменовані  AWT: Button b = new Button ("OK"); Swing: JButton b = new JButton("OK"); 4
  • 5. To build a GUI...      Make somewhere to display things—usually a Frame or Dialog (for an application), or an Applet Create some Components, such as buttons, text areas, panels, etc. Add your Components to your display area Arrange, or lay out, your Components Attach Listeners to your Components   Interacting with a Component causes an Event to occur A Listener gets a message when an interesting event occurs, and executes some code to deal with it 5
  • 6. Containers and Components  The job of a Container is to hold and display Components  Some common subclasses of Component are Button, Checkbox, Label, Scrollbar, TextField, and TextArea  A Container is also a Component   This allows Containers to be nested Some Container subclasses are Panel (and Applet), Window, and Frame 6
  • 7. 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 an Applet 7
  • 8. Example: A "Life" applet Container (Applet) Containers (Panels) Component (Canvas) Components (Buttons) Components (TextFields) Components (Labels) 8
  • 9. Applets     An application has a public static void main(String args[ ]) method, but an Applet usually does not An Applet's main method is in the Browser To write an Applet, you extend Applet and override some of its methods The most important methods are init( ), start( ), and paint(Graphics g) 9
  • 10. To create an applet  public class MyApplet extends Applet { … }       this is the only way to make an Applet You can add components to the applet The best place to add components is in init( ) You can paint directly on the applet, but… …it’s better to paint on a contained component Do all painting from paint(Graphics g) 10
  • 11. Some types of components Button Label Scrollbar Choice TextField Checkbox List TextArea Button Checkbox CheckboxGroup 11
  • 12. 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); 12
  • 13. 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); ... 13
  • 14. Creating a Frame   When you create an Applet, you get a Panel “for free” When you write a GUI for an application, you need to create and use a Frame:       Frame frame = new Frame(); frame.setTitle("My Frame"); frame.setSize(300, 200); // width, height ... add components ... frame.setVisible(true); Or:   class MyClass extends Frame { ... setTitle("My Frame"); // in some instance method 14
  • 15. Arranging components       Every Container has a layout manager The default layout for a Panel is FlowLayout An Applet is a Panel Therefore, 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 15
  • 16. FlowLayout       Use add(component); to add to a component when 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 16
  • 17. Complete example: FlowLayout import java.awt.*; import java.applet.*; public class FlowLayoutExample extends Applet { public void init () { setLayout (new FlowLayout ()); // default add (new Button ("One")); add (new Button ("Two")); add (new Button ("Three")); add (new Button ("Four")); add (new Button ("Five")); add (new Button ("Six")); } } 17
  • 18. 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 (new Button("NORTH"), BorderLayout.NORTH); 18
  • 19. BorderLayout with five Buttons public void init() { setLayout (new BorderLayout ()); add (new Button ("NORTH"), BorderLayout.NORTH); add (new Button ("SOUTH"), BorderLayout.SOUTH); add (new Button ("EAST"), BorderLayout.EAST); add (new Button ("WEST"), BorderLayout.WEST); add (new Button ("CENTER"), BorderLayout.CENTER); } 19
  • 20. Complete example: BorderLayout import java.awt.*; import java.applet.*; public class BorderLayoutExample extends Applet { public void init () { setLayout (new BorderLayout()); add(new Button("One"), BorderLayout.NORTH); add(new Button("Two"), BorderLayout.WEST); add(new Button("Three"), BorderLayout.CENTER); add(new Button("Four"), BorderLayout.EAST); add(new Button("Five"), BorderLayout.SOUTH); add(new Button("Six"), BorderLayout.SOUTH); } } 20
  • 21. Using a Panel Panel p = new Panel(); add (p, BorderLayout.SOUTH); p.add (new Button ("Button 1")); p.add (new Button ("Button 2")); 21
  • 22. GridLayout  The GridLayout manager divides the container up into a given number of rows and columns: new GridLayout(rows, columns)  All sections of the grid are equally sized and as large as possible 22
  • 23. Complete example: GridLayout import java.awt.*; import java.applet.*; public class GridLayoutExample extends Applet { public void init () { setLayout(new GridLayout(2, 3)); add(new Button("One")); add(new Button("Two")); add(new Button("Three")); add(new Button("Four")); add(new Button("Five")); } } 23
  • 24. 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 24
  • 25. 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 25
  • 26. Writing a Listener  For a Button, you need an ActionListener b1.addActionListener (new MyButtonListener ( ));  An ActionListener must have an actionPerformed(ActionEvent) method public void actionPerformed(ActionEvent e) { … } 26
  • 27. MyButtonListener public void init () { ... b1.addActionListener (new MyButtonListener ()); } class MyButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { showStatus ("Ouch!"); } } 27
  • 28. Listeners for TextFields   An ActionListener listens for someone hitting the Enter key An ActionListener requires this method: public void actionPerformed (ActionEvent e)  You can use getText( ) to get the text  A TextListener listens for any and all keys A TextListener requires this method:  public void textValueChanged(TextEvent e) 28
  • 29. AWT and Swing  AWT Buttons vs. Swing JButtons:    Containers:    Swing uses the AWT layout managers, plus a couple of its own Listeners:   A Frame is a Window is a Container is a Component A JFrame is a Frame, etc. Layout managers:   Swing uses AWT Containers AWT Frames vs. Swing JFrames:   A Button is a Component A JButton is an AbstractButton, which is a JComponent, which is a Container, which is a Component Swing uses many of the AWT listeners, plus a couple of its own Bottom line: Not only is there a lot of similarity between AWT and Swing, but Swing actually uses much of the AWT 29
  • 30. Summary I: Building a GUI     Create a container, such as Frame or Applet Choose a layout manager Create more complex layouts by adding Panels; each Panel can have its own layout manager Create other components and add them to whichever Panels you like 30
  • 31. Summary II: Building a GUI   For each active component, look up what kind of Listeners it can have Create (implement) the Listeners     often there is one Listener for each active component Active components can share the same Listener For each Listener you implement, supply the methods that it requires For Applets, write the necessary HTML 31
  • 32. Vocabulary       AWT – The Abstract Window Toolkit provides basic graphics tools (tools for putting information on the screen) Swing – A much better set of graphics tools Container – a graphic element that can hold other graphic elements (and is itself a Component) Component – a graphic element (such as a Button or a TextArea) provided by a graphics toolkit listener – A piece of code that is activated when a particular kind of event occurs layout manager – An object whose job it is to arrange Components in a Container 32