Presentation is loading. Please wait.

Presentation is loading. Please wait.

AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program.

Similar presentations


Presentation on theme: "AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program."— Presentation transcript:

1 AWT Package

2 Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program is a set of nested components starting from outermost window all the way down to the smallest UI components. A graphical Java program is a set of nested components starting from outermost window all the way down to the smallest UI components. Java awt package provides following: Java awt package provides following: A full set of UI widgets and other components including windows, menus, buttons, check boxes, text fields, scroll bars, etc. A full set of UI widgets and other components including windows, menus, buttons, check boxes, text fields, scroll bars, etc. Support for UI containers which can contain other embedded containers or UI widgets. Support for UI containers which can contain other embedded containers or UI widgets. An event system for managing system and user events among parts of the AWT. An event system for managing system and user events among parts of the AWT. Mechanisms for laying out components in a way that enable platform independent UI design. Mechanisms for laying out components in a way that enable platform independent UI design.

3 Windows Fundamental Component Canvas Container TextComponentButton Panel WindowTextField AppletFrame Dialog

4 The most commonly used windows are those derived from Panel which used by Applet, and The most commonly used windows are those derived from Panel which used by Applet, and Those derived from Frame which creates a standard window. Those derived from Frame which creates a standard window.

5 Component Abstract class that encapsulates all of the attributes of a visual component. Abstract class that encapsulates all of the attributes of a visual component. It defines over a hundred public methods that are responsible for managing events such as mouse, keyboard input, positioning and sizing the window and repainting. It defines over a hundred public methods that are responsible for managing events such as mouse, keyboard input, positioning and sizing the window and repainting. A component object is responsible for remembering the current foreground and background colors and currently selected text font. A component object is responsible for remembering the current foreground and background colors and currently selected text font.

6 Container It is a subclass of Component. It is a subclass of Component. A container is responsible for laying out ( i.e. positioning) any components that it containers. A container is responsible for laying out ( i.e. positioning) any components that it containers. It does this through the use of the various layout managers. It does this through the use of the various layout managers.

7 Panel It is a concrete subclass of Container. It is a concrete subclass of Container. It doesn’t add any new methods. It doesn’t add any new methods. It simply implements Container. It simply implements Container. A Panel may be thought of as a recursively nestable, concrete screen component. A Panel may be thought of as a recursively nestable, concrete screen component. Panel is the superclass of Applet. Panel is the superclass of Applet. A panel is a window that doesn’t contain a title bar, menu bar or border. A panel is a window that doesn’t contain a title bar, menu bar or border. This is why you don’t see these items when an applet is run inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border. This is why you don’t see these items when an applet is run inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border.

8 Other components can be added to a Panel object by its add() method [ inherited from container ] Other components can be added to a Panel object by its add() method [ inherited from container ] Once these components have been added, they can be positioned and resized manually using the setLocation(), setSize(), or setBounds() methods defined by Component. Once these components have been added, they can be positioned and resized manually using the setLocation(), setSize(), or setBounds() methods defined by Component.

9 Window Creates a top-level window. Creates a top-level window. A top level window is not contained within any other object. A top level window is not contained within any other object. Generally window objects are not created directly instead a subclass of Window called Frame is used. Generally window objects are not created directly instead a subclass of Window called Frame is used.

10 Frame it is a subclass of Window and has a title bar, menu bar, borders and resizing corners. it is a subclass of Window and has a title bar, menu bar, borders and resizing corners.

11 Canvas It encapsulates a blank window which can be drawn upon. It encapsulates a blank window which can be drawn upon. Good for painting images and performing other graphic operations. Good for painting images and performing other graphic operations.

12 Basic UI Components Labels Labels Buttons Buttons Check boxes Check boxes Radio buttons Radio buttons Choice menus / list Choice menus / list Text fields Text fields Text area Text area Scrolling list Scrolling list scrollbars scrollbars

