Upload
vijay-rijhwani
View
160
Download
2
Tags:
Embed Size (px)
Citation preview
APPLET
•Applet Component •Layout Manager•Event Handling• Adapter Class
Applet Component
COMPONENT
Container Labels Button CheckBox CheckBoxGroup Choice Lists ScrollBar TextField TextArea MenuBar Dialog Boxes FileDialog
Applet Component
Label :- Constructor:- new Label( ) throws HeadLessException new Label(String str) throws HeadlessException new Label(String str,int how) throws HeadlessException how=Label.LEFT or Label.RIGHT or Label.CENTER
Methods:- void setText(String str) String getText();
void setAlignment(int how) int getAlignment();
Applet Component
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ Label L1,L2; public void init() { L1=new Label(); L1.setText(“ABCD”); L2=new Label(); add(L1); add(L2); } }
Applet Component
Button :-Constructor:- new Button() throws HeadlessException new Button(String str) throws
HeadlessException Methods:- void setLabel(String str); //label for
the buttons. String getLabel();
Applet Componentimport java.awt.*;import java.applet.*;public class MyApp extends Applet{ Button b1,b2; public void init() { b1=new Button(); b1.setText(“PUSH”); L2=new Button(); add(b1); add(b2); } }
Applet ComponentCheck BoxesConstructor :- new Checkbox()throws HeadlessException // blank checkbox unchecked new Checkbox(String )throws HeadlessException// create a named checkbox new Checkbox(String ,boolean ) throws HeadlessException
// create a named checkbox if true checkbox is checked othewise it is false
new Checkbox(String,boolean,CheckboxGroup) throws HeadlessException new Checkbox(String,CheckboxGroup,boolean) throws HeadlessException //create a named checkbox with initial stateeither true or false depend on
boolean value , and also declare that check box is a part of checkbox group or not. Method:- boolean getState(); void setState(String str); String getLabel(); void setLabel(String str);
Applet Component
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ Checkbox c1,c2,c3; public void init() { c1=new Checkbox(); c2=new Checkbox(“WIN98”); c3=new Checkbox(WinXP,null,true); c1.setLabel(WIN95“); add(c1); add(c2); add(c3); } }
Applet Component
CheckboxGroup:- radiobutton only one checkboc at a time .
Constructor:- new CheckboxGroup(); Method:-
Checkbox getSelectedCheckbox(); void setSelectedCheckbox(Checkbox which)
Applet Component
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ Checkbox c1,c2,c3; public void init() { CheckboxGroup cbg=new CheckboxGroup(); c1=new Checkbox(WIN(%,cbg,true); c2=new Checkbox(“WIN98”,cbg,false); c3=new Checkbox(WinXP,cbg,false); c1.setLabel(WIN95“); add(c1); add(c2); add(c3); } }
Applet Component
Choice Controls:- Popup list of item which the user may choose.Constructor :- new Choice();Methods:- void add(String name) String getSelectedItem();
int getSelectedIndex();// index start from 0. void select(int index); void select (String name) ; String getItem(int index) ;
Applet Componentimport java.awt.*;import java.applet.*;public class MyApp extends Applet{ Choice c1; public void init() { Choice c1=new Choice(); c1.add(“WIN95”); c1.add(“WIN98”); c1.add(“WINXP”); add(c1); } }
Applet Component
Lists :- Multiple choice scroll selection list. Constructor :-
new List();
new List(int numrows);
new List(int numrows,boolean multiselect)
Method:-
void add(String name);
void add(String name,int index); // index start from 0. index=-1 it means add the item at the end of the list.
String getSelectedItem(); // index of selected item.
int getSelectedIndex(); //index start from 0. if more then one item is selected or no selection is made -1 is returned.
String [ ] getSelectedItems() ;//names of selcted items
int [ ] getSelectedIndexes() ; //indexes of selected items.
Applet Componentimport java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
List l1;
public void init()
{
l1=new List(5, true);
l1.add(“WIN95”);
l1.add(“WIN98”);
l1.add(“WINXP”);
l1.add(“WINVISTA”);
l1.add(“WIN&”);
add(l1);
}
}
Applet Component
Scroll Bar :- Used to select continuous values between a specified minimum and maximum .
Constructor:-
new Scrollbar() // horiozontal scrollbar
new Scrollbar(int style) //style=Scrollbar.Horizontal
Scrollbar.Vertical
new Scrollbar(int style,int initialvalue,int
thumbsize,int min,int max);
//initialvalue of the scroolbar, thumbsize represent the no of represent by the height of the thumb . minimumn and maximum value of scrollbar is represented by min and max .
Applet Component Scrollbar:- Methods :-
void setValues(int initval,int thumbsize,int max,int min); int getValue();// To retrieve the current value void setValue(int newvalue);// To set the new value of scrollbar. int getMinimum(); int getMaximum(); void setUnitIncrement(int newincr); void setBlockedIncrement(int newincr); // each time the 1 is incremented from the scroll bar each
time it is scrolled up or down one line. By default pageup and pagedown value are 10 we can change it by above two methods.
Applet Component
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ Scrollbar hsb,vsb; public void init() { hsb=new Scrollbar(Scrollbar.Horizontal,0,1,0,50); vsb=new Scrollbar(Scrollbar.Vertical,0,1,0,50) add(hsb);
add(vsb); } }
Applet ComponentTextField:-
Costructor:-
new TextField();
new TextField(int numchar);// create a textfield numchar wide
new TextField(String str); // create a text field with str.
new TextField(String str,int numchar); //create a textfield sets with str and numchar
set its width;
Methods:-
String getText();
void setText(String str);
String getSelectedText();
void select(int startindex,int endindex);//select at startindex and ending at endindex-1
void setEditable();
boolean isEditable();
Applet Component
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ TextField t1; public void init() { t1=new TextField( ); add(t1); } }
Applet ComponentTextField:-
Costructor:-
new TextArea();
new TextArea(int numline,int numchar);
new TextArea(String str);
new TextField(String str,int numline,int numchar);
new TextField(String str,int numline,int numchar,int sbars);// bars= SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Methods:-
String getText();
void setText(String str);
String getSelectedText();
void select(int startindex,int endindex);//select at startindex and ending at endindex-1
void setEditable();
boolean isEditable();
Applet Component
TextField:-Methods:- void append(String str); // append the string
specified by str void insert(String str,int index);//pass the
string str at specified by the index
void replaceRange(String str,int startind,int end ind); // replace the
characters from startindex to endIndex-1
Applet Component( MenuBar,Menu,MenuItem)
Constructors:-MenuBar :- new MenuBar(); Menu:- new Menu();
new Menu(String optionname);
MenuItem:- new MenuItem();
new MenuItem(String str); new MenuItem( String str,MenuShortcut keyAccel);
Applet Component( MenuBar,Menu,MenuItem)
Methods:- MenuItem:- Menuitem add(MenuItem
mi); MenuBar:- Menu add(Menu m);
Applet Componentimport java.awt.*;
import java.applet.*;
public class MyApp extends Applet
{
MenuBar mb,Menu m1,m2, MenuItem mi1,mi2,mi3
public void init()
{
mb=new MenuBar();
m1=new Menu(“File”);
m2=new Menu(“Edit”);
mi1=new MenuItem(“New”);
mi2=new MenuItem(“Open”);
mi3=new MenuItem(“Cut”);
m1.add(mi1);
m1.add(mi2);
m2.add(mi3)
mb.add(m1);
mb.add(m2);
setMenuBar(mb);
add(t1);
}
}
Applet Component
Container:-
1) Inherit the Component class . 2) It has additional method that allow other component object to be nested within it. 3)Container object contain other Container object. This makes multilevel containment system. 4)Conatianer is responsible for laying out(positioning) any component that it contain.It does this through the layout managers.
methods:- void add(Component cmp); void remove(Component cmp);
Layout Manager Every Component that we place in the
Container has a size and location in Container.This size and location of the component in the Container is managed by the LayoutManager. Following are the types of LayoutManagers
FlowLayoutManager GridLayoutManager BorderLayoutManager CardLayoutManager
Layout Manager
Methods:- void setLayout(LayoutManager
layoutobj); // layoutobj is the object of
LayoutManager class.if we pass null then we disable the layout manager and determine the shape and position manually using setBound() defined by Component class.
Layout Manager
FlowLayoutManager :- new FlowLayout(); // maintain the space of 5 pixels between
each component. new FlowLayout(int how) // how specify
alignment of component how=FlowLayout.LEFT
FlowLayout.RIGHT FlowLayout.CENTER new FlowLayout(int how,int horz, int vert ) // horz & vert specify horizontal and vertical space left between
each component .
Layout Manager
import java.awt.*;
import java.applet.*;public class MyApp extends Applet{ TextField t1,t2; Button b1,b2; FlowLayout fl; public void init() { fl=new FlowLayout(); setLayout(fl); t1=new TextField();
t1=new TextField(); b1=new Button(“add”); b2=new Button(“sustract”); add(t1);
add(t2); add(b1); add(b2);
} }
Layout Manager
GridLayoutManager:- new GridLayoutManager( ); //create
a grid of single row and
single column .
new GridLayoutManager(int row, int col);
new GridLayoutManager(int row, int col,int horz,int col);
Layout Manager
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ TextField t1,t2; Button b1,b2;
GridLayout gl; public void init() { gl=new GridLayout(2,2); setLayout(gl);; t1=new TextField();
t1=new TextField(); b1=new Button(“add”); b2=new Button(“sustract”); add(t1);
add(t2); add(b1); add(b2);
} }
Layout Manager
BorderLayoutManager :- It has four narrow fixed width components at
the edges and one large area in the center ,The four sides is referred to as north, south,east,west and middle are is referred as center. BorderLayout reffers the following constant that specify the regions.
BorderLayout.CENTER BorderLayout.EAST BorderLayout.WEST BorderLayout.SOUTH BorderLayout.NORTH All the regions specify with the Component when we add
that component in that Container. void add(Component compobj , Object region);
Layout Manager
Constructor:-
new BorderLayoutManager( );
new BorderLayout(int horz,int vert);
Layout Manager
import java.awt.*;import java.applet.*;public class MyApp extends Applet{ TextField t1,t2; Button b1,b2,b3; BorderLayout bl; public void init() { bl=new BorderLayout( ; setLayout(bl);; t1=new TextField();
t1=new TextField(); b1=new Button(“add”);
Layout Manager
b2=new Button(“sustract”); b3=new Button(“equal”) ; add(t1,BorderLayout.EAST);
add(t2, BorderLayout.WEST; add(b1, BorderLayout.SOUTH); add(b2, BorderLayout.NORTH);
add(b2, BorderLayout.CENTER); } }
Event Handling
Event:- It can be generated as a
consequence of person interacting with the elements in a gui. Some of the activities that cause events to be generated are pressing a button ,entring a character via a keyboard , selecting a list etc.
“ In the Event Delegation model,an Event is an object that describes a state change in a source. ”
Event Handling
To process the events we need to identify this events and then take some action. This is known as
event handling. The modern approach to handling an event is based on “Event Delegation Model”. In this model we have 3 following entities .
Source Event Handler or Listenr
Event Handling
Event Delegation Model :- (Delegation Event Model) Source generates
EventsHandeled by
register
Handler
Event Handling
Source :- Source is used to generate the event and it is mainly a Component.Examples Button,TextField etc. Every source and component has the predefined .
Event :- An Event can be generated by the source.It can possible that the same events are generated by different sources. Events are represented by classes .
Handler or Listener:- To handle the event handlers are used . Every event is a predefined handler. The event can be generated by any source but the handler is always be same. Handlers are represented by or interfaces.
Event Handling
ActionEvent is an event is generated when we click on button ar press the enter key in TextField . And this ActionEvent is handled by ActionListener interface.
Button onclick ActionEvent handled by
ActionListener
on enter
TextField
Event Handling
ActionListener is an interface which contains the following method.
public void actionPerformed(ActionEvent ae)
When the ActionEvent is generated the
ActionListener’s actionPerformed method get executed . The operation that we want to perform on the click of Button should be put within the actionPerformed method.
Event Handling
For the event handling we need to create the handler class. The class will be handler class when it implement the appropriate listener and override the methods of interface.
Ex:- public class A implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } }
Event HandlingHandler 1:- public class A implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } }Handler 2:-
public class B implements ActionListener { public void actionPerformed(ActionEvent ae) { -------------- -------------- -------------- -------------- } }
Event Handling(ActionEvent) import java.awt.*; import java.applet.*; import java.awt.event.*; public class MyApp extends Applet { public TextField t1,t2,t3; Button b1; FlowLayout fl; public void init()
Event Handling(ActionEvent) { fl=new FlowLayout( ); setLayout(fl); t1=new TextField(5); t2=new TextField(5); t3=new TextField(5); b1=new Button("add"); add(t1); add(t2); add(t3); add(b1);
Event Handling(ActionEvent) AddHandler ah=new
AddHandler(t1,t2,t3); b1.addActionListener(ah); } }
Event Handling(ActionEvent) import java.awt.*; import java.applet.*; import java.awt.event.*; public class AddHandler implements ActionListener { TextField th1,th2,th3; public AddHandler(TextField t33,TextField t43,TextField t53) { th1=t33; th2=t43; th3=t53; }
Event Handling(ActionEvent) public void actionPerformed(ActionEvent ae) { int
i=Integer.parseInt(th1.getText()); int
j=Integer.parseInt(th2.getText()); String s1=i+j+ ""; th3.setText(s1); } }
Event Handling
Once the handler is created it should be registered with source without which eventhandling is not possible it is necessary otherwise we don’t know which handler handle the event.
To register the handler with the source th methods are provided in the source.
Registration methods of Button and Textfield source is
following . void addActionListener(ActionListner al) // al represent the object of that class which implement
the ActionListener interface. Method belonging to Button and Textfield class.
Event Handling
Action Event- 1)button is clicked. 2) list item is double
clicked. 3)menu item is selected. 4) txt field is entered. ActionListener :- public void actionPerformed(ActionEvent
ae)
Event Handling
ItemEvent:- 1) checkbox or list item is selected . 2) choice selection is made 3)Checkable menu item is selected or deselected. ItemListener:- public void itemStateChanged(ItemEvent ie)
Event Handling
AdjustmentEvent :-Scroll bar is manipulated .
AdjustementListener:- void adjustmentValueChanged(
AdjustmentEvent e);
Event Handling
ComponentEvent:- when a component is hidden moved resized or become visible.
ComponentListener:-
void ComponentResized(ComponentEvevnt ce);void ComponentMoved(ComponentEvevnt ce);void ComponentShown(ComponentEvevnt ce);void ComponentHidden(ComponentEvevnt ce);
Event Handling
FocusEvent :- Component gains or loses keyboard focus.
FocusListener:-
void focusGained(FocusEvent fe); void focusLost(FocusLost fl);
Event Handling
ContainerEvent :- Component is added or removed from Container .
ContainerListener :- void componentAdded(ContainerEvent
ce) ;void
componentRemoved(ContainerEvent ce )
Event Handling
KeyEvent:- Input is received from keyboard .
KeyListener:- void keyPressed(KeyEvent
ke); void keyReleased(KeyEvent
ke); void keyTyped(KeyEvent ke);
Event Handling
MouseEvent :- When mouse is draggd, moved,clicked,pressed or released .
MouseListener :- void mouseClicked(MouseEvent me);void mouseEntered(MouseEvent me); void mouseDragged(MouseEvent me);
Event Handling
void mousePressed(MouseEvent me); void mouseExited(MouseEvent me)
Event Handling
MouseWheelEvent:- When mouse wheel is moved .
MouseWheelListener:-
void mouseWheelMoved(MouseWheelEvent mwe );
Event Handling
TextEvent:- When the value of text area or text field is generated .
TextListener:- void textChanged(TextEvent
te);
Event Handling
WindowEvent:- When window is activated, closed , deactivated, deiconified , iconified ,
opened or quit . WindowListener:- void windowActivated(WindowEvent we); void windowClosed(WindowEvent we); void windowClosing(WindowEvent we); void windowIconified(WindowEvent we); void windowDeiconified(WindowEvent we);
Event Handling
void windowDeactivated(WindowEvent we);
void windowOpened(WindowEvent we);
Inner class
Classes which is defined within another class. class A { Class B { } } A is container class or top level class. B is inner
class. One class can contain any no of inner class./Inner classs can contain inner class and this can go
upto any level.
Inner class
Inner classes are of 4 types.
1)Static inner class 2)Non static inner class 3)Local inner class4) Anonymous inner class
Inner class
Non static inner class:- This class ork as the member of class like
static inner class they can have any type of accessibility (public/private/protected/default),but like static inner class they can not have static members .
Because the non static class is the member of top level class and non static member exist when its object is created .The non static inner class doesn’t exist until the object of top level class created.
A non static inner class is always associated within the
object of top level class and therefore it has the special privilege it can access any member(static/non static) of its top level class . So that non static inner class is used for event handling.
Panel
A Component which is used as a Container. It is used for physically grouping of Component.Constructor:- new Panel(); // Create the panel with Flow
layout Manager. new Panel(LayoutManager lm); // Create
the panel with same layoutManager by
which is pass as the parameter in the panel.
Panel
Methods:- add(Component cmp);
add(Component cmp,int loc);
Standalone application
To cretae a stand alone application we ned to create a class which will inherit Frame class . To run it we don’t need html ,this type of application has it ‘s own window. And this type of application created just like console based application.
Features of stand alone application.1) Standalone application is also called Frame.2)Frame provide the outlaw of window we only need to place
the components on window. 3)Frame has the same applarcchitecture and structure as the applet
and therefore GUI application and the event handling will be same as applet.
4)Like applet it doesn’t have init,start,stop,destroy,paint methods and therefore instead of initialize the applet we need we need to define the constructor of that class which inherit the Frame class.
5)To display standalone applicatio we need main method.just like console based application
Standalone application
Import java.awt.*; Impoort java.applet.*; Import java.awt.event.*; public class Hobby extends Frame implements ItemListener{ --------------- -------------- public Hobbbby( )
{ ----------------- ------------------
------------------ same as init }
Standalone application
Public void itemStateChanged(ItemListener il){
----------------------
}}
Standalone application
public class HobbyGui { psvm(String arg[ ]){Hobby h=new Hobby();h.setSize(300,300);h.setVisible(true); }}
Standalone application
import java.awt.*; import java.applet.*; import java.awt.event.*; class MyFrame extends Frame implements ActionListener { TextField t1,t2,t3; Button b1; FlowLayout fl; public MyFrame() { fl=new FlowLayout( ); setLayout(fl); t1=new TextField(5);
Standalone application
t2=new TextField(5); t3=new TextField(5); b1=new Button("add"); add(t1); add(t2); add(t3); add(b1); b1.addActionListener(this); }
Standalone application
public void actionPerformed(ActionEvent ae)
{ int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); t3.setText(i+j+""); } }
Standalone application
public class MyWindow { public static void main(String arg[]) { MyFrame mf=new MyFrame(); mf.setSize(300,300); mf.setVisible(true); } }
Standalone application Some issues on Window:- When w click on crossbar th window
doesn’t terminate and still on RAM to close it we have to close the
DOS prompt because the event is generated by Frame we have to handle the event of Frame. There are 7 state of Frame when it generate event. In all 7 state it generate the WindowEvent .
To handle the 7 state of Frame WindowListener interface provide 7 different methods.When we implement the WindowListener we need to override the all 7 methods.
void windowActivated(WindowEvent we); void windowClosed(WindowEvent we); void windowClosing(WindowEvent we);
Standalone application
void windowIconified(WindowEvent we);
void windowDeiconified(WindowEvent we);
void windowDeactivated(WindowEvent we);
void windowOpened(WindowEvent we);
Standalone application
1) windowOpened is called after the application display on the screen . It called only once .It is used to initialize
the application.2)windowIconified is called when window is minimized .3) windowDeiconified is called when window is maximized .4) winowActivate is called when the application get the focus.5)windowDeactvate is called when the application lost the
focus.6)windowClosed is called when we click on cross button . 7)windowClosing dispose():- is called to terminate the application System.exit(0) :-is called to terminate the dos prompt or to
terminate the JVM.
Standalone application
windowClosing is called when the application is dispose.It is used to save the state of application.This is called only once but closed is not called untill dispose is called .
Registration method :- void addindowListner(WindowEvent we)
import java.awt.*; import java.applet.*; import java.awt.event.*; class MyFrame extends Frame
implements ActionListener,WindowListener
{ TextField t1,t2,t3; Button b1;
FlowLayout fl; public MyFrame() { fl=new FlowLayout( ); setLayout(fl); t1=new TextField(5); t2=new TextField(5); t3=new TextField(5);
b1=new Button("add"); add(t1); add(t2); add(t3); add(b1); b1.addActionListener(this); }
public void actionPerformed(ActionEvent ae) { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); t3.setText(i+j+""); } public void windowActivated(WindowEvent we) { } public void windowClosed(WindowEvent we)
{dispose(); System.exit(0); } public void windowClosing(WindowEvent we) { }
Standalone application
public void windowIconified(WindowEvent we) { }
public void windowDeiconified(WindowEvent we){ }
public void windowDeactivated(WindowEvent we) { }
public void windowOpened(WindowEvent we) { }
}
Standalone application
public class MyWindowHandler { public static void main(String arg[]) { MyFrame mf=new MyFrame(); mf.setSize(300,300); mf.setVisible(true); } }
Adapter class
WindowListener interface implements UserDefined class
Adapter class
WindowListener interface
implements WindowAdapter class extends UserDefined class
Adapter class
import java.awt.*; import java.applet.*; import java.awt.event.*; class MyFrame extends Frame implements
ActionListener,WindowAdapter { ------------------ ------------------ --------------------- public void windowClosed(WindowEvent we) { dispose(); System.exit(0); } ---------------------- -----------------
Adapter class
--------------------- } public class MyWindowAdapter { public static void main(String arg[]) { ------------------- ------------------- --------------------- } }
Adapter class
All listener those contain more than one
methods have Adapter classs. WindowListener . KeyListener MouseListener MouseMotionListener.