SlideShare a Scribd company logo
1 of 25
Java Programming Language
Objectives


                • In this session, you will learn to:
                       Describe the Abstract Window Toolkit (AWT) package and its
                       components
                       Define the terms containers, components, and layout
                       managers, and describe how they work together to build a GUI
                       Use the Frame and Panel containers appropriately
                       Add components to a container
                       Use various layout managers to achieve a desired dynamic
                       layout




     Ver. 1.0                          Session 10                          Slide 1 of 25
Java Programming Language
Abstract Window Toolkit


                • Provides GUI components that are used in all Java applets
                  and applications.
                • Contains classes that can be composed or extended.
                • Ensures that every GUI component that is displayed on the
                  screen is a subclass of class Component or
                  MenuComponent.
                • Has Container, which is an abstract subclass of
                  Component and includes two subclasses:
                     Panel
                     Window




     Ver. 1.0                        Session 10                      Slide 2 of 25
Java Programming Language
The java.awt Package


                    Basic overview of AWT package:
                                BorderLayout
                                FlowLayout
                                CardLayout
            Java.lang.object    CheckboxGroup
                                Event
                                Color
                                Font
                                Graphics
                                                                  MenuBar             Menu
                                MenuComponent
                                                                  MenuItem            CheckboxMenuItem
                                GridLayout
                                GridBagLayout
                                Image
                                Rectangle
                                Component
                                                                   Applet

                               Button
                               Canvas                Panel
                               Container             Window                  Dialog
                               Label                 ScrollPane              Frame
                               List                                                       TextArea
                               TextComponen                                               TextField
                               t
     Ver. 1.0                                 Session 10                                        Slide 3 of 25
Java Programming Language
Containers


                  The two main types of containers are:
                      Window
                      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.
                • Add components with the add() method.




     Ver. 1.0                        Session 10                       Slide 4 of 25
Java Programming Language
Components


                 Java programming language supports various components:
                  – Button
                  – Choice
                  – Label
                  – List
                  – Scrollbar
                  – TextComponent, etc.
               • The position and size of a component in a container is
                 determined by a layout manager.
               • You must use setLocation(), setSize(), or
                 setBounds() on components to locate them in the
                 container.



    Ver. 1.0                       Session 10                    Slide 5 of 25
Java Programming Language
Frames


               Frames have the following characteristics:
                – Are a subclass of Window
                – Have title and resizing corners
                – Are invisible initially; use setVisible(true) to expose the
                  frame
                – Have BorderLayout as the default layout manager
                – Use the setLayout() method to change the default layout
                  manager




    Ver. 1.0                       Session 10                          Slide 6 of 25
Java Programming Language
Frames (Contd.)


                An example to create a frame:
                import java.awt.*;
                public class FrameExample {
                private Frame f;                   Declaration of Frame Object
                public FrameExample() {
                                                                      Initialization
                  f = new Frame("Hello Out There!");}                 of Frame
                public void launchFrame() {                           Object
                  f.setSize(170,170);                Setting the size of Frame
                  f.setBackground(Color.blue);
                  f.setVisible(true); }                     Making Frame visible
                public static void main(String args[]) {
                  FrameExample guiWindow = new FrameExample();
                  guiWindow.launchFrame(); }
                }




     Ver. 1.0                       Session 10                             Slide 7 of 25
Java Programming Language
Panels


                • Panel provide a space for components.
                • This enables subpanels to have their own layout manager.
                • After creating Panel, it must be added to a Window or
                  Frame.




     Ver. 1.0                        Session 10                     Slide 8 of 25
Java Programming Language
Panels (Contd.)


                The following code snippet helps in creating a small yellow
                panel and adds it to a frame:
                 public Panel pan;                       Declaring Panel Object
                 public Frame f;
                 f=new Frame( “I’m with panel”);
                 pan = new Panel();                      Initializing Panel Object
                 public void launchFrame()
                 {
                 f.setSize(200,200);
                 f.setLayout(null); // Use default layout
                 pan.setSize(100,100);                    Setting the size of Panel
                 pan.setBackground(Color.yellow);                 Giving the
                 f.add(pan);                                      yellow color to
                                                                  the Panel
                 f.setVisible(true);
                                                Adding Panel to the Frame
                 }


     Ver. 1.0                        Session 10                            Slide 9 of 25