13 Basic procedure to create these UI Basic procedure to create these UI Create the component Create the component Add it to panel or applet Add it to panel or applet

14 Labels Constructors Constructors Label()creates an empty label Label()creates an empty label Label(String)creates a label with the given text string aligned left Label(String)creates a label with the given text string aligned left Label(String,int)creates a label with the given string and int alignment i.e. Label(String,int)creates a label with the given string and int alignment i.e. Label.RIGHT Label.RIGHT Label.LEFT Label.LEFT Label.CENTER Label.CENTER

15 Lable’s font can be changed by setFont() method of label class. Lable’s font can be changed by setFont() method of label class. Label lblName = new Label(“Label Name”); add(lblName);

16 import java.awt.*; public class LabelTest extends java.applet.Applet{ LabelTest public void init(){ setFont(new Font(“Helvetica”,Font.BOLD,14)); Label lblLeft=new Label(“LEFT”,Label.LEFT); add(lblLeft); Label lblRight=new Label(“RIGHT”,Label.RIGHT); add(lblRight); } } //============================= Applet Applet Applet <body>

17 Methods of Label getText() getText() Returns a string containing this label’s text Returns a string containing this label’s text setText(String) setText(String) Changes the text of the label Changes the text of the label getAlignment() getAlignment() Returns an integer 0=Label.LEFT 1=Label.CENTER 2=Label.RIGHT Returns an integer 0=Label.LEFT 1=Label.CENTER 2=Label.RIGHT setAlignment(int) setAlignment(int) Changes the alignment of the label. Changes the alignment of the label.

18 Buttons Constructors Constructors Button()button with no label Button()button with no label Button(String)button with label Button(String)button with label add() used to add button on applet. add() used to add button on applet. getLabel() getLabel() returns a label of button returns a label of button setLabel(String) setLabel(String) add or change the label of button add or change the label of button

19 Check Boxes It has two states – on (checked/selected/true) or off (unchecked/unselected/false) It has two states – on (checked/selected/true) or off (unchecked/unselected/false) Used in 2 ways Used in 2 ways Exclusive Exclusive Given a series of checkboxes, only 1 check box can be selected at a time Given a series of checkboxes, only 1 check box can be selected at a time Nonexclusive Nonexclusive Given a series of check boxes any of them can be selected. Given a series of check boxes any of them can be selected.

20 Nonexclusive check boxes can be created using the “Checkbox” class. Nonexclusive check boxes can be created using the “Checkbox” class. Exclusive check boxes are created by first creating a Radio Group and then assigning check boxes to the Radio Group. Exclusive check boxes are created by first creating a Radio Group and then assigning check boxes to the Radio Group.

21 Constructors Constructors Checkbox()empty checkbox, unselected Checkbox()empty checkbox, unselected Checkbox(String)checkbox with label Checkbox(String)checkbox with label Checkbox(String,boolean) checkbox with label and selected / deselected depending upon true / false. Checkbox(String,boolean) checkbox with label and selected / deselected depending upon true / false. Checkbox(String,null,boolean) Checkbox(String,null,boolean) getLabel()returns a string containing the check box label getLabel()returns a string containing the check box label setLabel(String)Changes checkbox label setLabel(String)Changes checkbox label getState()returns true / false based on whether checkbox is selected getState()returns true / false based on whether checkbox is selected setState(boolean)changes the state of checkbox setState(boolean)changes the state of checkbox

22 public class CheckboxTest extends Applet{ public void init(){ Checkbox chkShoe=new Checkbox(“Shoe”); add(chkShoe); Checkbox chkSock=new Checkbox(“Sock”,true); add(chkSock);}}

