Download presentation
Published byAlvin Webster Modified over 9 years ago
1
Canvas Button Event Check Box Choice Label Object Component List Scrollbar CheckboxGroup Text Field Textcomponent Container Text Area Window Frame Panel File Dialog Dialog
2
All the Components of the AWT can be organized into 3 Categories
Visual Components : Like Button, Text Field, Checkbox and List Container Components : Like Frame, Applet, Dialog and Panel Menu Components : Like Menu Items, Checkbox menu items and Separators
3
Containers Container is a class which contain components, on which layout managers act. Panel Applet Object Component Container Frame Window Dialog FileDialog
4
Container Hierarchy Panel : Difference between Window and Panel is that Panels do not have a graphic representation, not even an outline. Applet is a subclass of Panel. Panels work always inside Windows or Applets. It must be added to some other container. Panel is a container that has no visible boundaries and cannot handle events and is used to hold components collectively as a group. Frame : is a top level window in which components are placed. Normally to create a Java window we extend the Frame class. Dialog : is used for implementing direct application to user feedback or questions. Typical uses include pop-warning dialog windows, quit confirmation dialog boxes, etc.. The dialog class is not a self contained component FileDialog : is used to do file operations like Open / Save / SaveAs and also we can create directories.
5
LABEL A Label displays a string on the screen. Labels are used to display the information what user has to do with the components (like “Enter your name” before a text field). BUTTON Button is an important component on the screen with which the user can trigger an action.
6
Checkbox A checkbox has two states on and of .A checkbox is generally used where user has to choose between two options like male and female. A checkbox is associated with a label indicating for what checkbox is meant. CheckboxGroup It is possible to make the group of checkboxes, so that the user can choose only one out of many choices.
7
Textfield allows only single line of entry by the user.
Textfield is used for user input and button is for generating action as per the user input. Textfield allows only single line of entry by the user. TextField and TextArea are the sub classes of TextComponent. TextArea With TextArea user can enter multiple lines of text,TextArea includes scroll bars so that large amounts of Data can be entered are displayed.
8
Choice A Choice object is used to present the user with a pop-up list of options, from which user can select exactly one . Choice is preferred in place of radio buttons , when the options are many. List List displays the items like choice, it is used to display much longer list of items. List allows user to choose multiple selection and if set to false , allows single selection.
9
AWT provides canvas for drawing. Canvas receives mouse events.
A canvas has got its own graphics context. Scrollbar The scrollbar component is an up-down or left –right slider that can be used to set a numeric value.
10
Event Handling An event occurs when a user clicks on a button or a mouse click or when a scroll bar is adjusted etc. An event listener is an object to which a component has delegated the task of handling a particular kind of event. A listener must implement the interface that contains the event-handling method
11
Java.util.EventObject Java.util.Event ActionEvent AdjustmentEvent
Item Event TextEvent ComponentEvent ContainerEvent FocusEvent pointEvent WindowEvent InutEvent KeyEvent MouseEvent
12
Listener Type Act that results in the event ActionListener User clicks a button, press Return while typing in a text field or choosing a menu item WindowListener User class a frame ( main window ) MouseListener User presses a mouse button while the cursor is on a component MouseMotionListener User moves the mouse over a component ComponentListener Component becomes visible FocusListener Components gets the keyboard focus
13
actionPerformed(ActionEvent) AdjustmentListener
INTERFACE INTERFACE METHODS ADD METHOD ActionListener actionPerformed(ActionEvent) addActionListener(ActionListener) AdjustmentListener adjustmentValueChanged(AdjustmentEvent) addAdjustmentListener(AdjustmentListener) ComponentListener componentHidden(ComponentEvent) componentMoved(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) addComponentListener(ComponentListener) ContainerListener componentAdded(ContainerEvent) componentRemoved(ContainerEvent) addContainerListener(ContainerListener) FocusListener focusGained(FocusEvent) focusLost(FocusEvent) addFocusListener(FocusListener) ItemListener itemStateChanged(ItemEvent) addItemListener(ItemListener)
14
All these methods return void.
KeyListener keyPressed(KeyEvent) addKeyListener(KeyListener) MouseListener mouseClicked(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mousePressed(MouseEvent) mouseReleased(MouseEvent) addMouseListener(MouseListener) MouseMotionListener mouseDragged(MouseEvent) mouseMoved(MouseEvent) addMouseMotionListener(MouseMotionListener) WindowListener windowActivated(WindowEvent) windowDeactivated(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowOpened(WindowEvent) windowClosed(WindowEvent) windowClosing(WindowEvent) addWindowListener(WindowListener) All these methods return void.
15
ActionListener ItemListener AdjustmentListener TextListener Button
TextField MenuItem ListItem ItemListener Choice Checkbox CheckboxMenuItem AdjustmentListener Scrollbar TextListener Textfield TextArea
16
ComponentListener FocusListener MouseListener MouseMotionListener
KeyListener For all Components derived from Component class ContainerListener For all classes derived from Container class WindowListener For all classes derived from Window class
17
Layout Managers FlowLayout BorderLayout Object GridLayout CardLayout
GridBagLayout
18
This is default layout manager for Applets and Panels
FlowLayout This is default layout manager for Applets and Panels Components are arranged from left to right and then overflowing to the next line when the first line is filled.
19
This is the default layout manager for the Frame.
BorderLayout This is the default layout manager for the Frame. It allows the components to be laid out along top, bottom, left or right edges. Any unassigned areas will be recovered by the central component.
20
GridLayout Components are arranged in a grid fashion.
This layout manager breaks up the container into a series of equally sized grid squares.. Each grid square holds exactly one component. The size of all the grid squares defaults to the size of the largest component.
21
CardLayout This layout works on the concept of keeping a number of cards, one above the other, but the top one only being visible. This layout style is often used to show one page among a number of pages like a worksheet in a MS-Excel workbook. The desired worksheet can be made visible by clicking over the tabs. This is useful for instances where a “slide show” effect needs to be produced.
22
CardLayout CardLayout() first(Panel p) last(Panel p) next(Panel p) previous(Panel p) showPanel(Panel p)
23
Create a Panel for which the layout is CardLayout
Create sub panels and add the components individually to each of these sub panels ( Layout of these sub panels can be anything ) Add these sub panels to the panel for which the layout is CardLayout Add this main panel to the actual container.
24
GridBagLayout It supports components of different sizes, and we can specify layout options for each component. It supports a rectangular grid of cells. Each component in a GridBagLayout can occupy one or more cells.
25
Component void setSize(int w, int h) void setSize(Dimension d)
Dimension getSize() void setFont(Font f) Font getFont() void setBackground(Color obj) Color getBackground() void setForeground(Color obj) Color getForeground() void invalidate() void validate() void repaint()
26
void update(Graphics g) void paint(Graphics g) void requestFocus()
Graphics getGraphics() void addComponentListener(ComponentListener obj) void removeComponentListener(ComponentListener obj) void addMouseListener(MouseListener obj) void removeMouseListener(MouseListener obj) void addKeyListener(KeyListener obj) void removeKeyListener(KeyListener obj) void addMouseMotionListener(MouseMotionListener obj) void removeMouseMotionListener(MouseMotionListener obj) void addFocusListener(FocusListener obj) void removeFocusListener(FocusListener obj) void setEnabled(Boolean) Boolean isEnabled() void setVisible(Boolean) Boolean inVisible()
27
Container void add(Component c) void add(String s, Component c)
void add(Component c, ObjectConstraints) void remove(Component c) void remove(int index) void addContainerListener(ContainerListener obj) void removeContainerListener(ContainerListener obj) void setLayout(LayoutManager obj) LayoutManager getLayout()
28
Button Button() Button(String label) setLabel(String s)
String getLabel() void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj)
29
Window Frame Frame() Frame(String title) Void setMenuBar(MenuBar mb)
void addWindowListener(WindowListener obj) void removeWindowListener(WindowListener obj) dispose() void setTitle(String s) String getTitle() Frame Frame() Frame(String title) Void setMenuBar(MenuBar mb) Void setTitle(String s) String getTitle()
30
TextComponent Void setText(String text) String getText()
Void setSelectionStart(int index) int getSelectionStart() Void setSelectionEnd(int index) int getSelectionEnd() Void setCaretPosition(int pos) int getCaretPosition() String getSelectedText() Void addTextListener(TextListener obj) Void removeTextListener(TextListener obj)
31
TextField TextField() TextField(int cols) TextField(String text)
TextField(String text, int cols) void setEchoChar(Char ch) Char getEchoChar() void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj)
32
TextArea TextArea() TextArea(int rows, int cols) TextArea(String text)
TextArea(String text, int rows, int cols) TextArea(String text, int rows, int cols, int Scrollbars) Void append(String str) Void insert(String str, int pos) Void replaceRange(String str, int start, int end)
33
Checkbox Checkbox() Checkbox(String label)
Checkbox(String label, Boolean state) Checkbox(String label, CheckboxGroup x, Boolean state) void addItemListener(Itemlistener obj) void removeItemListener(Itemlistener obj) void setLabel(String label) String getLabel() void setCheckboxGroup(CheckboxGroup cbg) CheckboxGroup getCheckboxGroup() void setState(Boolean state) Boolean getState()
34
MenuBar Menu MenuBar() void add(Menu m) void remove(MenuComponent mc)
void remove(int index) int getMenu(Component () Menu Menu(String item) void add(MenuItem m) void remove(MenuComponent mc) void remove(int index) int getItem(Count c)
35
MenuItem CheckboxMenuItem MenuItem(String name)
MenuItem(String name,MenuShortcut obj) void addActionListener(ActionListener obj) void removeActionListener(ActionListener obj) void setShortcut(MenuShortcut obj) MenuShortcut getShortcut() CheckboxMenuItem CheckboxMenuItem(String item) void addItemListener(ItemListener obj) void removeItemListener(ItemListener obj)
36
Applets In JAVA, we can develop two types of applications. Applets
Applets are small applications that are accessed on an internet server transported over the Net, automatically installed and runs as part of a web document. Applets are not executed by the console-based java runtime interpreter. They are executed by either a java compatible web browser or an appletviewer. Thus execution of an applet does not begin at main method.
37
appletviewer Appletviewer is a standalone program that we can use to test our applets. This comes freely along with sun’s JDK. Example: >Appletviewer firstApplet.html Note: Every applet program must be extending Applet class
38
Hierarchy Object Component Container Panel Applets
39
Applet Life Cycle An Applet life cycle is a cycle which stars with the opening of an HTML file or a web page in which the applet is loaded. The class Applet has four methods which constitute the applet life cycle. These four methods are also called milestones. public void init() public void start() public void stop() public void destroy()
40
init() to initialize the applet each time it’s loaded ( or reloaded). This is called only once in the life time of an applet. Start() to start the applet’s execution, such as when the applet’s loaded or when the user revisits a page that contains the applet. Stop() To stop the applet’s execution, such as when the user leaves the applet’s page or quits the browser. Destroy() To perform a final cleanup in preparation for unloading.
41
In the life time of an applet all the 4 methods will get call whether or not you override them. If you have overridden, the overridden method is called or else the function with default functionality coming from the base class is called. Whenever browser comes across the applet tag, it downloads those bytecodes and converts them into local language and then instantiates the applet class. An applet program must be always public, so as to make it available to the JVM of the browser.
42
Security restriction on an Applet
An applet program can not access the local file system. It can’t ordinarily read or write files on the host that’s executing it. It can’t establish server connection with any machine other than the one from where it has been downloaded. It can’t start any program on the host that’s executing it. It can’t read certain system properties. Every browser has a security manager object that implements its security policies. When a security manager detects a violation it throws a security exception.
43
Calling applets To see our applets on the browser we need create a separate HTML file. After compiling our applet program we should include the applet.class file in the HTML file. Then we have to call the HTML file which contains our applet.class file to call to the applet in the browser.
44
Graphics The color of Foreground and Background.
Graphics classes are defined in java.awt package. With Graphic classes we can manipulate The color of Foreground and Background. The font size, name and type of the characters can be dictated. The Polygon class helps us in drawing different geometrical figures like squares, rectangles, triangles. To draw graphics on the screen the co-ordinates where the graphics are to be displayed should be mentioned in the statements. JAVA’s co-ordinate system supports (x,y) co-ordinate system.
45
Graphics class is an abstract class, we can’t instantiate the class. The component class paint method ( public void paint(Graphics g) ) takes a graphics object as an argument. Paint method should be overridden in the source code. The paint method is called by the programmer.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.