Java Programming Language
Layout Managers


               Layout managers are used to place the components on
               required positions.
               The following layout managers are included with the Java
               programming language:
                  FlowLayout
                  BorderLayout
                  GridLayout
                  CardLayout
                  GridBagLayout




    Ver. 1.0                     Session 10                       Slide 10 of 25
Java Programming Language
Layout Managers (Contd.)


                •   The Default layout manager for Window, Frame, and
                    Dialog class is BorderLayout.
                •   In the same way for Panel and Applet, FlowLayout is
                    default layout manager.




     Ver. 1.0                        Session 10                   Slide 11 of 25
Java Programming Language
Layout Managers (Contd.)


                • The FlowLayout manager has the following
                  characteristics:
                   – Forms the default layout for the Panel class
                    –   Adds components from left to right
                    –   Alignment default is centered
                    –   Uses components preferred sizes
                    –   Uses the constructor to tune behavior




     Ver. 1.0                            Session 10                 Slide 12 of 25
Java Programming Language
Layout Managers (Contd.)


                A simple example of FlowLayout:
                 public class LayoutExample
                 {
                 private Frame f;
                 private Button b1;             Declaring the components

                 private Button b2;
                 public LayoutExample()
                 {
                 f = new Frame("GUI example");
                 b1 = new Button("Press Me");                    Initializing the
                                                                 components
                 b2 = new Button("Don’t press Me");
                 }


     Ver. 1.0                        Session 10                          Slide 13 of 25
Java Programming Language
Layout Managers (Contd.)


                public void launchFrame()
                {
                f.setLayout(new FlowLayout());              Setting FlowLayout
                f.add(b1);           Adding components on the Frame
                f.add(b2);
                f.pack();
                f.setVisible(true);
                }
                public static void main(String args[])
                {
                LayoutExample guiWindow = new
                LayoutExample();
                guiWindow.launchFrame();
                }
                }


     Ver. 1.0                     Session 10                        Slide 14 of 25
Java Programming Language
Layout Managers (Contd.)


                The preceding code will show the following output:




     Ver. 1.0                      Session 10                        Slide 15 of 25
Java Programming Language
Layout Managers (Contd.)


                • The BorderLayout manager has following characteristics:
                   – Default layout for the Frame class
                   – Components are added to specific regions
                   – The resizing behavior is as follows:
                         North, South, and Center regions adjust horizontally
                         East, West, and Center regions adjust vertically




     Ver. 1.0                           Session 10                              Slide 16 of 25
Java Programming Language
Layout Managers (Contd.)


                – Using the constructor without any parameters constructs and
                  installs a new BorderLayout with no gaps between the
                  components:
                    setLayout(new BorderLayout());
                – BorderLayout constructor specifiying hgap and vgap can be
                  used to indicate the gaps between components:
                   BorderLayout(int hgap,int vgap);
                – Components must be added to the named regions in
                  BorderLayout manager:
                    f.add(button1,BorderLayout.NORTH);




     Ver. 1.0                     Session 10                         Slide 17 of 25
Java Programming Language
Layout Managers (Contd.)


                • The characteristics of GridLayout manager are:
                     Components are added from left to right, and from top to
                     bottom.
                     All regions are sized equally.
                     The constructor specifies the rows and columns. For example:
                      f.setLayout ( new GridLayout( 3,2));
                      This statement using in Java Program will help to get the
                      following output:




     Ver. 1.0                        Session 10                          Slide 18 of 25
Java Programming Language
Demonstration


               Lets see how to create a Java class that uses the AWT API to
               create a simple GUI front end.




    Ver. 1.0                        Session 10                       Slide 19 of 25
Java Programming Language
Drawing in AWT


               • Graphics class is an abstract class, which is used for
                 drawing graphical figures.
               • Every component has a Graphics object.
               • The Graphics class implements many drawing methods.
               • You can draw in any Component (although AWT provides
                 the Canvas and Panel classes just for this purpose).
               • Create a subclass of Canvas or Panel and override the
                 paint() method.
               • The paint() method is called every time the component is
                 shown (for example, if another window overlapped the
                 component and was then removed).




    Ver. 1.0                       Session 10                     Slide 20 of 25