23 Radio Buttons It is hollow round It is hollow round To create a series of radio buttons an instance of CheckboxGroup must first be created. To create a series of radio buttons an instance of CheckboxGroup must first be created. CheckboxGroup chkgrp=new CheckboxGroup(); CheckboxGroup chkgrp=new CheckboxGroup(); After this, create and add individual radio buttons from the checkbox class as follows- After this, create and add individual radio buttons from the checkbox class as follows- add( newCheckbox( String,CheckboxGroup,boolean)); add( newCheckbox( String,CheckboxGroup,boolean)); getCheckboxGroup() getCheckboxGroup() to access the group to access the group setCheckboxGroup() setCheckboxGroup() to change the group to change the group getSelectedCheckbox() getSelectedCheckbox() gets the selected checkbox gets the selected checkbox setSelectedCheckbox(Checkbox) setSelectedCheckbox(Checkbox) sets the given checkbox as selected sets the given checkbox as selected getCurrent() and setCurrent() can be used to get and set the currently selected check box. getCurrent() and setCurrent() can be used to get and set the currently selected check box.

24 Choice Menus / Lists Pull down list of items from which one item can be selected at a time. Pull down list of items from which one item can be selected at a time. Choice ch = new Choice(); ch.add(“apples”);ch.add(“oranges”);ch.add(“bananas”);add(ch);

25 Methods with Choice object Methods with Choice object getItem(int) getItem(int) Returns the string item at the given place; begins at 0. Returns the string item at the given place; begins at 0. getSelectedIndex() getSelectedIndex() Returns index position of selected item. Returns index position of selected item. getSelectedItem() getSelectedItem() Returns currently selected item as a string Returns currently selected item as a string select(int) select(int) Selects the item at given position Selects the item at given position select(String) select(String) Selects the item with the given string. Selects the item with the given string.

26 TextFields Constructors Constructors TextField() TextField() Empty text field which can be resized by the current layout manager. Empty text field which can be resized by the current layout manager. TextField(int) TextField(int) Empty text field with “int” width Empty text field with “int” width TextField(String) TextField(String) Text field with initialized string Text field with initialized string TextField(String, int) TextField(String, int)

27 getText() getText() returns text that the text field contains returns text that the text field contains setText(String) setText(String) Puts the string into the field Puts the string into the field setColumns() setColumns() Sets width Sets width select(int,int) select(int,int) Select the text between 2 integer positions (starting from 0) Select the text between 2 integer positions (starting from 0) selectAll() selectAll() Select all text Select all text isEditable() isEditable() Returns true / false whether text is editable Returns true / false whether text is editable setEditable(boolean) setEditable(boolean) True(default) to edit, false to freeze text True(default) to edit, false to freeze text getEchoChar(char) getEchoChar(char) Returns the character used for masking input Returns the character used for masking input setEchoChar(char) setEchoChar(char) Set the character used for masking input Set the character used for masking input echoCharIsSet() echoCharIsSet() Returns true / false whether field has echo masking character set. Returns true / false whether field has echo masking character set.

28 Text Area Constructors: Constructors: TextArea()creates an empty text area TextArea()creates an empty text area TextArea(int no_of_lines,int width) TextArea(int no_of_lines,int width) TextArea(String) TextArea(String) TextArea(String,int,int) TextArea(String,int,int) String letter=“ad,adksdskj akdkl \n” + “sndsakjd andkajsd jsdkjkdnkjskddsk jsdk \n”+ “asdj hdbjsa jhbsdjbh hd”; TextArea ta=new TextArea(letter,10,45); add(ta);

29 setText, getText, setEditable, isEditable can be used with TextArea also. setText, getText, setEditable, isEditable can be used with TextArea also. insertText(String, int) insertText(String, int) Inserts the string at the character index indicated by the integer. Inserts the string at the character index indicated by the integer. replaceText(String,int,int) replaceText(String,int,int) Replaces the text between the given integer position with the indicated string. Replaces the text between the given integer position with the indicated string.

