SlideShare ist ein Scribd-Unternehmen logo
1 von 37
  Building Java GUI
Objectives  * Describe the AWT package and its components. * Define the terms  containers ,  components,  and  layout  managers , and how they work together to build a  graphical user interface (GUI)  * Use layout managers * Use the FlowLayout, BorderLayout, GridLayout, and CardLayout managers to achieve a desired dynamic  layout. * Add components to a container * Use the Frame and Panel containers appropriately
* Describe how complex layouts with nested containers  work.  * In a Java program, identify the following: Containers The associated layout managers  The layout hierarchy of all components
  Abstract Window Toolkit (AWT) Provides graphical user interface (GUI) components that are used in all Java applets and application. Contains classes that can be extended and their properties  inherited; classes can also be abstract. Ensures that every GUI component that is displayed on the screen  is a subclass of the abstract class Component or MenuComponent Has Container, which is an abstract subclass of Component  and includes two subclasses:  * Panel * Window
 
Containers * The two main types of containers are Window and  Panel  * A Window is a free floating window on the display * A Panel is a container of GUI components that  must exist in the context of some other container,  such as a window or applet
 
Building Graphical User Interfaces The position and size of a component in a container  is determined by a layout manager. You can control the size or position of components  by disabling the layout manager. You must then use setLocation(), setSize(),or  setBounds() on components to locate them in the  container.
Frames * Are a subclass of Window * Have title and resizing corners * Are initially invisible, use setVisible(true) to expose the  frame * Have BorderLayout as the default layout manager * Use the setLayout method to change the default layout  manager
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FrameExample.java
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
FrameWithPanel.java
[object Object],[object Object],[object Object],[object Object],[object Object]
  Default Layout Manager
[object Object],[object Object],[object Object],[object Object],[object Object]
public static void main(String args[]) { LayoutExample guiWindow = new LayoutExample();   guiWindow.launchFrame();   } }
FlowLayoutManager  •  Default layout for the Panel class  •  Components added from left to right •  Default alignment is centered  •  Uses components’ preferred sizes  •  Uses the constructor to tune behavior
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
FlowExample.java
BorderLayoutManager  Default layout for the Frame class Components added to specific regions The resizing behavior: North, South, and Center regions adjust horizontally East, West, and Center regions adjust vertically
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
GridLayoutManager  •  Components are added left to right, top to bottom •  All regions are equally sized  •  The constructor specifies the rows and columns
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Card Layout   The  CardLayout  class is unique among the other layout managers in that it stores several different layouts. This can be useful for user interfaces with optional components  that can be dynamically enabled and disabled upon user input. The cards are typically held in an object of type  Panel . This panel must have  CardLayout  selected as its layout manager.
[object Object]
[object Object],[object Object],[object Object],[object Object]
lb1 = new Label("This is the first Panel"); lb2 = new Label("This is the second Panel"); lb3 = new Label("This is the third Panel"); lb4 = new Label("This is the fourth Panel"); lb5 = new Label("This is the fifth Panel"); } public void launchFrame() { f.setLayout(myCard); // change the color of each panel, so they are // easily distinguishable p1.setBackground(Color.yellow); p1.add(lb1); p2.setBackground(Color.green); p2.add(lb2); p3.setBackground(Color.magenta); p3.add(lb3); p4.setBackground(Color.white); p4.add(lb4); p5.setBackground(Color.cyan); p5.add(lb5);
// Set up the event handling here. p1.addMouseListener(this); p2.addMouseListener(this); p3.addMouseListener(this); p4.addMouseListener(this); p5.addMouseListener(this); // Add each panel to my CardLayout f.add(p1, "First"); f.add(p2, "Second"); f.add(p3, "Third"); f.add(p4, "Fourth"); f.add(p5, "Fifth"); // Display the first panel. myCard.show(f, "First"); f.setSize(200,200); f.setVisible(true) }
public void mousePressed(MouseEvent e) {  myCard.next(f);  }  public void mouseReleased(MouseEvent e) { }  public void mouseClicked(MouseEvent e) { }  public void mouseEntered(MouseEvent e) { }  public void mouseExited(MouseEvent e) { }  public static void main (String args[]) {  CardExample ct = new CardExample();  ct.launchFrame();  }  }

Weitere ähnliche Inhalte

Was ist angesagt?

Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answersMadhu Dhare
 
Phoenix for laravel developers
Phoenix for laravel developersPhoenix for laravel developers
Phoenix for laravel developersLuiz Messias
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and ComponentsSohanur63
 

Was ist angesagt? (8)

Dbms file
Dbms fileDbms file
Dbms file
 
Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answers
 
Les09
Les09Les09
Les09
 
Java swing
Java swingJava swing
Java swing
 
Phoenix for laravel developers
Phoenix for laravel developersPhoenix for laravel developers
Phoenix for laravel developers
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
Java swing
Java swingJava swing
Java swing
 
Oracle: DML
Oracle: DMLOracle: DML
Oracle: DML
 

Andere mochten auch