Java Programming Language
Drawing in AWT (Contd.)


                • Example of paint() method:
                   public void paint( Graphics g )
                   {
                   //display word "today" centred at x,y positions
                   FontMetrics fm =
                   getFontMetrics( g.getFont() );
                   String wording = "today";
                   int xadj = fm.stringWidth( wording ) / 2;
                   //position bottom left corner of the text
                   g.drawString( wording, x-xadj, y );
                   //draw a red line from x1,y1 to x2,y2




     Ver. 1.0                     Session 10                   Slide 21 of 25
Java Programming Language
Drawing in AWT (Contd.)


                g.setColor(Color.red );
                g.drawLine(x1, y1, x2, y2);
                //draw an pre-existing Image.imX, imY is top
                left corner of the Image.
                g.drawImage
                ( image,imX,imY,imWidth,imHeight,this);
                } // end paint




     Ver. 1.0                  Session 10                 Slide 22 of 25
Java Programming Language
Drawing in AWT (Contd.)


                • Various Shapes Drawn by the Graphics Object:




     Ver. 1.0                      Session 10                    Slide 23 of 25
Java Programming Language
Summary


               In this session, you learned that:
                – Abstract Window Toolkit provides GUI components that are
                  used in all Java applets and applications.
                – Window and Panel are subclasses of container.
                – Button, Choice, Label, List, Scrollbar,
                  TextComponent are various components supported by Java
                  programming language.
                – Frame is subclass of Window and they are invisible until
                  setVisible(true)method is not used to expose them.
                – Panel provides space for components and then panel need to
                  be added to a Frame or Window.
                – Layout Managers are provided by Java language to place the
                  components at any required positions.




    Ver. 1.0                       Session 10                        Slide 24 of 25
Java Programming Language
Summary (Contd.)


                 Java programming Language supports following Layout
                 Managers:
                  •   FlowLayout
                  •   BorderLayout
                  •   GridLayout
                  •   CardLayout
                  •   GridBagLayout
               – FlowLayout manager adds the components from left to right
                 and it is default layout for Panel.
               – BorderLayout manager adds the components to specific
                 regions i.e North, South, East, West, and Centre.
               – GridLayout manager adds components from left to right, and
                 from top to bottom. The regions are sized equally as the
                 specified number of rows and columns in the constructor.
               – Graphical figures can be drawn by using Graphics class
                 object.


    Ver. 1.0                     Session 10                        Slide 25 of 25

More Related Content

What's hot

Java session17
Java session17Java session17
Java session17Niit Care
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Javasuraj pandey
 
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...SoftwareSaxony
 
Windbg cmds
Windbg cmdsWindbg cmds
Windbg cmdskewuc
 
08.Abap Dialog Programming Overview
08.Abap Dialog Programming Overview08.Abap Dialog Programming Overview
08.Abap Dialog Programming Overviewsapdocs. info
 
J2 Se 5.0 Name And Version Change
J2 Se 5.0 Name And Version ChangeJ2 Se 5.0 Name And Version Change
J2 Se 5.0 Name And Version Changewhite paper
 
Gui programming a review - mixed content
Gui programming   a review - mixed contentGui programming   a review - mixed content
Gui programming a review - mixed contentYogesh Kumar
 

What's hot (10)

Gui in java
Gui in javaGui in java
Gui in java
 
Java session17
Java session17Java session17
Java session17
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...
Frameworkevolution ohne Nebenwirkung - Automatische Adaption von Clients und ...
 
Windbg cmds
Windbg cmdsWindbg cmds
Windbg cmds
 
XMPPart5
XMPPart5XMPPart5
XMPPart5
 
Confluence
ConfluenceConfluence
Confluence
 
08.Abap Dialog Programming Overview
08.Abap Dialog Programming Overview08.Abap Dialog Programming Overview
08.Abap Dialog Programming Overview
 
J2 Se 5.0 Name And Version Change
J2 Se 5.0 Name And Version ChangeJ2 Se 5.0 Name And Version Change
J2 Se 5.0 Name And Version Change
 
Gui programming a review - mixed content
Gui programming   a review - mixed contentGui programming   a review - mixed content
Gui programming a review - mixed content
 

Similar to Java session10

Similar to Java session10 (20)

JavaYDL12
JavaYDL12JavaYDL12
JavaYDL12
 
12slide
12slide12slide
12slide
 
Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window Toolkit
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdfUNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
 
Swing
SwingSwing
Swing
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
AWT.pptx
AWT.pptxAWT.pptx
AWT.pptx
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
08graphics
08graphics08graphics
08graphics
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, Listeners
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, Listeners
 
Java Swing
Java SwingJava Swing
Java Swing
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 

