Unit-1:- Awt # Event Handling Q 1) Delegation event model / Event - TopicsExpress



          

Unit-1:- Awt # Event Handling Q 1) Delegation event model / Event / Event class / Event Listener Interface Event Class Description Interface Handler 1) ItemEven When a menu item is selected or deselected ItemListener p v itemStateChanged(ItemEvent i) or when a checkbox or list item is clicked 2) WindowEvent Window is activated, closed, opened or quit WindowListener p v windowIconified(WindowEvent w) p v windoweiconified(WindowEvent w) p v windowActivated(WindowEvent w) p v windowDeactivated(WindowEvent w) 3) TextEvent Value of TextArea and TextField is changed TextListener p v textValueChanged(TextEvent te) 4) MouseEvent Mouse is moved, clicked, dragged and released MouseListener, p v mouseEntered(MouseEvent m) p v mouseExited(MouseEvent m) p v mousePressed(MouseEvent m) p v mouseReleased(MouseEvent m) p v mouseClicked(MouseEvent m) MouseMotionListener p v mouseDragged(MouseEvent m) p v mouseMoved(MouseEvent m) 5) KeyEvent input is received from the keyboard KeyListener p v keyPressed(KeyEvent k) p v keyReleased(KeyEvent k) p v keyClicked(KeyEvent k) 6) ActionEvent Button is pressed, list item is double clicked or ActionListener p v actionPerformed(ActionEvent ae) a menu is selected 7) AdjustmentEvent When scrollbar is used AdjustmentListener p v adjustmentValueChanged(AdjustmentEvent ae1) 8) ComponentEvent Component is resized, moved, hidden or made ComponentListener p v componentHidden(ComponentEvent ce) visible p v componentMoved(ComponentEvent ce) p v componentResized(ComponentEvent ce) p v componentShown(ComponentEvent ce) 9) FocusEvent Component gains or loses keyboard focus FocusListener p v focusGained(FocusEvent fe) p v focusLost((FocusEvent fe) Q 2)Adapter classes:- Ans) 1)Adapters are abstract classes from java.awt.event package introduced with JDK 2)Adapter classes make event handling simple and easy. 3)Adapter classes are useful when you want to receive and process only some of the events that are handled by a particular event listener interface. 4)You can define a new class to act as an event listener by extending one of the adapter classes and implementing only those events in which you are interested. 5)For example, to close the window, all the 7 abstract methods of WindowListener should be overridden atleast with empty bodies. But is not the case with WindowAdapter. With WindowAdapter only one method windowClosing() is enough to override. Adapters can be used in inner classes also. 6)Below Table Indicates Listener Interface with their respective adapter class. Listener Interface Adapter Class ComponentListener ComponentAdapter ContainerListener ContainerAdapter FocousListener FocousAdapter KeyListener KeyAdapter MouseListener MouseAdapter MouseMotionListener MouseMotionAdapter WindowListener WindowAdapter Q3)Inner class:- Ans:- Inner Class : A class defined inside another class is called an ‘Inner Class’. 1)There are four types of inner classes: member, static member, local, and anonymous. 2)Member class:- is defined at the top level of the class. It may have the same access modifiers as variables (public, protected, package, static, final), and is accessed in much the same way as variables of that class. 3)Static member class:- is defined like a member class, but with the keyword static. Despite its position inside another class, a static member class is actually an "outer" class--it has no special access to names in its containing class. To refer to the static inner class from a class outside the containing class, use the syntax OuterClassName. InnerClassName. A static member class may contain static fields and methods. 4)Local inner class:- is defined within a method, and the usual scope rules apply to it. It is only accessible within that method, therefore access restrictions (public, protected, package) do not apply. However, because objects (and their methods) created from this class may persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method. 5)Anonymous inner class:- is one that is declared and used to create one object (typically as a parameter to a method), all within a single statement. Q4)Layout manager:- Ans:- 1)Layout manager class provides a mean for controlling the physical layout of GUI 2)Different types of layouts are : FlowLayout BorderLayout CardLayout GridLayout GridBagLayout 3)A layout is set with the method ‘setLayout()’ 4)FlowLayout:- i)FlowLayout is the default layout manager for applets and panels. ii)constructor:-FlowLayout exLayout=new FlowLayout(FlowLayout.RIGHT) 5)BorderLayout:- i)Default layout manager for objects of type ‘Window’, ‘Frame’ and ‘Dialog’ ii)BorderLayout arranges components in the ‘NORTH’, ‘SOUTH’, ‘EAST’, ‘WEST’ and ‘CENTER” of a container 6)GridLayout:- i)GridLayout places components in rows and columns ii)Constructor:- GridLayout g1=new GridLayout(4,3) (4 represents the number of rows and 3 the number of columns) 7)CardLayout:- i)CardLayout places components on top of each other ii)Can store a stack of several layouts iii)Used whenever we need number of panels each with a different layout to be displayed one by one 8)GridBagLayou:- i)GridBagLayout places components more precisely than any other layout manager ii)Components are arranged in rows and columns iii)Order of placing the components is ‘top-to-bottom’ iv)Components need not be of same size v)A container can be given the GridBagLayout using the following syntax: GridBagLayout gb=new GridBagLayout() ContainerName.setLayout(gb) Q 5)Lebels:- Ans:- 1)Generally used to indicate the purpose of an item 2)Not user-editable 3)Can be created using one of the following constructors – i)Label() – creates an empty label ii)Label(String labeltext) – creates a label with a given text iii)Label(String labeltext, int alignment) – creates a label with given alignment where alignment can be Label.LEFT, Label.RIGHT or Label.CENTER 4)example import java.awt.*; public class label extends Frame { Label lb1; lb=new Label("LABEL"); public label() { setTitle("My label"); setLayout(new FlowLayout()); add lb1; } public static void main(String args[]) { label l=new label(); l.setSize(500,500); l.setVisibel(true); } } Output LABEL Q6)TextField:- Ans:- 1)GUI element used to input text 2)Generally accepts one line of input 3)Can be created using one of the following constructors – i)Textfield() – creates a new textfield ii)Textfield(int columns) – creates a new textfield with given number of columns iii)Textfield(String s) – creates a new textfield with given string iv)Textfield(String s, int columns) – creates a new textfield with given string and given number of columns 4)example import java.awt.*; public class textF extends Frame { TextField lb1; lb=new TextField(20); public textF() { setTitle("My TextField"); setLayout(new FlowLayout()); add lb1; } public static void main(String args[]) { textF l=new textF(); l.setSize(500,500); l.setVisibel(true); } } Q7)Button:- Ans:- 1)Part of GUI 2)The easiest way to trap user action 3)Can create buttons in Java using any of the following constructors – i)Button():- Creat a button without a title ii)Button(String text):- Creat a button with title 4)Example:- import java.awt.*; public class button extends Frame { Button lb1; lb=new Button("BUTTON"); public button() { setTitle("My Button"); setLayout(new FlowLayout()); add lb1; } public static void main(String args[]) { button l=new button(); l.setSize(500,500); l.setVisibel(true); } } Q 8) CheckBox:- Ans:- 1)Used for multi-option user input that the user may select or deselect by clicking them 2)Checkboxes in Java can be created using any of the following constructors – i)Checkbox() – creates an empty textbox ii)Checkbox(String text) – creates a checkbox with given string as label 3)Example:- import java.awt.*; public class check extends Frame { Checkbox lb1,lb2,lb3; lb1=new Checkbox("Red",false); lb2=new Checkbox("Blue",false); lb3=new Checkbox("Green",false); public check() { setTitle("My Button"); setLayout(new FlowLayout()); add lb1; add lb2; add lb3; } public static void main(String args[]) { check l=new check(); l.setSize(500,500); l.setVisibel(true); } } Q 9)Radiobutton:- Ans:- 1)Used as option button to specify choices 2)Only one button in a radiobutton group can be selected 3)First create a CheckboxGroup object CheckboxGroup cg=new CheckboxGroup(); Then create each of the radio buttons – Checkbox male=Checkbox(“male”,cg,true); Checkbox female=Checkbox(“female”,cg,false) 4)Example:- import java.awt.*; public class radio extends Frame { Checkbox lb1,lb2; CheckboxGroup cg=new CheckboxGroup(); lb1=new Checkbox("Male",cg,false); lb2=new Checkbox("Female",cg,false); public radio() { setTitle("My Radio Button"); setLayout(new FlowLayout()); add lb1; add lb2; add lb3; } public static void main(String args[]) { radio l=new radio(); l.setSize(500,500); l.setVisibel(true); } } Q 10) Working with frame windows Ans:- 1)A window that is independent of an applet and of the browser 2)Can be a component or a container 3)Can be created using any one of the following constructors – i)Frame():-Creates a Frame which is invisible ii)Frame(String Title):-Creates a Frame with given title which is invisible 4)Example:- import java.awt.*; public class frame extends Frame { public frame() { setTitle("My Radio Button"); } public static void main(String args[]) { frame l=new frame(); l.setSize(500,500); l.setVisibel(true); } }
Posted on: Tue, 16 Jul 2013 16:48:44 +0000

Trending Topics



Recently Viewed Topics




© 2015