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

Gui in java
Gui in javaGui in java
Gui in javaGaurav Raj
Β 
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

Abstract Window Toolkit
Abstract Window ToolkitAbstract Window Toolkit
Abstract Window ToolkitRutvaThakkar1
Β 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1sotlsoc
Β 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
Β 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
Β 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and ComponentsSohanur63
Β 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01Ankit Dubey
Β 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13karan saini
Β 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, ListenersKalai Selvi
Β 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01JONDHLEPOLY
Β 
AWT controls, Listeners
AWT controls, ListenersAWT controls, Listeners
AWT controls, ListenersKalai Selvi
Β 
Java Swing
Java SwingJava Swing
Java SwingShraddha
Β 

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

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 bNiit Care
Β 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 bNiit Care
Β 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 aNiit Care
Β 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 cNiit Care
Β 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 bNiit Care
Β 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 aNiit Care
Β 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 cNiit Care
Β 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 bNiit Care
Β 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 aNiit Care
Β 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 cNiit Care
Β 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 aNiit Care
Β 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 cNiit Care
Β 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-cNiit Care
Β 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-bNiit Care
Β 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-aNiit Care
Β 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-cNiit Care
Β 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-bNiit Care
Β 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-aNiit Care
Β 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 bNiit Care
Β 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 cNiit Care
Β 

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

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
Β 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
Β 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
Β 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
Β 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
Β 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Β 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
Β 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
Β 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
Β 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
Β 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
Β 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
Β 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
Β 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
Β 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
Β 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
Β 

Recently uploaded (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Β 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
Β 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Β 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
Β 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Β 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
Β 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Β 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Β 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
Β 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
Β 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
Β 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Β 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
Β 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Β 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
Β 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Β 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Β 

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