Java session10

  • 1. Java Programming Language Objectives • In this session, you will learn to: Describe the Abstract Window Toolkit (AWT) package and its components Define the terms containers, components, and layout managers, and describe how they work together to build a GUI Use the Frame and Panel containers appropriately Add components to a container Use various layout managers to achieve a desired dynamic layout Ver. 1.0 Session 10 Slide 1 of 25
  • 2. Java Programming Language Abstract Window Toolkit • Provides GUI components that are used in all Java applets and applications. • Contains classes that can be composed or extended. • Ensures that every GUI component that is displayed on the screen is a subclass of class Component or MenuComponent. • Has Container, which is an abstract subclass of Component and includes two subclasses: Panel Window Ver. 1.0 Session 10 Slide 2 of 25
  • 3. Java Programming Language The java.awt Package Basic overview of AWT package: BorderLayout FlowLayout CardLayout Java.lang.object CheckboxGroup Event Color Font Graphics MenuBar Menu MenuComponent MenuItem CheckboxMenuItem GridLayout GridBagLayout Image Rectangle Component Applet Button Canvas Panel Container Window Dialog Label ScrollPane Frame List TextArea TextComponen TextField t Ver. 1.0 Session 10 Slide 3 of 25
  • 4. Java Programming Language Containers The two main types of containers are: Window 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. • Add components with the add() method. Ver. 1.0 Session 10 Slide 4 of 25
  • 5. Java Programming Language Components Java programming language supports various components: – Button – Choice – Label – List – Scrollbar – TextComponent, etc. • The position and size of a component in a container is determined by a layout manager. • You must use setLocation(), setSize(), or setBounds() on components to locate them in the container. Ver. 1.0 Session 10 Slide 5 of 25
  • 6. Java Programming Language Frames Frames have the following characteristics: – Are a subclass of Window – Have title and resizing corners – Are invisible initially; use setVisible(true) to expose the frame – Have BorderLayout as the default layout manager – Use the setLayout() method to change the default layout manager Ver. 1.0 Session 10 Slide 6 of 25
  • 7. Java Programming Language Frames (Contd.) An example to create a frame: import java.awt.*; public class FrameExample { private Frame f; Declaration of Frame Object public FrameExample() { Initialization f = new Frame("Hello Out There!");} of Frame public void launchFrame() { Object f.setSize(170,170); Setting the size of Frame f.setBackground(Color.blue); f.setVisible(true); } Making Frame visible public static void main(String args[]) { FrameExample guiWindow = new FrameExample(); guiWindow.launchFrame(); } } Ver. 1.0 Session 10 Slide 7 of 25
  • 8. Java Programming Language Panels • Panel provide a space for components. • This enables subpanels to have their own layout manager. • After creating Panel, it must be added to a Window or Frame. Ver. 1.0 Session 10 Slide 8 of 25
  • 9. Java Programming Language Panels (Contd.) The following code snippet helps in creating a small yellow panel and adds it to a frame: public Panel pan; Declaring Panel Object public Frame f; f=new Frame( “I’m with panel”); pan = new Panel(); Initializing Panel Object public void launchFrame() { f.setSize(200,200); f.setLayout(null); // Use default layout pan.setSize(100,100); Setting the size of Panel pan.setBackground(Color.yellow); Giving the f.add(pan); yellow color to the Panel f.setVisible(true); Adding Panel to the Frame } Ver. 1.0 Session 10 Slide 9 of 25
  • 10. Java Programming Language Layout Managers Layout managers are used to place the components on required positions. The following layout managers are included with the Java programming language: FlowLayout BorderLayout GridLayout CardLayout GridBagLayout Ver. 1.0 Session 10 Slide 10 of 25
  • 11. Java Programming Language Layout Managers (Contd.) • The Default layout manager for Window, Frame, and Dialog class is BorderLayout. • In the same way for Panel and Applet, FlowLayout is default layout manager. Ver. 1.0 Session 10 Slide 11 of 25
  • 12. Java Programming Language Layout Managers (Contd.) • The FlowLayout manager has the following characteristics: – Forms the default layout for the Panel class – Adds components from left to right – Alignment default is centered – Uses components preferred sizes – Uses the constructor to tune behavior Ver. 1.0 Session 10 Slide 12 of 25
  • 13. Java Programming Language Layout Managers (Contd.) A simple example of FlowLayout: public class LayoutExample { private Frame f; private Button b1; Declaring the components private Button b2; public LayoutExample() { f = new Frame("GUI example"); b1 = new Button("Press Me"); Initializing the components b2 = new Button("Don’t press Me"); } Ver. 1.0 Session 10 Slide 13 of 25
  • 14. Java Programming Language Layout Managers (Contd.) public void launchFrame() { f.setLayout(new FlowLayout()); Setting FlowLayout f.add(b1); Adding components on the Frame f.add(b2); f.pack(); f.setVisible(true); } public static void main(String args[]) { LayoutExample guiWindow = new LayoutExample(); guiWindow.launchFrame(); } } Ver. 1.0 Session 10 Slide 14 of 25
  • 15. Java Programming Language Layout Managers (Contd.) The preceding code will show the following output: Ver. 1.0 Session 10 Slide 15 of 25
  • 16. Java Programming Language Layout Managers (Contd.) • The BorderLayout manager has following characteristics: – Default layout for the Frame class – Components are added to specific regions – The resizing behavior is as follows: North, South, and Center regions adjust horizontally East, West, and Center regions adjust vertically Ver. 1.0 Session 10 Slide 16 of 25
  • 17. Java Programming Language Layout Managers (Contd.) – Using the constructor without any parameters constructs and installs a new BorderLayout with no gaps between the components: setLayout(new BorderLayout()); – BorderLayout constructor specifiying hgap and vgap can be used to indicate the gaps between components: BorderLayout(int hgap,int vgap); – Components must be added to the named regions in BorderLayout manager: f.add(button1,BorderLayout.NORTH); Ver. 1.0 Session 10 Slide 17 of 25
  • 18. Java Programming Language Layout Managers (Contd.) • The characteristics of GridLayout manager are: Components are added from left to right, and from top to bottom. All regions are sized equally. The constructor specifies the rows and columns. For example: f.setLayout ( new GridLayout( 3,2)); This statement using in Java Program will help to get the following output: Ver. 1.0 Session 10 Slide 18 of 25
  • 19. Java Programming Language Demonstration Lets see how to create a Java class that uses the AWT API to create a simple GUI front end. Ver. 1.0 Session 10 Slide 19 of 25
  • 20. Java Programming Language Drawing in AWT • Graphics class is an abstract class, which is used for drawing graphical figures. • Every component has a Graphics object. • The Graphics class implements many drawing methods. • You can draw in any Component (although AWT provides the Canvas and Panel classes just for this purpose). • Create a subclass of Canvas or Panel and override the paint() method. • The paint() method is called every time the component is shown (for example, if another window overlapped the component and was then removed). Ver. 1.0 Session 10 Slide 20 of 25
  • 21. Java Programming Language Drawing in AWT (Contd.) • Example of paint() method: public void paint( Graphics g ) { //display word "today" centred at x,y positions FontMetrics fm = getFontMetrics( g.getFont() ); String wording = "today"; int xadj = fm.stringWidth( wording ) / 2; //position bottom left corner of the text g.drawString( wording, x-xadj, y ); //draw a red line from x1,y1 to x2,y2 Ver. 1.0 Session 10 Slide 21 of 25
  • 22. Java Programming Language Drawing in AWT (Contd.) g.setColor(Color.red ); g.drawLine(x1, y1, x2, y2); //draw an pre-existing Image.imX, imY is top left corner of the Image. g.drawImage ( image,imX,imY,imWidth,imHeight,this); } // end paint Ver. 1.0 Session 10 Slide 22 of 25
  • 23. Java Programming Language Drawing in AWT (Contd.) • Various Shapes Drawn by the Graphics Object: Ver. 1.0 Session 10 Slide 23 of 25
  • 24. Java Programming Language Summary In this session, you learned that: – Abstract Window Toolkit provides GUI components that are used in all Java applets and applications. – Window and Panel are subclasses of container. – Button, Choice, Label, List, Scrollbar, TextComponent are various components supported by Java programming language. – Frame is subclass of Window and they are invisible until setVisible(true)method is not used to expose them. – Panel provides space for components and then panel need to be added to a Frame or Window. – Layout Managers are provided by Java language to place the components at any required positions. Ver. 1.0 Session 10 Slide 24 of 25
  • 25. Java Programming Language Summary (Contd.) Java programming Language supports following Layout Managers: • FlowLayout • BorderLayout • GridLayout • CardLayout • GridBagLayout – FlowLayout manager adds the components from left to right and it is default layout for Panel. – BorderLayout manager adds the components to specific regions i.e North, South, East, West, and Centre. – GridLayout manager adds components from left to right, and from top to bottom. The regions are sized equally as the specified number of rows and columns in the constructor. – Graphical figures can be drawn by using Graphics class object. Ver. 1.0 Session 10 Slide 25 of 25