Java Programming LanguageObjectives                • In this session, you will learn to:                       Describe th...
Java Programming LanguageAbstract Window Toolkit                • Provides GUI components that are used in all Java applet...
Java Programming LanguageThe java.awt Package                    Basic overview of AWT package:                           ...
Java Programming LanguageContainers                  The two main types of containers are:                      Window    ...
Java Programming LanguageComponents                 Java programming language supports various components:                ...
Java Programming LanguageFrames               Frames have the following characteristics:                – Are a subclass o...
Java Programming LanguageFrames (Contd.)                An example to create a frame:                import java.awt.*;   ...
Java Programming LanguagePanels                • Panel provide a space for components.                • This enables subpa...
Java Programming LanguagePanels (Contd.)                The following code snippet helps in creating a small yellow       ...
Java Programming LanguageLayout Managers               Layout managers are used to place the components on               r...
Java Programming LanguageLayout Managers (Contd.)                •   The Default layout manager for Window, Frame, and    ...
Java Programming LanguageLayout Managers (Contd.)                • The FlowLayout manager has the following               ...
Java Programming LanguageLayout Managers (Contd.)                A simple example of FlowLayout:                 public cl...
Java Programming LanguageLayout Managers (Contd.)                public void launchFrame()                {               ...
Java Programming LanguageLayout Managers (Contd.)                The preceding code will show the following output:     Ve...
Java Programming LanguageLayout Managers (Contd.)                • The BorderLayout manager has following characteristics:...
Java Programming LanguageLayout Managers (Contd.)                – Using the constructor without any parameters constructs...
Java Programming LanguageLayout Managers (Contd.)                • The characteristics of GridLayout manager are:         ...
Java Programming LanguageDemonstration               Lets see how to create a Java class that uses the AWT API to         ...
Java Programming LanguageDrawing in AWT               • Graphics class is an abstract class, which is used for            ...
Java Programming LanguageDrawing in AWT (Contd.)                • Example of paint() method:                   public void...
Java Programming LanguageDrawing in AWT (Contd.)                g.setColor(Color.red );                g.drawLine(x1, y1, ...
Java Programming LanguageDrawing in AWT (Contd.)                • Various Shapes Drawn by the Graphics Object:     Ver. 1....
Java Programming LanguageSummary               In this session, you learned that:                – Abstract Window Toolkit...
Java Programming LanguageSummary (Contd.)                 Java programming Language supports following Layout             ...
Upcoming SlideShare
Loading in …5
×

Java session10

469 views

Published on

Published in: Technology, Art & Photos
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
469
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Java session10

  1. 1. Java Programming LanguageObjectives • 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. 2. Java Programming LanguageAbstract 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. 3. Java Programming LanguageThe 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. 4. Java Programming LanguageContainers 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. 5. Java Programming LanguageComponents 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. 6. Java Programming LanguageFrames 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. 7. Java Programming LanguageFrames (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. 8. Java Programming LanguagePanels • 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. 9. Java Programming LanguagePanels (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. 10. Java Programming LanguageLayout 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. 11. Java Programming LanguageLayout 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. 12. Java Programming LanguageLayout 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. 13. Java Programming LanguageLayout 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. 14. Java Programming LanguageLayout 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. 15. Java Programming LanguageLayout Managers (Contd.) The preceding code will show the following output: Ver. 1.0 Session 10 Slide 15 of 25
  16. 16. Java Programming LanguageLayout 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. 17. Java Programming LanguageLayout 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. 18. Java Programming LanguageLayout 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. 19. Java Programming LanguageDemonstration 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. 20. Java Programming LanguageDrawing 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. 21. Java Programming LanguageDrawing 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. 22. Java Programming LanguageDrawing 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. 23. Java Programming LanguageDrawing in AWT (Contd.) • Various Shapes Drawn by the Graphics Object: Ver. 1.0 Session 10 Slide 23 of 25
  24. 24. Java Programming LanguageSummary 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. 25. Java Programming LanguageSummary (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

×