Keep it simple, stupid! w/ speaker notes
Keep it simple, stupid! w/ speaker notesKeep it simple, stupid! w/ speaker notes
Keep it simple, stupid! w/ speaker notesMoritz Rogalli
 
Edward R. Fyfe charity book
Edward R. Fyfe charity bookEdward R. Fyfe charity book
Edward R. Fyfe charity bookmararistina
 
Jody Warnick CV 2014 new
Jody Warnick CV 2014 newJody Warnick CV 2014 new
Jody Warnick CV 2014 newJody Warnick
 
Presentation Smyers
Presentation SmyersPresentation Smyers
Presentation Smyersjmovitt
 
Article review
Article review Article review
Article review kemakamal
 
SONG YU HUI CV UPDATED
SONG YU HUI CV UPDATEDSONG YU HUI CV UPDATED
SONG YU HUI CV UPDATEDSong Yu Hui
 
National Cooperative Month Observed in October
National Cooperative Month Observed in OctoberNational Cooperative Month Observed in October
National Cooperative Month Observed in OctoberCarl Casale
 
Modelos de Gestão de Pessoas em ambiente de fusões e aquisições
Modelos de Gestão de Pessoas em ambiente de fusões e aquisiçõesModelos de Gestão de Pessoas em ambiente de fusões e aquisições
Modelos de Gestão de Pessoas em ambiente de fusões e aquisiçõesAnderson Rafael Brugnera
 
Best Mobile App Development Services in India
Best Mobile App Development Services in IndiaBest Mobile App Development Services in India
Best Mobile App Development Services in IndiaSteve Verma
 
The Real World June 2016
The Real World June 2016The Real World June 2016
The Real World June 2016Posterscope
 
The binomial distributions
The binomial distributionsThe binomial distributions
The binomial distributionsmaamir farooq
 
Docker and Microservice
Docker and MicroserviceDocker and Microservice
Docker and MicroserviceSamuel Chow
 

Andere mochten auch (16)

Ricky ortegon
Ricky ortegonRicky ortegon
Ricky ortegon
 
101017cnw2sa
101017cnw2sa101017cnw2sa
101017cnw2sa
 
Keep it simple, stupid! w/ speaker notes
Keep it simple, stupid! w/ speaker notesKeep it simple, stupid! w/ speaker notes
Keep it simple, stupid! w/ speaker notes
 
Lambdas
LambdasLambdas
Lambdas
 
Edward R. Fyfe charity book
Edward R. Fyfe charity bookEdward R. Fyfe charity book
Edward R. Fyfe charity book
 
Jody Warnick CV 2014 new
Jody Warnick CV 2014 newJody Warnick CV 2014 new
Jody Warnick CV 2014 new
 
Presentation Smyers
Presentation SmyersPresentation Smyers
Presentation Smyers
 
Article review
Article review Article review
Article review
 
SONG YU HUI CV UPDATED
SONG YU HUI CV UPDATEDSONG YU HUI CV UPDATED
SONG YU HUI CV UPDATED
 
National Cooperative Month Observed in October
National Cooperative Month Observed in OctoberNational Cooperative Month Observed in October
National Cooperative Month Observed in October
 
Modelos de Gestão de Pessoas em ambiente de fusões e aquisições
Modelos de Gestão de Pessoas em ambiente de fusões e aquisiçõesModelos de Gestão de Pessoas em ambiente de fusões e aquisições
Modelos de Gestão de Pessoas em ambiente de fusões e aquisições
 
Best Mobile App Development Services in India
Best Mobile App Development Services in IndiaBest Mobile App Development Services in India
Best Mobile App Development Services in India
 
The Real World June 2016
The Real World June 2016The Real World June 2016
The Real World June 2016
 
Amul public relation strategy REPORT
Amul public relation strategy REPORTAmul public relation strategy REPORT
Amul public relation strategy REPORT
 
The binomial distributions
The binomial distributionsThe binomial distributions
The binomial distributions
 
Docker and Microservice
Docker and MicroserviceDocker and Microservice
Docker and Microservice
 

Ähnlich wie Md10 building java gu is

Ähnlich wie Md10 building java gu is (20)

09 gui
09 gui09 gui
09 gui
 
09 gui
09 gui09 gui
09 gui
 
09 gui
09 gui09 gui
09 gui
 
LAYOUT.pptx
LAYOUT.pptxLAYOUT.pptx
LAYOUT.pptx
 
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
 
Chap1 1 4
Chap1 1 4Chap1 1 4
Chap1 1 4
 
Java Graphics Programming
Java Graphics ProgrammingJava Graphics Programming
Java Graphics Programming
 
Chap1 1.4
Chap1 1.4Chap1 1.4
Chap1 1.4
 
ADVANCED JAVA PROGRAMME
ADVANCED JAVA PROGRAMME ADVANCED JAVA PROGRAMME
ADVANCED JAVA PROGRAMME
 
Advance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWTAdvance Java Programming (CM5I) 1.AWT
Advance Java Programming (CM5I) 1.AWT
 
Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
 
