Professional Documents
Culture Documents
Syntax Use
write() To write a byte to the Output stream
write(byte b[]) To write all bytes in the array b to the
Output stream.
write(byte b[],int n,int m) To write m bytes from array b starting
from nth byte
close() To close the Output stream.
Syntax Use
read() To read a byte from the Input stream
read(byte b[]) To read an array of b.length bytes into
array b.
read(byte b[],int n,int m) To read m bytes into b starting from nth
close() byte
reset() To close the Input stream.
skip(n) To go back to the beginning of the stream.
II) BufferedWriter :
The functionality of this class is, it writes buffers of characters into
a file.
❖ Some other important subclasses of the Writer class are listed below.
The functionality of this class is, it read one character at a time from a file.
II) BufferedReader :
The functionality of this class is, it read buffers of characters from a file.
❖ Some other important subclasses of the Reader class are listed below.
catch(Exception e)
{
System.out.println(“Exception : “+e);
}
}
}
Output: ABCDEFGHIJKLMNOPQRSTUVWXYZ
DataInputStream Class:
❖ The DataInputStream class extends FilterInputStream class and implements
DataInput interface. The FilterInputStream class extends the InputStream class
Therefore ,the DataInputStream class contains all the methods in InputStream
Srimad Andavan Arts and Science College
Department of Computer Sciecne
Class.
Window Fundamentals
▪ The AWT defines windows according to a class hierarchy.
▪ The two most common windows are,
❖ Panel, which is used by applets.
❖ Frame, which is used to create standard window.
▪ The functionality of these windows is derived from their parent
classes.
The Class Hierarchy for Panel and Frame
Component
▪ Component is an abstract class that encapsulates all of the attributes of a visual
component.
Container
❖ The Container class is a subclass of Component.
❖ It has additional methods that allow other Component objects to be nested within it.
❖ A Container is responsible for laying out (positioning) any component that it contains.
❖ It uses various layout managers to do this work.
Panel
• The Panel class is a concrete subclass of Container.
• It does not add any new methods, but it simply implements Container.
• Panel is the super class for Applet. When screen output is directed to
an applet, it is drawn on the surface of the Panel object.
• Panel is a window that does not contain a title bar, menu bar, or border.
Since you don’t see these items when an applet is run inside a browser.
• Other components can be added to a Panel object by its add () method.
You can position and resizing the components by using setLocation (),
setSize (), or subtends () methods defined by the Component.
Window
• The Window class creates a top-level window. It sits directly on the desktop.
• You can’t create Window objects directly. But you will use a subclass of Window
called Frame.
Frame
❖ It is a subclass of Window. It has a title bar, menu bar, borders, and resizing
corners.
Canvas
❖ This encapsulates a blank window upon which we can draw
Constructors
Methods:
void setLabel (String Str) Sets the button label by Str.
String getLabel () Return the label of the button.
Handling Events:
• Each time a button is pressed, an action event is generated. Each listener implements
the ActionListener interface.
• When events occur the actionPerformed () method will be invoked. An ActionEvent
object is the argument of this method.
• The label is obtained by calling the getActionCommand () method on the ActionEvent
object.
CHECKBOX
This control is used to turn an option on or off.there is a label associated
with each checkbox.
Constructors:
Checkbox() - creates empty checkbox
Checkbox(String s) - creates checkbox with string label
Checkbox(String s,Boolean on) - creates checkbox with string label and selcted/deselcted
option
Checkbox(String s,CheckboxGroup cg,boolean on) - creates checkbox with string label
and selcted/deselcted option with group
Methods
Srimad Andavan Arts and Science College
Department of Computer Sciecne
1. getState() - Retrieves the control
2. setState - Sets the state true or false for a check box.
3. getLabel - Fetches the current label associated with the checkbox
4.setLabel() - Sets the label for a checkbox
Constructor
CheckboxGroup();
Methods
Event
▪ Each time a choice is selected, an item event is generated. This is sent to any
listener. Each listener implements “ItemListener” interface.
1. List()
2. List(int rows)
3. List(int rows,boolean multiple)
1. Here, rows represents the number of items that are visible to a user at a time.
2. When a multiple is true, a user can select two or more items in a list.
❖ An itemEvent Object is generated when a user selects or deselects one of the items
listed in a list control.
❖ An ActionEvent object is generated when a user double-clicks the item.
❖ An ItemListener object can be register to receive the item event by the following
method.
addItemListener(this)
• We can add an item to a list control by using the following method.
add(String s)
• The source that generated an itemEvent can be obtained through the
getItemSelectable().
The ItemListener interface declares the itemStateChanged() method,while the
ActionListener declares the actionPerformed() method.
Events:
LAYOUT MANAGERS
• A layout manager automatically arranges your controls within a window.
• The layout managers are used to organize the controls or components.
• Layout managers are placed inside a Frame or Applet or Panel
To Set Layouts:
• setLayout (null) -Null layout cancels the container’s layout. So users must manually
placed the components by using the setBounds () method.
• setLayout (new LayoutManager) - To set the layout model. If no call to setLayout ()
method, then the default layout manager is used.
▪ setBounds (int left,int top,int width,int height) -Set the components
position and
size.
Ex: - Label L=new Label ();
L.setBounds (100,100,75,25);
FLOWLAYOUT : Default layout for Applets and Panel.
Constructors:
▪ FlowLayout (); Default constructor, which centers components and
leaves 5 pixels of space between each component.
Srimad Andavan Arts and Science College
Department of Computer Sciecne
▪ FlowLayout (Align); Specify how each line is aligned.
▪ FlowLayout (Align, int hoz, int ver); Specify the horizontal and
vertical space between each component.
Align : 1. FlowLayout.LEFT 2. FlowLayout.RIGHT
3.FlowLayout.CENTER
Container class methods:
1. setLayout (new Layout (…)); 2. add (component) 3. remove (component)
BORDERLAYOUT : Default layout for Frames.
Constructors:
❖ BorderLayout (); Set the default border layout.
❖ BorderLayout (int hor, int ver) Specify horizontal and vertical space between
components.
Container class methods: 1. setLayout (new BorderLayout(…));
2. add (Component,Direction);
Direction: 1. BorderLayout.NORTH 2. BorderLayout.SOUTH
3. BorderLayout.EAST 4. BorderLayout.WEST
5. BorderLayout.CENTER
HANDLING EVENTS
Listeners:
• A Listener receives the click event of the button by calling the appropriate listener
methods.
• A listener should implement the appropriate listener interface.
• All of the listener interfaces are specified in the java.awt.event package.
class AwtDemo
{
Frame f;
Label lb;
TextField txt;
TextArea ta;
List l1;
Choice ch;
Scrollbar sc;
Checkbox cb;
AwtDemo()
{
f=new Frame("Thiyagu");
f.setSize(300,300);
f.setLayout(new FlowLayout());
lb=new Label("empnmae");
txt=new TextField(10);
ta=new TextArea(10,10);
l1=new List(5);
l1.add("rajan");
l1.add("mala");
l1.add("ravi");
l1.add("ramu");
l1.add("mani");
l1.add("vani");
Srimad Andavan Arts and Science College
Department of Computer Sciecne
l1.add("manu");
ch=new Choice();
ch.add("rajan");
ch.add("mala");
ch.add("ravi");
ch.add("ramu");
sc=new Scrollbar();
cb=new Checkbox();
f.add(lb);
f.add(txt);
f.add(ta);
f.add(l1);
f.add(ch);
f.add(sc);
f.add(cb);
f.setVisible(true);
}
public static void main(String arg[])
{
new AwtDemo();
}
}
Begin Initialization
(Load Applet)
start()
stop()
Display Stopped
paint()
start()
destroy()
Destroyed End
▪ An applet has a well-defined life cycle, as shown in the figu re.
Applets
java.awt.Component
java.awt.Cotainer
java.awt.Panel
java.applet.Applet
▪ These are the five methods that provide basic mechanism by which the
browser or applet viewer interface to the applet and control its execution.
▪ This skeleton does not do anything, it can be compiled and run.
Syntax of the HTML Applet Tag
<applet
[codebase=codebase-url]
code=”appletname.class”
[alt=alternate-text]
[name=applet-instance-name]
width=pixels
height=pixels
[align=alignment]
[vspace=pixels]
[hspace=pixels]
>
[<param name=name1 value=value1>]
[<param name=name2 value=value2>]
…….
.. …….
</applet>
• From the above applet tag, the attributes shown inside the [ ] brackets are
optional.