Applet

322 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
322
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Applet

  1. 1. APPLET•Applet Component•Layout Manager•Event Handling• Adapter Class
  2. 2. Applet Component COMPONENT Container Labels Button CheckBox CheckBoxGroup Choice Lists ScrollBar TextField TextArea MenuBar Dialog Boxes FileDialog
  3. 3. Applet ComponentLabel :-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.CENTERMethods:- void setText(String str) String getText(); void setAlignment(int how) int getAlignment();
  4. 4. Applet Componentimport 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); } }
  5. 5. Applet ComponentButton :-Constructor:- new Button() throws HeadlessException new Button(String str) throws HeadlessExceptionMethods:- void setLabel(String str); //label for the buttons. String getLabel();
  6. 6. 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); } }
  7. 7. 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();
  8. 8. Applet Componentimport 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); } }
  9. 9. Applet ComponentCheckboxGroup:- radiobutton only one checkboc at a time .Constructor:- new CheckboxGroup();Method:-Checkbox getSelectedCheckbox();void setSelectedCheckbox(Checkbox which)
  10. 10. Applet Componentimport 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); } }
  11. 11. Applet ComponentChoice 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) ;
  12. 12. 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); } }
  13. 13. 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.
  14. 14. 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); } }
  15. 15. 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 .
  16. 16. Applet Component Scrollbar:- Methods :-void setValues(int initval,int thumbsize,int max,int min);int getValue();// To retrieve the current valuevoid 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.
  17. 17. Applet Componentimport 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); } }
  18. 18. 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();
  19. 19. Applet Componentimport java.awt.*;import java.applet.*;public class MyApp extends Applet{ TextField t1; public void init() { t1=new TextField( ); add(t1); } }
  20. 20. 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_ONLYMethods:- 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();
  21. 21. 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
  22. 22. 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);
  23. 23. Applet Component( MenuBar,Menu,MenuItem)Methods:-MenuItem:- Menuitem add(MenuItem mi);MenuBar:- Menu add(Menu m);
  24. 24. 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);
  25. 25. 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);
  26. 26. 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
  27. 27. 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.
  28. 28. 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 .
  29. 29. Layout Managerimport 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); } }
  30. 30. 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);
  31. 31. Layout Managerimport 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); } }
  32. 32. 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);
  33. 33. Layout Manager Constructor:- new BorderLayoutManager( ); new BorderLayout(int horz,int vert);
  34. 34. Layout Managerimport 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”);
  35. 35. Layout Managerb2=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); } }
  36. 36. 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. ”
  37. 37. 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
  38. 38. Event Handling Event Delegation Model :- (Delegation Event Model) Source generates Events Handeled by register Handler
  39. 39. 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.
  40. 40. 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 enterTextField
  41. 41. 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.
  42. 42. 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) { -------------- -------------- -------------- -------------- } }
  43. 43. 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) { -------------- -------------- -------------- -------------- } }
  44. 44. 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()
  45. 45. 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);
  46. 46. Event Handling(ActionEvent) AddHandler ah=new AddHandler(t1,t2,t3); b1.addActionListener(ah); } }
  47. 47. 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; }
  48. 48. 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); } }
  49. 49. 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.
  50. 50. 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)
  51. 51. 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)
  52. 52. Event Handling AdjustmentEvent :-Scroll bar is manipulated . AdjustementListener:-void adjustmentValueChanged( AdjustmentEvent e);
  53. 53. 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);
  54. 54. Event Handling FocusEvent :- Component gains or loses keyboard focus. FocusListener:-void focusGained(FocusEvent fe);void focusLost(FocusLost fl);
  55. 55. Event Handling ContainerEvent :- Component is added or removed from Container .ContainerListener :-void componentAdded(ContainerEvent ce) ;void componentRemoved(ContainerEvent ce )
  56. 56. Event Handling KeyEvent:- Input is received from keyboard . KeyListener:- void keyPressed(KeyEvent ke); void keyReleased(KeyEvent ke); void keyTyped(KeyEvent ke);
  57. 57. 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);
  58. 58. Event Handlingvoid mousePressed(MouseEvent me);void mouseExited(MouseEvent me)
  59. 59. Event HandlingMouseWheelEvent:- When mouse wheel is moved .MouseWheelListener:-void mouseWheelMoved(MouseWheelEvent mwe );
  60. 60. Event Handling TextEvent:- When the value of text area or text field is generated . TextListener:- void textChanged(TextEvent te);
  61. 61. 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);
  62. 62. Event Handlingvoid windowDeactivated(WindowEvent we);void windowOpened(WindowEvent we);
  63. 63. 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.
  64. 64. Inner class Inner classes are of 4 types.1)Static inner class2)Non static inner class3)Local inner class4) Anonymous inner class
  65. 65. Inner class Non static inner class:- This class ork as the member of class likestatic 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.
  66. 66. 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.
  67. 67. Panel Methods:- add(Component cmp); add(Component cmp,int loc);
  68. 68. 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
  69. 69. Standalone application Import java.awt.*; Impoort java.applet.*; Import java.awt.event.*;public class Hobby extends Frame implements ItemListener{ --------------- -------------- public Hobbbby( ){ ----------------- ------------------ ------------------ same as init }
  70. 70. Standalone applicationPublic void itemStateChanged(ItemListener il) { ---------- ----- ------- }}
  71. 71. Standalone application public class HobbyGui { psvm(String arg[ ]){Hobby h=new Hobby();h.setSize(300,300);h.setVisible(true); }}
  72. 72. 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);
  73. 73. 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); }
  74. 74. Standalone application public void actionPerformed(ActionEvent ae) { int i=Integer.parseInt(t1.getText()); int j=Integer.parseInt(t2.getText()); t3.setText(i+j+""); } }
  75. 75. Standalone application public class MyWindow { public static void main(String arg[]) { MyFrame mf=new MyFrame(); mf.setSize(300,300); mf.setVisible(true); } }
  76. 76. 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);
  77. 77. Standalone applicationvoid windowIconified(WindowEvent we);void windowDeiconified(WindowEvent we);void windowDeactivated(WindowEvent we);void windowOpened(WindowEvent we);
  78. 78. Standalone application1) windowOpened is called after the applicationdisplay 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.
  79. 79. 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)
  80. 80.  import java.awt.*; import java.applet.*; import java.awt.event.*; class MyFrame extends Frame implements ActionListener,WindowListener { TextField t1,t2,t3; Button b1;
  81. 81.  FlowLayout fl; public MyFrame() { fl=new FlowLayout( ); setLayout(fl); t1=new TextField(5); t2=new TextField(5); t3=new TextField(5);
  82. 82.  b1=new Button("add"); add(t1); add(t2); add(t3); add(b1); b1.addActionListener(this); }
  83. 83.  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) { }
  84. 84. Standalone application public void windowIconified(WindowEvent we) { } public void windowDeiconified(WindowEvent we){ } public void windowDeactivated(WindowEvent we) { } public void windowOpened(WindowEvent we) { } }
  85. 85. Standalone application public class MyWindowHandler { public static void main(String arg[]) { MyFrame mf=new MyFrame(); mf.setSize(300,300); mf.setVisible(true); } }
  86. 86. Adapter class WindowListener interface implements UserDefined class
  87. 87. Adapter class WindowListener interface implements WindowAdapter class extends UserDefined class
  88. 88. 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); } ---------------------- -----------------
  89. 89. Adapter class --------------------- } public class MyWindowAdapter { public static void main(String arg[]) { ------------------- ------------------- --------------------- } }
  90. 90. Adapter class All listener those contain more than one methods have Adapter classs. WindowListener . KeyListener MouseListener MouseMotionListener.

×