ch20.pptx
ch20.pptxch20.pptx
ch20.pptx
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Final_Project
Final_ProjectFinal_Project
Final_Project
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
import java.awt.;class FlowLayoutDemo {    public static void.pdf
import java.awt.;class FlowLayoutDemo {    public static void.pdfimport java.awt.;class FlowLayoutDemo {    public static void.pdf
import java.awt.;class FlowLayoutDemo {    public static void.pdf
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 

Mehr von Rakesh Madugula

Mehr von Rakesh Madugula (13)

New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Md121 streams
Md121 streamsMd121 streams
Md121 streams
 
Md11 gui event handling
Md11 gui event handlingMd11 gui event handling
Md11 gui event handling
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Md08 collection api
Md08 collection apiMd08 collection api
Md08 collection api
 
Md07 exceptions&assertion
Md07 exceptions&assertionMd07 exceptions&assertion
Md07 exceptions&assertion
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
Md05 arrays
Md05 arraysMd05 arrays
Md05 arrays
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
A begineers guide of JAVA - Getting Started
 A begineers guide of JAVA - Getting Started A begineers guide of JAVA - Getting Started
A begineers guide of JAVA - Getting Started
 

Kürzlich hochgeladen

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
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
 
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
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 

Kürzlich hochgeladen (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
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
 
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
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Md10 building java gu is

  • 1. Building Java GUI
  • 2. Objectives * Describe the AWT package and its components. * Define the terms containers , components, and layout managers , and how they work together to build a graphical user interface (GUI) * Use layout managers * Use the FlowLayout, BorderLayout, GridLayout, and CardLayout managers to achieve a desired dynamic layout. * Add components to a container * Use the Frame and Panel containers appropriately
  • 3. * Describe how complex layouts with nested containers work. * In a Java program, identify the following: Containers The associated layout managers The layout hierarchy of all components
  • 4. Abstract Window Toolkit (AWT) Provides graphical user interface (GUI) components that are used in all Java applets and application. Contains classes that can be extended and their properties inherited; classes can also be abstract. Ensures that every GUI component that is displayed on the screen is a subclass of the abstract class Component or MenuComponent Has Container, which is an abstract subclass of Component and includes two subclasses: * Panel * Window
  • 5.  
  • 6. Containers * The two main types of containers are Window and Panel * A Window is a free floating window on the display * A Panel is a container of GUI components that must exist in the context of some other container, such as a window or applet
  • 7.  
  • 8. Building Graphical User Interfaces The position and size of a component in a container is determined by a layout manager. You can control the size or position of components by disabling the layout manager. You must then use setLocation(), setSize(),or setBounds() on components to locate them in the container.
  • 9. Frames * Are a subclass of Window * Have title and resizing corners * Are initially invisible, use setVisible(true) to expose the frame * Have BorderLayout as the default layout manager * Use the setLayout method to change the default layout manager
  • 10.
  • 12.
  • 13.
  • 14.
  • 16.
  • 17. Default Layout Manager
  • 18.
  • 19. public static void main(String args[]) { LayoutExample guiWindow = new LayoutExample(); guiWindow.launchFrame(); } }
  • 20. FlowLayoutManager • Default layout for the Panel class • Components added from left to right • Default alignment is centered • Uses components’ preferred sizes • Uses the constructor to tune behavior
  • 21.
  • 22.
  • 24. BorderLayoutManager Default layout for the Frame class Components added to specific regions The resizing behavior: North, South, and Center regions adjust horizontally East, West, and Center regions adjust vertically
  • 25.
  • 26.
  • 27.
  • 28. GridLayoutManager • Components are added left to right, top to bottom • All regions are equally sized • The constructor specifies the rows and columns
  • 29.
  • 30.
  • 31.
  • 32. Card Layout The CardLayout class is unique among the other layout managers in that it stores several different layouts. This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input. The cards are typically held in an object of type Panel . This panel must have CardLayout selected as its layout manager.
  • 33.
  • 34.
  • 35. lb1 = new Label("This is the first Panel"); lb2 = new Label("This is the second Panel"); lb3 = new Label("This is the third Panel"); lb4 = new Label("This is the fourth Panel"); lb5 = new Label("This is the fifth Panel"); } public void launchFrame() { f.setLayout(myCard); // change the color of each panel, so they are // easily distinguishable p1.setBackground(Color.yellow); p1.add(lb1); p2.setBackground(Color.green); p2.add(lb2); p3.setBackground(Color.magenta); p3.add(lb3); p4.setBackground(Color.white); p4.add(lb4); p5.setBackground(Color.cyan); p5.add(lb5);
  • 36. // Set up the event handling here. p1.addMouseListener(this); p2.addMouseListener(this); p3.addMouseListener(this); p4.addMouseListener(this); p5.addMouseListener(this); // Add each panel to my CardLayout f.add(p1, "First"); f.add(p2, "Second"); f.add(p3, "Third"); f.add(p4, "Fourth"); f.add(p5, "Fifth"); // Display the first panel. myCard.show(f, "First"); f.setSize(200,200); f.setVisible(true) }
  • 37. public void mousePressed(MouseEvent e) { myCard.next(f); } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main (String args[]) { CardExample ct = new CardExample(); ct.launchFrame(); } }