Upload
everett-hensley
View
259
Download
3
Tags:
Embed Size (px)
Citation preview
AWT PackageAWT 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 is a set of nested components A graphical Java program is a set of nested components
starting from outermost window all the way down to the starting from outermost window all the way down to the smallest UI components.smallest UI components.
Java awt package provides following:Java awt package provides following: A full set of UI widgets and other components including windows, A full set of UI widgets and other components including windows,
menus, buttons, check boxes, text fields, scroll bars, etc.menus, buttons, check boxes, text fields, scroll bars, etc. Support for UI containers which can contain other embedded Support for UI containers which can contain other embedded
containers or UI widgets.containers or UI widgets. An event system for managing system and user events among parts of An event system for managing system and user events among parts of
the AWT.the AWT. Mechanisms for laying out components in a way that enable platform Mechanisms for laying out components in a way that enable platform
independent UI design.independent UI design.
Windows FundamentalWindows FundamentalComponent
Canvas Container TextComponent Button
Panel Window TextField
Applet Frame Dialog
The most commonly used windows are those The most commonly used windows are those derived from Panel which used by Applet, andderived from Panel which used by Applet, and
Those derived from Frame which creates a Those derived from Frame which creates a standard window.standard window.
ComponentComponent
Abstract class that encapsulates all of the attributes of Abstract class that encapsulates all of the attributes of a visual component.a visual component.
It defines over a hundred public methods that are It defines over a hundred public methods that are responsible for managing events such as mouse, responsible for managing events such as mouse, keyboard input, positioning and sizing the window keyboard input, positioning and sizing the window and repainting.and repainting.
A component object is responsible for remembering A component object is responsible for remembering the current foreground and background colors and the current foreground and background colors and currently selected text font.currently selected text font.
ContainerContainer
It is a subclass of Component.It is a subclass of Component. A container is responsible for laying out ( i.e. A container is responsible for laying out ( i.e.
positioning) any components that it containers.positioning) any components that it containers. It does this through the use of the various It does this through the use of the various
layout managers.layout managers.
PanelPanel 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 A Panel may be thought of as a recursively nestable, concrete
screen component.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 A panel is a window that doesn’t contain a title bar, menu bar
or border.or border. This is why you don’t see these items when an applet is run 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 inside a browser. When you run an applet using an applet viewer, the applet viewer provides the title and border.viewer, the applet viewer provides the title and border.
Other components can be added to a Panel Other components can be added to a Panel object by its add() method [ inherited from object by its add() method [ inherited from container ]container ]
Once these components have been added, they Once these components have been added, they can be positioned and resized manually using can be positioned and resized manually using the setLocation(), setSize(), or setBounds() the setLocation(), setSize(), or setBounds() methods defined by Component.methods defined by Component.
WindowWindow
Creates a top-level window.Creates a top-level window. A top level window is not contained within A top level window is not contained within
any other object.any other object. Generally window objects are not created Generally window objects are not created
directly instead a subclass of Window called directly instead a subclass of Window called Frame is used.Frame is used.
FrameFrame
it is a subclass of Window and has a title bar, it is a subclass of Window and has a title bar, menu bar, borders and resizing corners.menu bar, borders and resizing corners.
CanvasCanvas
It encapsulates a blank window which can be It encapsulates a blank window which can be drawn upon.drawn upon.
Good for painting images and performing Good for painting images and performing other graphic operations.other graphic operations.
Basic UI ComponentsBasic UI Components
LabelsLabels ButtonsButtons Check boxesCheck boxes Radio buttonsRadio buttons Choice menus / listChoice menus / list Text fieldsText fields Text areaText area Scrolling listScrolling list scrollbarsscrollbars
Basic procedure to create these UIBasic procedure to create these UI Create the componentCreate the component Add it to panel or appletAdd it to panel or applet
LabelsLabels
ConstructorsConstructors Label()Label() creates an empty labelcreates an empty label Label(String)Label(String) creates a label with the creates a label with the
given text string aligned leftgiven text string aligned left Label(String,int)Label(String,int) creates a label with the given creates a label with the given
string and int alignment i.e.string and int alignment i.e. Label.RIGHTLabel.RIGHT Label.LEFTLabel.LEFT Label.CENTERLabel.CENTER
Lable’s font can be changed by setFont() method of Lable’s font can be changed by setFont() method of label class.label class.
Label lblName = new Label(“Label Name”);Label lblName = new Label(“Label Name”);
add(lblName);add(lblName);
import java.awt.*;import java.awt.*;public class public class LabelTestLabelTest extends java.applet.Applet{ extends java.applet.Applet{
public void init(){public void init(){setFont(new setFont(new
Font(“Helvetica”,Font.BOLD,14));Font(“Helvetica”,Font.BOLD,14));Label lblLeft=new Label(“LEFT”,Label.LEFT);Label lblLeft=new Label(“LEFT”,Label.LEFT);add(lblLeft);add(lblLeft);Label lblRight=new Label(“RIGHT”,Label.RIGHT);Label lblRight=new Label(“RIGHT”,Label.RIGHT);add(lblRight); } }add(lblRight); } }//=============================//=============================<html><head><title><html><head><title>AppletApplet</title></head></title></head><body><body><applet code=“LabelTest.class” width=300 <applet code=“LabelTest.class” width=300
height=200> </applet></body></html>height=200> </applet></body></html>
Methods of LabelMethods of Label
getText()getText() Returns a string containing this label’s textReturns a string containing this label’s text
setText(String)setText(String) Changes the text of the labelChanges the text of the label
getAlignment()getAlignment() Returns an integer 0=Label.LEFT Returns an integer 0=Label.LEFT
1=Label.CENTER 2=Label.RIGHT1=Label.CENTER 2=Label.RIGHT setAlignment(int)setAlignment(int)
Changes the alignment of the label.Changes the alignment of the label.
ButtonsButtons
ConstructorsConstructors Button()Button() button with no labelbutton with no label Button(String)Button(String) button with labelbutton with label
add() used to add button on applet.add() used to add button on applet. getLabel()getLabel()
returns a label of buttonreturns a label of button setLabel(String)setLabel(String)
add or change the label of buttonadd or change the label of button
Check BoxesCheck Boxes
It has two states – on (checked/selected/true) or off It has two states – on (checked/selected/true) or off (unchecked/unselected/false)(unchecked/unselected/false)
Used in 2 waysUsed in 2 ways ExclusiveExclusive Given a series of checkboxes, only 1 check box can Given a series of checkboxes, only 1 check box can
be selected at a timebe selected at a time NonexclusiveNonexclusive Given a series of check boxes any of them can be Given a series of check boxes any of them can be
selected.selected.
Nonexclusive check boxes can be created Nonexclusive check boxes can be created using the “Checkbox” class.using the “Checkbox” class.
Exclusive check boxes are created by first Exclusive check boxes are created by first creating a Radio Group and then assigning creating a Radio Group and then assigning check boxes to the Radio Group.check boxes to the Radio Group.
ConstructorsConstructors Checkbox()Checkbox() empty checkbox, unselectedempty checkbox, unselected Checkbox(String)Checkbox(String) checkbox with labelcheckbox with label Checkbox(String,boolean) checkbox with label and selected / Checkbox(String,boolean) checkbox with label and selected /
deselected depending upon true / false.deselected depending upon true / false. Checkbox(String,null,boolean)Checkbox(String,null,boolean) getLabel()getLabel() returns a string containing the check box labelreturns a string containing the check box label setLabel(String)setLabel(String) Changes checkbox labelChanges checkbox label getState()getState() returns true / false based on whether checkbox returns true / false based on whether checkbox
is selectedis selected setState(boolean)setState(boolean) changes the state of checkboxchanges the state of checkbox
public class CheckboxTest extends Applet{public class CheckboxTest extends Applet{public void init(){public void init(){Checkbox chkShoe=new Checkbox(“Shoe”);Checkbox chkShoe=new Checkbox(“Shoe”);add(chkShoe);add(chkShoe);Checkbox chkSock=new Checkbox chkSock=new
Checkbox(“Sock”,true);Checkbox(“Sock”,true);add(chkSock);add(chkSock);}}}}
Radio ButtonsRadio Buttons It is hollow roundIt 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( add( newCheckbox(
String,CheckboxGroup,boolean));String,CheckboxGroup,boolean)); getCheckboxGroup()getCheckboxGroup()
to access the groupto access the group setCheckboxGroup()setCheckboxGroup()
to change the groupto change the group getSelectedCheckbox()getSelectedCheckbox()
gets the selected checkboxgets the selected checkbox setSelectedCheckbox(Checkbox)setSelectedCheckbox(Checkbox)
sets the given checkbox as selectedsets 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.
Choice Menus / ListsChoice Menus / Lists
Pull down list of items from which one item Pull down list of items from which one item can be selected at a time.can be selected at a time.
Choice ch = new Choice();Choice ch = new Choice();ch.add(“apples”);ch.add(“apples”);ch.add(“oranges”);ch.add(“oranges”);ch.add(“bananas”);ch.add(“bananas”);add(ch);add(ch);
Methods with Choice objectMethods 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 stringReturns currently selected item as a string select(int)select(int)
Selects the item at given positionSelects the item at given position select(String)select(String)
Selects the item with the given string.Selects the item with the given string.
TextFieldsTextFields
ConstructorsConstructors TextField()TextField()
Empty text field which can be resized by the Empty text field which can be resized by the current layout manager.current layout manager.
TextField(int)TextField(int) Empty text field with “int” widthEmpty text field with “int” width
TextField(String)TextField(String) Text field with initialized stringText field with initialized string
TextField(String, int)TextField(String, int)
getText()getText() returns text that the text field containsreturns text that the text field contains
setText(String)setText(String) Puts the string into the fieldPuts the string into the field
setColumns()setColumns() Sets widthSets 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 textSelect all text
isEditable()isEditable() Returns true / false whether text is editableReturns true / false whether text is editable
setEditable(boolean)setEditable(boolean) True(default) to edit , false to freeze textTrue(default) to edit , false to freeze text
getEchoChar(char)getEchoChar(char) Returns the character used for masking inputReturns the character used for masking input
setEchoChar(char)setEchoChar(char) Set the character used for masking inputSet 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.
Text AreaText Area Constructors:Constructors: TextArea()TextArea() creates an empty text areacreates 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” +String letter=“ad,adksdskj akdkl \n” +““sndsakjd andkajsd jsdkjkdnkjskddsk jsdk \n”+sndsakjd andkajsd jsdkjkdnkjskddsk jsdk \n”+““asdj hdbjsa jhbsdjbh hd”;asdj hdbjsa jhbsdjbh hd”;
TextArea ta=new TextArea(letter,10,45);TextArea ta=new TextArea(letter,10,45);add(ta);add(ta);
setText, getText, setEditable, isEditable can be setText, getText, setEditable, isEditable can be used with TextArea also.used with TextArea also.
insertText(String, int)insertText(String, int) Inserts the string at the character index Inserts the string at the character index
indicated by the integer.indicated by the integer. replaceText(String,int,int)replaceText(String,int,int) Replaces the text between the given integer Replaces the text between the given integer
position with the indicated string.position with the indicated string.
Scrolling ListsScrolling Lists
More than one item can be selected at a timeMore than one item can be selected at a time Multiple items are displayedMultiple items are displayed Scroll bars can be used.Scroll bars can be used. Constructors:Constructors: List()List() empty scrolling list from which only one item empty scrolling list from which only one item
is selected at a time.is selected at a time. List(int,boolean)List(int,boolean) scrolling list indicating no of scrolling list indicating no of
items visible on list. Boolean shows whether multiple items visible on list. Boolean shows whether multiple items can be selected or not.items can be selected or not.
add() is used.add() is used.
List lt=new List(3,true);List lt=new List(3,true);
lt.add(“MCA”);lt.add(“MCA”);
lt.add(“MBA”);lt.add(“MBA”);
lt.add(“BCA”);lt.add(“BCA”);
lt.add(“BBA”);lt.add(“BBA”);
add(lt);add(lt);
addItem() to add item to list.addItem() to add item to list. getItem, countItems, getSelectedIndex, getItem, countItems, getSelectedIndex,
getSelectedItem, select work same as in chioce list.getSelectedItem, select work same as in chioce list. getSelectedIndexes()getSelectedIndexes()
Returns array of integers containing index position of each Returns array of integers containing index position of each selected item.selected item.
getSelectedItems()getSelectedItems() Returns array of strings containing text of each selected Returns array of strings containing text of each selected
item.item.
ScrollbarsScrollbars 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, Same as above. “int” Scrollbar.HORIZONTAL,
Scrollbar.VERTICALScrollbar.VERTICAL Scrollbar(int,int,int,int,int)Scrollbar(int,int,int,int,int) Orientation horizontal or verticalOrientation horizontal or vertical Initial value in between of max and min valueInitial value in between of max and min value Overall width or height of the boxOverall width or height of the box Min valueMin value Max valueMax value
getValue()getValue() Returns scroll bars current valueReturns scroll bars current value setValue(int)setValue(int) Sets the current value for the scrollbar.Sets the current value for the scrollbar.
Working with PanelsWorking with Panels
Panel class is subclass of Container. It simply Panel class is subclass of Container. It simply implements Container.implements Container.
Panel is super class of Applet.Panel is super class of Applet. In essence, panel is a window that does not In essence, panel is a window that does not
contain title bar, menu bar, border.contain title bar, menu bar, border. PanelApplet.javaPanelApplet.java
Working with FramesWorking with Frames
Frame is used to create child windows within Frame is used to create child windows within applets and top-level or child windows for applets and top-level or child windows for application.application.
Constructors:Constructors: Frame()Frame() window without titlewindow without title Frame(String title)Frame(String title) window with titlewindow with title After creation give size to windowAfter creation give size to window ExampleExample
Methods of Frame Methods of Frame
dispose() to close frame windowdispose() to close frame window getTitle() to get the title of frame windowgetTitle() to get the title of frame window isRsizeable() it takes a boolean valueisRsizeable() it takes a boolean value setTitle(String str)setTitle(String str) FrameTestFrameTest
Setting the window’s dimensionSetting 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.
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.
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)
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.