Professional Documents
Culture Documents
Kuldeep Yogi
Banasthali Unuversity
Part of the AWT Class Hierarchy
Object
Component
Applet
Dialog Frame
Java Applet
….
</APPLET>
Options:
CODEBASE Designates the base URL
ALIGN Designates the applet alignment choice (Left, Right, Top, Bottom, Middle)
HSPACE Designates the empty space at the right and left of the applet
VSPACE Designates the empty space above and below the applet
g.setColor(new Color(rval,gval,bval));
g.fillRect(i, j, 25, 25);
g.setColor(Color.black);
g.drawRect(i, j, 25, 25);
};
delay(finalDelay);
};
};
}
ColorBoxes.html
<HTML>
<HEAD>
<TITLE>
Colored Boxes
</TITLE>
</HEAD>
<BODY>
<H2>
Event Driven Software Example #3
</H2>
<H2>
Colored Boxes
</H2>
<P>
<APPLET code="ColorBoxes.class" width="600" height="300">
<PARAM name="delay" value="10000000">
</APPLET>
</P>
<P>
<A href="ColorBoxes.java">The Source</A>
</P>
</BODY>
</HTML>
Control Fundamentals
The AWT supports the following types of controls
1. Label
Constructor:-
Label()
Label(String s)
Label(String ,Int how)
Here how may be Label.LEFT,Label.RIGHT,Label.Center
Methods:-
setText(String s)
getText()
setAlignment()
getAlignment()
2. Button
Constructor:-
Button()
Button(String s)
Methods:-
getLabel()
setLabel()
3. Checkbox
Constructor:-
Checkbox();
Checkbox(String s)
Checkbox(String s, boolean on)
Checkbox(String s, CheckboxGroup cgb ,boolean on)
Methods:-
getState()
setState()
getLabel()
setLabel()
4. CheckboxGroup
Constructor:-
CheckboxGroup()
Methods:-
setSeletedCheckbox(Checkbox c)
Checkbox getSelectedCheckbox()
5. Choice
Constructor:-
Choice()
Methods:-
add(String s)
getSelectedItem()
getSelectedIndex()
getItemCount()
void select(int index)
void select(String s)
6. List
Constructor:-
List()
List(int n_row)
List(int r, boolean multiSelect)
Methods:-
add(String s),
add(String s, int index)
getSelectedItem()
int[] getSelectedIndexs()
String[] getSelectedItems()
7. TextField
Constructor:-
TextField()
TextField(int numChars)
TextField(String s)
TextField(String s,int numchars)
Methods:-
getText()
setText(String s)
getSelectedText()
isEditable()
setEditable(boolean b)
setEchoChar(Char c)
echoCharIsSet()
getEchoChar()
Events in Java
In Java most objects within the AWT class hierarchy have the ability to
observe events occurring with respect to itself (e.g. a button can observe that
it has been pressed, or a textbox can detect that it has been written to). The
object can then send this information to any other object that has indicated
it is interested in knowing about such happenings. These notified objects
can then react to these events.
actionPerformed(Act
ButtonPanel
ionEvent)
Button
addActionListen
er(this)
getObjecct() buttonPressed
ActionEvent
8. TextArea
Constructor:-
TextArea()
TextArea(int numLines, int numChars)
TextArea(String s)
TextArea(String s,int numLines, int numchars)
TextArea(String s,int numLines, int numchars,int Bars)
9. MenuBar
To create a menubar first create a frame in which you want to display the menus.
Frame f =new Frame(“Menu”);
MenuBar mbar=new MenuBar();
Menu file=new Menu(“file”);
MenuItem New=new MenuItem(“New”);
CheckBoxMenuItem CB=new CheckBoxMenuItem(“dfd”);
f.setMenuBar()
file.add(New);
file.add(CB);
f.setVisible()
Dialog Box
Constructor
Dialog(Frame f, boolean mode)
Dialog(Frame f, String title,boolean mode)
FileDialog
Constructor
FileDialog(Frame f, String s)
FileDialog(Frame f,String s, int how)
FileDialog(Frame f)
How=FileDialog.LOAD , FileDialog.SAVE
Methods
getFile()
getDirectory()
Events in Java An event source:
can register listener object
can send listeners “event objects”
A listener object:
implements a “listener interface”
can be registered as a listener to various event sources
(ActionEvent actionPerformed)
ButtonPanel Button
addActionListener(this)
getObjecct() buttonPressed
An event object:
ActionEvent
contains information about the event that occurred
can be queried to receive this information
Event Listeners for Mouse and Keyboard Events
MouseListener
public void mouseEntered(MouseEvent e)
public void mouseExited (MouseEvent e)
public void mousePressed (MouseEvent e)
public void mouseReleased (MouseEvent e)
public void mouseClicked (MouseEvent e) Pressed and released at same location
MouseMotionListener
public void mouseMoved (MouseEvent e)
public void mouseDragged (MouseEvent e)
KeyListener
public void keyPressed(KeyEvent e)
public void keyReleased(keyEvent e)
public void keyTyped(keyEvent e)
Keyboard and Mouse Events
ActionListioner Interface
Void actionPerformed(ActionEvent ae)
FocusListener Interface
FocusEvent methods
Void focusGained(FocusEvent fe)
Void focusLost(FocusEvent fe)
ItemListerner Interface
Void itemStateChanged(ItemEven ie)
TextListener Interface
TextEvent Methods
Void textChanged(TextEvent te)
WindowListerner Interface
WindowEvent Methods
Void windowActivated(WindowEvent we)
Void windowClosed(WindowEvent we)
Void windowClosing(WindowEvent we)
Void windowDeactivated(WindowEvent we)
Void windowOpened(WindowEvent we)
Adapter Classes
If we are implementing a particular listener interface, we have to implement all the
methods defined in that interface. which might not actually be used. In order to simplify
things, Java came up with the concept of Adapter classes. Adapter classes provide empty
definitions for all the methods of their corresponding Listener interface.
ComponentAdapter
ContainerAdapter
FocusAdapter
HierarchyBoundsAdapter
KeyAdapter
MouseAdapter
MouseMotionAdapter
WindowAdapter
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.*;
Public class AdapterDemo extends Applet
{
int xcord,ycord;
public void init()
{
addMouseMotionListner(new MouseDemo(this));
}
public void paint(Graphics g)
{
g.drawString(“(“+xcord+”,”+ycord+”)”,xcord,ycord);
}
}
Class MouseDemo extends MouseMotionAdapter
{
AdapterDemo d;
MouseDemo(AdapterDemo d)
{
this.d=d;
}
Public void mouseMoved(MouseEvent me)
{
d.xcord=me.getX(); d.ycord=me.getY(); d.repaint();
}
}
Anonymous Inner Class
It is a class that is not assigned a name and created on the fly. They are created,
instantiated, used and garbage collected when they are done. They are automatically assigned a
name as Outerclassname$1.
Example: The syntax new KeyAdapter() {…….} indicates to the compiler that the
Import java.awt.*;
Code between the braces defines an anonymous inner class. This class
Import java.awt.event.*;
Import java.applet.*;
Extends KeyAdapter .
Public class AnonyKeyListDemo extends Applet
{
public void init()
{
addKeyListener (new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
showStatus(“key pressed”);
}
public void keyReleased(KeyEvent me)
{
showStatus(“KeyReleased”);
}
})
}
}
Testing an Applet
Construct an HTML file that will locate your applet in a region of an html
page.
Run appletviewer with the name of the html file where you call your applet.