30 Scrolling Lists More than one item can be selected at a time More than one item can be selected at a time Multiple items are displayed Multiple items are displayed Scroll bars can be used. Scroll bars can be used. Constructors: Constructors: List()empty scrolling list from which only one item is selected at a time. List()empty scrolling list from which only one item is selected at a time. List(int,boolean)scrolling list indicating no of items visible on list. Boolean shows whether multiple items can be selected or not. List(int,boolean)scrolling list indicating no of items visible on list. Boolean shows whether multiple items can be selected or not. add() is used. add() is used.

31 List lt=new List(3,true); lt.add(“MCA”);lt.add(“MBA”);lt.add(“BCA”);lt.add(“BBA”);add(lt);

32 addItem() to add item to list. addItem() to add item to list. getItem, countItems, getSelectedIndex, getSelectedItem, select work same as in chioce list. getItem, countItems, getSelectedIndex, getSelectedItem, select work same as in chioce list. getSelectedIndexes() getSelectedIndexes() Returns array of integers containing index position of each selected item. Returns array of integers containing index position of each selected item. getSelectedItems() getSelectedItems() Returns array of strings containing text of each selected item. Returns array of strings containing text of each selected item.

33 Scrollbars Constructors: Constructors: Scrollbar() Scrollbar() Vertical scroll bar with max and min value as 0. Vertical scroll bar with max and min value as 0. Scrollbar(int) Scrollbar(int) Same as above. “int” Scrollbar.HORIZONTAL, Scrollbar.VERTICAL Same as above. “int” Scrollbar.HORIZONTAL, Scrollbar.VERTICAL Scrollbar(int,int,int,int,int) Scrollbar(int,int,int,int,int) Orientation horizontal or vertical Orientation horizontal or vertical Initial value in between of max and min value Initial value in between of max and min value Overall width or height of the box Overall width or height of the box Min value Min value Max value Max value

34 getValue() getValue() Returns scroll bars current value Returns scroll bars current value setValue(int) setValue(int) Sets the current value for the scrollbar. Sets the current value for the scrollbar.

35 Working with Panels Panel class is subclass of Container. It simply implements Container. Panel class is subclass of Container. It simply implements Container. Panel is super class of Applet. Panel is super class of Applet. In essence, panel is a window that does not contain title bar, menu bar, border. In essence, panel is a window that does not contain title bar, menu bar, border. PanelApplet.java PanelApplet.java PanelApplet.java

36 Working with Frames Frame is used to create child windows within applets and top-level or child windows for application. Frame is used to create child windows within applets and top-level or child windows for application. Constructors: Constructors: Frame()window without title Frame()window without title Frame(String title)window with title Frame(String title)window with title After creation give size to window After creation give size to window Example Example Example

37 Methods of Frame dispose() to close frame window dispose() to close frame window getTitle() to get the title of frame window getTitle() to get the title of frame window isRsizeable() it takes a boolean value isRsizeable() it takes a boolean value setTitle(String str) setTitle(String str) FrameTest FrameTest FrameTest

38 Setting the window’s dimension void setSize(int newWidth, int newHeight) void setSize(Dimension newSize) The new size of the window is specified by newWidth and newHeight, or by the width and height fields of the Dimension object passed in newSize. The dimensions are specified in terms of pixels.

39 The getSize( ) method is used to obtain the current size of a window. Dimension getSize( ) This method returns the current size of the window contained within the width and height fields of a Dimension object.

40 Hiding and Showing a Window After a frame window has been created, it will not be visible until you call setVisible( ). void setVisible(boolean visibleFlag) The component is visible if the argument to this method is true. Otherwise, it is hidden. You can change the title in a frame window using setTitle( ) – void setTitle(String newTitle)

41 Closing a Frame Window When using a frame window, your program must remove that window from the screen when it is closed, by calling setVisible(false). To intercept a window-close event, you must implement the windowClosing( ) method of the WindowListener interface. Inside windowClosing( ), you must remove the window from the screen.


Download ppt "AWT Package. Java GUI classes are contained in the java.awt package. Java GUI classes are contained in the java.awt package. A graphical Java program."

Similar presentations


Ads by Google