• No results found

Delegation Event Modelling

In document Java Notes (Page 148-189)

When an event occurs programmer has 3 options 1. Ignore the event

2. Let the component manage the event

3. Delegate its handling to some other object called Listener

According to delegation modeling, For example when a Button click occurs , then the listener if connected can listen to the event, and then invoke a method action performed.

Button click | |

Listener listens,and creates an ActionEvent object |

|

Passes this object to actionPerformed( ) method

Control Listener Methods and Events received

--- --- ---

Button ActionListener actionPerformed(ActionEvent e) List

Menultem TextField

Checkbox ItemListener itemStateChanged(ItemEvent e)

Choice

List

Checkbox MenuItem

DialogFrame WindowListener windowOpened(WindowEvent e)

windowClosing(WindowEvent e) windowMinimized(WindowEvent e)

Scrollbar AdjustmentListener

adjustmentValueChanged( AdjustmentEvent e)

Canvas MouseListener mousePressed(MouseEvent e)

Dialog mouseReleased(MouseEvent e)

Frame mouseEntered(MouseEvent e)

Panel mouseExited(MouseEvent e)

Window mouseClicked(MouseEvent e)

Component KeyListener keyTyped(KeyEvent e) keyPressed(KeyEvent e)

keyReleased(KeyEvent e)

Component FocusListener focusGained(FocusEvent e) focusLost(FocusEvent e)

TextComponent TextListener textValueChanged(TextEvent e)

Events and methods ActionEvent ::

It occurs,

1. When a button is clicked

2. When a menu item is selected 3. When a list item is double clicked 4. When enter key pressed in text field

method :: getSource( ) gives the source that made this event ItemEvent ::

It occurs,

1. When a check box is select/deselected 2. when a choice item is select/deselected 3. When a list item select/deselected

4. Whena checkbox menu item select/deselected

method :: getItemSelectable( ) gives reference to the item selected component.

getItem() gives reference to the item that caused the event

AdjustmentEvent :: It occurs,

1. When a scroll bar thumb is moved

method :: getAdjustable( ) gives the object that caused the event getValue() gives the new value

TextEvent :: It occurs,

1. When content of text is changed in TextField, TextArea

MouseEvent :: It occurs,

1. When user clicks or moves the mouse pointer

method :: getX(), getY() gives x,y screen coordinates of mouse position

getClickCount() gives click count for an event WindowEvent :: It occurs,

1. When a window minimized, opened, activated etc., FocusEvent :: It occurs,

1. When a component gains or loses focus

KeyEvent ::

1. It is generated by the user during interaction from keyboard

method :: getkeyCode() gives the key code for a given char getKeyChar() gives char defined by a key code

Example 111 : Demo Button Events

public class Demobutt extends Applet implements ActionListener {

TextField t1, t2, t3;

Label l1,l2,l3;

Button b1,b2;

GridLayout gl;

public void init() {

gl = new GridLayout(4,2);

setLayout(gl);

l1 = new Label("Firstnumb" );

add(l1);

t1 = new TextField(10);

add(t1);

l2 = new Label("Secondnumb" );

add(l2);

t2 = new TextField(10);

add(t2);

l3 = new Label("Result " );

add(l3);

t3 = new TextField(10);

add(t3);

b1 = new Button("ADD");

add(b1);

b1.addActionListener(this);

b2 = new Button("SUB");

add(b2);

b2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == b1) {

int p = Integer.parseInt( t1.getText() ) + Integer.parseInt( t2.getText() );

t3.setText( String.valueOf(p) );

}

if(e.getSource() == b2) {

int p = Integer.parseInt( t1.getText() ) - Integer.parseInt( t2.getText() );

t3.setText( String.valueOf(p) );

} } }

Example 112 : Demo CheckBox Events import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <APPLET CODE=checks.class WIDTH=200 HEIGHT=200 >

</APPLET> */

public class checks extends Applet implements ItemListener {

Checkbox c1, c2, c3;

TextField t1;

public void init() {

c1 = new Checkbox("1");

add(c1);

c1.addItemListener(this);

c2 = new Checkbox("2");

add(c2);

c2.addItemListener(this);

c3 = new Checkbox("3");

add(c3);

c3.addItemListener(this);

t1 = new TextField(20);

add(t1);

}

public void itemStateChanged(ItemEvent e)

{

if(e.getItemSelectable() == c1 && c1.getState() ) {

t1.setText("Check box 1 selected !");

} else

if(e.getItemSelectable() == c2 && c2.getState() ) {

t1.setText("Check box 2 selected!");

} else

if(e.getItemSelectable() == c3 && c3.getState() ) {

t1.setText("Check box 3 selected!");

} }

}

Example 113 :Demo Radio Events

public class radios extends Applet implements ItemListener {

CheckboxGroup cg;

Checkbox r1, r2, r3;

TextField t1;

public void init() {

cg = new CheckboxGroup();

r1 = new Checkbox("CSE", false, cg);

add(r1);

r1.addItemListener(this);

r2 = new Checkbox("ECE", false, cg);

add(r2);

r2.addItemListener(this);

r3 = new Checkbox("EEE", false, cg);

add(r3);

r3.addItemListener(this);

t1 = new TextField(20);

add(t1);

}

public void itemStateChanged(ItemEvent e) {

String k = ((Checkbox) e.getItemSelectable()).getLabel();

t1.setText("Radio button " + k + " clicked!");

} }

Example 114 : Demo Text Area

public class Demoreplace extends Applet implements ActionListener {

TextArea ta1;

Button b1;

public void init() {

ta1 = new TextArea("Now is the time.", 5, 20, TextArea.SCROLLBARS_BOTH);

add(ta1);

b1 = new Button("Replace selected text");

add(b1);

b1.addActionListener(this);

}

public void actionPerformed (ActionEvent e) {

if( e.getSource() == b1 ) {

ta1.replaceRange( "Hello from Java!" , ta1.getSelectionStart() , ta1.getSelectionEnd() ) ;

} } }

Example 115 : Demo List Events- single select

public class list extends Applet implements ActionListener {

List l1;

TextField t1;

public void init() {

t1 = new TextField(20);

add(t1);

l1 = new List(4);

l1.add("Item 1");

l1.add("Item 2");

l1.add("Item 3");

l1.add("Item 4");

l1.add("Item 5");

l1.add("Item 6");

l1.add("Item 7");

l1.add("Item 8");

l1.add("Item 9");

add(l1);

l1.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == l1) {

String n = ( (List) e.getSource() ).getSelectedItem();

t1.setText( n );

} } }

Example 116 : Demo Multiselect - List Events

public class multiselect extends Applet implements ActionListener {

List l1;

TextField t1;

Button b1;

String s[];

public void init() {

t1 = new TextField(40);

add(t1);

l1 = new List(4, true);

l1.add("Item 1");

l1.add("Item 2");

l1.add("Item 3");

l1.add("Item 4");

l1.add("Item 5");

l1.add("Item 6");

l1.add("Item 7");

l1.add("Item 8");

l1.add("Item 9");

add(l1);

b1 = new Button("Show selections");

b1.addActionListener(this);

add(b1);

}

public void actionPerformed(ActionEvent e) {

String ot = new String("You selected:");

if(e.getSource() == b1) {

s = l1.getSelectedItems();

for(int i = 0; i < s.length; i++) {

ot += " " + s[i];

}

t1.setText(ot);

} } }

Example 117 : Demo Choice Events

public class choice extends Applet implements ItemListener, ActionListener

{

TextField t1, t2;

Choice c1;

Button b1;

public void init() {

t1 = new TextField(20);

t2 = new TextField(20);

b1 = new Button("Remove Listeners");

add(t1);

add(t2);

c1 = new Choice();

c1.add("Item 1");

c1.add("Item 2");

c1.add("Item 3");

c1.add("Item 4");

add(c1);

add(b1);

c1.addItemListener(this);

b1.addActionListener(this);

}

public void itemStateChanged(ItemEvent e) {

if( e.getItemSelectable() == c1 ) {

String n = ((Choice)e.getItemSelectable)).getSelectedItem();

t1.setText("You chose " + n);

} }

public void actionPerformed(ActionEvent e) {

if( e.getSource() == b1 ) {

ItemListener[] al = c1.getItemListeners();

for (int i=0; i<al.length; i++) {

c1.removeItemListener(al[i]);

t2.setText("Listener Removed");

} }

}

}

Example 118 : Demo Scroll Events

public class Demoscroll extends Applet implements AdjustmentListener {

Scrollbar hS1, hS2, vS1, vS2;

TextField t1;

public void init() {

setLayout(new BorderLayout());

hS1 = new Scrollbar(Scrollbar.HORIZONTAL, 1, 1, 1, 100);

add("North", hS1);

hS1.addAdjustmentListener(this);

vS1 = new Scrollbar(Scrollbar.VERTICAL, 1, 1, 1, 100);

add("West", vS1);

vS1.addAdjustmentListener(this);

hS2 = new Scrollbar(Scrollbar.HORIZONTAL, 1, 1, 1, 100);

add("South", hS2);

hS2.addAdjustmentListener(this);

vS2 = new Scrollbar(Scrollbar.VERTICAL, 1, 1, 1, 100);

add("East", vS2);

vS2.addAdjustmentListener(this);

t1 = new TextField();

add("Center", t1);

}

public void adjustmentValueChanged(AdjustmentEvent e) {

if(e.getAdjustable() == hS1) {

hS2.setValue(hS1.getValue());

t1.setText("From Horizontal " + hS1.getValue());

}

if(e.getAdjustable() == vS1) {

vS2.setValue(vS1.getValue());

t1.setText("From Vertical " + vS1.getValue());

}

if(e.getAdjustable() == hS2)

{

hS1.setValue(hS2.getValue());

t1.setText("From Horizontal " + hS2.getValue());

}

if(e.getAdjustable() == vS2) {

vS1.setValue(vS2.getValue());

t1.setText("From Vetical " + vS2.getValue());

} } }

Example 119 : Demo Mouse Events

public class MouseDemo extends Applet implements MouseListener, MouseMotionListener

{

String str=" ";

int X=0,Y=0;

public void init() {

addMouseListener(this );

addMouseMotionListener( this);

}

public void mouseClicked(MouseEvent e) {

X=0; Y=10;

str= "Mouse Clicked";

repaint( ) ; }

public void mouseEntered(MouseEvent e) {

X=0; Y=10;

str= "Mouse Entered";

repaint( ) ; }

public void mouseExited(MouseEvent e) {

X=0; Y=10;

str= "Mouse Exited";

repaint ( ) ; }

public void mousePressed(MouseEvent e) {

X=e.getX(); Y=e.getY( ) ; str= "Mouse Down";

repaint( ) ; }

public void mouseReleased(MouseEvent e) {

X = e.getX() ; Y=e.getY() ; str="Mouse Up"; repaint();

}

public void mouseDragged(MouseEvent e) {

X=e.getX(); Y=e.getY(); str="Dragging Mouse ";

showStatus ( "Dragging at"+ e.getX()+ ","+ e.getY() );

repaint();

}

public void mouseMoved(MouseEvent e) {

showStatus ( "Moving mouse at"+ e.getX()+"," + e.getY() );

}

public void paint(Graphics g) {

g.drawString(str, X, Y);

} }

Example 120 : demo Key Events

public class Demokey extends Applet implements KeyListener {

String text = "";

public void init() {

addKeyListener(this);

requestFocus();

}

public void paint(Graphics g) {

g.drawString(text, 0, 100);

}

public void keyTyped(KeyEvent e) {

text= "Key Character";

text = text + e.getKeyChar();

repaint();

}

public void keyPressed(KeyEvent e) { }

public void keyReleased(KeyEvent e) {

text = "Key Location";

text = text + e.getKeyLocation();

} }

Example 121 : Demo card layout class cardPanel extends Panel

{

Button b;

Label l;

cardPanel(card app, String cardnumb) {

b = new Button("Next card");

b.addActionListener(app);

add(b);

l = new Label("This is card " + cardnumb);

add(l);

} }

public class card extends Applet implements ActionListener {

int index = 1;

CardLayout cl;

CardPanel p1, p2, p3;

public void init() {

cl = new CardLayout();

setLayout(cl);

p1 = new cardPanel(this, "one");

p2 = new cardPanel(this, "two");

p3 = new cardPanel(this, "three");

add("first", p1);

add("second", p2);

add("third", p3);

cl.show(this, "first");

}

public void actionPerformed(ActionEvent event) {

switch(++index) {

case 1: cl.show(this, "first");

break;

case 2: cl.show(this, "second");

break;

case 3: cl.show(this, "third");

break;

}

if(index == 3) index = 0;

repaint();

} }

Example 122 : Demo Gridbag Layout

public class Demogridbag extends Applet implements ActionListener {

Button b1, b2, b3;

TextField t1;

public void init()

{

GridBagLayout gr = new GridBagLayout();

GridBagConstraints con = new GridBagConstraints();

setLayout(gr);

con.weighty = 1;

con.fill = GridBagConstraints.BOTH;

con.weightx = 1;

b1 = new Button("Button 1");

gr.setConstraints(b1, con);

b1.setActionCommand("button 1");

add(b1);

b1.addActionListener(this);

con.weightx = 2;

b2 = new Button("Button 2");

gr.setConstraints(b2, con);

b2.setActionCommand("button 2");

add(b2);

b2.addActionListener(this);

con.weightx = 1;

b3 = new Button("Button 3");

con.gridwidth = GridBagConstraints.REMAINDER;

gr.setConstraints(b3, con);

b3.setActionCommand("button 3");

add(b3);

b3.addActionListener(this);

t1 = new TextField();

con.gridwidth = GridBagConstraints.REMAINDER;

gr.setConstraints(t1, con);

add(t1);

}

public void actionPerformed(ActionEvent e) {

String m = ((Button) e.getSource()).getActionCommand();

t1.setText("You clicked " + m );

} }

Adapter classes ::

They are the classes that have already implemented the event interfaces with empty methods. Then we may override any method of choice as per the need. For Ex. MouseListener has 5 methods that we must implement, but when a MouseAdpater class is used in its place then, we may override only the required methods.

Example 123 :: Demo of inner adapter class public class Demoapp extends Applet

{

String s = "Hello to Java!";

public void init() {

addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

s = "Bye to Java!";

repaint();

} } );

}

public void paint(Graphics g) {

g.drawString(s,60,100);

} }

Creating window Applications

Example 124 : Demo frame window

public class TestWin extends Applet implements ActionListener {

Button b1, b2;

Demowin wind;

public void init() {

b1 = new Button("Display the window");

add(b1);

b1.addActionListener(this);

b2 = new Button("Hide the window");

add(b2);

b2.addActionListener(this);

wind = new Demowin("Java window");

wind.setSize(300, 200);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == b1) { wind.setVisible(true);

}

if(e.getSource() == b2) { wind.setVisible(false);

} }

}

class Demowin extends Frame implements MouseListener,WindowListener {

Label l;

Demowin(String title) {

super(title);

setLayout( new FlowLayout() );

l = new Label("Hello from Java! This is a frame window.");

add(l);

addMouseListener(this);

// This is to exit when the window is closed addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e)

{

setVisible(false);

// we can also use, System.exit(0);

} } );

}

public void mousePressed(MouseEvent e) {

if( ( e.getModifiers() & InputEvent.BUTTON1_MASK ) ==

InputEvent.BUTTON1_MASK ) {

l.setText( "Left mouse down at "+e.getX()+","+e.getY() );

} else {

l.setText( "Right mouse down at"+e.getX()+","+ e.getY() );

} }

public void mouseClicked(MouseEvent e)

{

l.setText( "Mouse clicked at " + e.getX()+","+e.getY() );

}

public void mouseReleased(MouseEvent e) {

l.setText("The mouse button went up.");

}

public void mouseEntered(MouseEvent e) {

l.setText("The mouse entered.");

}

public void mouseExited(MouseEvent e) {

l.setText("The mouse exited.");

}

}

Menu:

Object |

MenuComponent |

____________

| | MenuItem MenuBar |

_______________

| | CheckboxMenuItem Menu |

| PopupMenu

In java there is an abstract class called MenuComponent, for menu related classes.

A MenuBar class, implements a menubar which is attached to a frame window

Menu class, implements a single pull down menu, that is attached to a menu bar or other menu.

MenuItem class, implements items that can be selected from a pull down Menu. CheckboxMenuItem class implements menu items, that may be cheked on or off for toggle purpose.

There is a seperate MenuContainer interface providing a set of methods, which are implemented by MenuBar class and Menu class. Frames also implement this class.

Example 125 : Demo of Menus

public class Demomenu extends Applet implements ActionListener {

Button b1;

Testwind menuWindow;

public void init() {

b1 = new Button("Display the menu window");

add(b1);

b1.addActionListener(this);

menuWindow = new Testwind("SampleMenus");

menuWindow.setSize(200, 200);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == b1) {

menuWindow.setVisible(true);

} }

}

class Testwind extends Frame implements ActionListener {

MenuBar mb;

Menu m;

MenuItem mi1, mi2, mi3;

Label l;

Testwind(String title) {

super(title);

l = new Label("Hello from Java!");

setLayout(new GridLayout(1, 1));

add(l);

mb = new MenuBar();

m = new Menu("File");

mi1 = new MenuItem("Item 1");

m.add(mi1);

mi1.addActionListener(this);

mi2 = new MenuItem("Item 2");

m.add(mi2);

mi2.addActionListener(this);

mi3 = new MenuItem("Item 3");

m.add(mi3);

mi3.addActionListener(this);

mb.add(m);

setMenuBar(mb);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

setVisible(false);

} } );

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == mi1) l.setText("You chose item 1");

else

if(e.getSource() == mi2)

l.setText("You chose item 2");

else if(e.getSource() == mi3) l.setText("You chose item 3");

} }

Example 126 : Demo of Menus, submenu, CheckMenuitem etc., public class Demomenu1 extends Applet implements ActionListener {

Button b1;

Testwind menuWindow;

public void init() {

b1 = new Button("Display the menu window");

add(b1);

b1.addActionListener(this);

menuWindow = new Testwind("SampleMenus");

menuWindow.setSize(200, 200);

}

public void actionPerformed(ActionEvent event) {

if(event.getSource() == b1)

{ menuWindow.setVisible(true); } }

}

class Testwind extends Frame implements ActionListener, ItemListener {

MenuBar mb;

Menu m, s;

Label l;

MenuItem mi1, mi2, mi4;

MenuItem si1, si2, si3;

CheckboxMenuItem mi3;

Testwind(String title) {

super(title);

l = new Label("Hello from Java!");

setLayout(new GridLayout(1, 1));

add(l);

mb = new MenuBar();

m = new Menu("File");

mi1 = new MenuItem("Item 1");

mi1.addActionListener(this);

m.add(mi1);

mi2 = new MenuItem("Item 2");

mi2.addActionListener(this);

m.add(mi2);

m.addSeparator();

mi3 = new CheckboxMenuItem("Check Item");

mi3.addItemListener(this);

m.add(mi3);

m.addSeparator();

s = new Menu("Sub menus");

si1 = new MenuItem("Sub item 1");

si2 = new MenuItem("Sub item 2");

si3 = new MenuItem("Sub item 3");

si1.addActionListener(this);

si2.addActionListener(this);

si3.addActionListener(this);

s.add(si1);

s.add(si2);

s.add(si3);

mi2.add(s);

m.addSeparator();

mi4 = new MenuItem("Exit");

mi4.addActionListener(this);

m.add(mi4);

mb.add(m);

setMenuBar(mb);

addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e) {

setVisible(false);

} } );

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == mi1)

label.setText("You chose item 1");}

else

if(e.getSource() == mi2) {

mi2.setEnabled(false);

l.setText("You chose item 2");

} else

if(e.getSource() == si1)

l.setText("You chose sub item 1");

else

if(e.getSource() == si2)

l.setText("You chose sub item 2");

else

if(e.getSource() == si3)

l.setText("You chose sub item 3");

else

if(e.getSource() == mi4) setVisible(false);

} }

public void itemStateChanged (ItemEvent e) {

if(e.getSource() == mi3) {

if( ( (CheckboxMenuItem) e.getItemSelectable() ).getState() ) l.setText("Item 3 is checked");

else

l.setText("Item 3 is not checked");

} } }

Example 127 : Demo of Pop up menus

When we right click on some application menus called, pop up appear

public class popup extends Applet implements ActionListener, MouseListener

{

Label l;

PopupMenu pp;

MenuItem mi1, mi2, mi3, mi4;

public void init() {

pp = new PopupMenu("Menu");

mi1 = new MenuItem("Item 1");

mi1.addActionListener(this);

mi2 = new MenuItem("Item 2");

mi2.addActionListener(this);

mi3 = new MenuItem("Item 3");

mi3.addActionListener(this);

mi4 = new MenuItem("Item 4");

mi4.addActionListener(this);

pp.add(mi1);

pp.addSeparator();

pp.add(mi2);

pp.addSeparator();

pp.add(mi3);

pp.addSeparator();

pp.add(mi4);

add(pp);

l = new Label("Hello from Java!");

add(l);

addMouseListener(this);

}

public void mousePressed(MouseEvent e) {

if(e.getModifiers() != 0) {

pp.show( this, e.getX(), e.getY() );

} }

public void mouseClicked(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ }

public void actionPerformed(ActionEvent e) {

if(e.getSource() == mi1)

l.setText("You chose item 1");

else if(e.getSource() == mi2) l.setText("You chose item 2");

else if(e.getSource() == mi3) l.setText("You chose item 3");

else if(e.getSource() == mi4) l.setText("You chose item 4");

} }

Dialog Box

A Dialog class can create a Dialog box. It is a sub class of window class. It will not have menubar, and can not be resized. They are popup windows, and accept input from user and always depend on other windows. Hnece it is always created from an existing window.

There are two types.

Modal :: It does not allow the user to interact with any other window while it is displyed, and it is on top of all

Modeless:: It allow the user to interact with other windows. for Ex a message box displaying the status of installation process, is modeless dialogue as the user can perform other jobs while installation is under progress.

Example 128 : Demo of Dialog Boxes

class mydialogue extends Dialogue implements ActionListener {

label l; Button b1,b2;

mydialogue( par, title) {

super(par, title, true);

setSize(200,200);

setLayout( new FlowLayout( ) );

l = new Label("selected quit option");

add(l);

b1 = new Button("OK");

add(b1);

b1.addActionListener(this);

b2 = new Button("Cancel");

add(b2);

b2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

dispose( );

} }

class myFrame extends Frame implements ActionListener {

myFrame( String title ) {

super(title);

Menubar mb = new MenuBar( );

setMenuBar(mb);

Menu m = new Menu("File");

MenuItem mi1,mi2;

mi1 = new MenuItem("New");

mi2 = new MenuItem("Quit");

mb.add(m);

mi2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == mi2) {

mydialogue d = new mydialogue(this, "Demo Dialogue");

d.setVisible(true);

dispose();

} } }

public class Demodialogue extends Applet {

myFrame f;

public void init( ) {

f = new myFrame(" frame window");

f.setSize(300,300);

}

public void start( ) {

f.setVisible(true);

}

public void stop( ) {

f.setVisible(false);

} }

Example 129 : Demo of mini calculator class glass extends Panel

{

TextField t1;

glass( ) {

t1 = new TextField(20);

add(t1);

} }

class digits extends Panel {

Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;

Button bp,bs,bm,bd,bpo,be,bon;

digits( ) {

setlayout( new GridLayout(5,4));

b0 = new Button("0"); add (b0);

b1 = new Button("1"); add (b1);

b2 = new Button("2"); add (b2);

b3 = new Button("3"); add (b3);

b4 = new Button("4"); add (b4);

b5 = new Button("5"); add (b5);

b6 = new Button("6"); add (b6);

b7 = new Button("7"); add (b7);

b8 = new Button("8"); add (b8);

b9 = new Button("9"); add (b9);

bp = new Button("+"); add (bp);

bs = new Button("-"); add (bs);

bm = new Button("*"); add (bm);

bd = new Button("/"); add (bd);

bpo = new Button("."); add (bpo);

be = new Button("="); add(be);

bon = new Button("ON/OFF"); add(bon);

} }

public class Democal extends Applet implements ActionListener {

float m,n; char f;

glass p; digits d;

public void init( ) {

setlayout(new BorderLayout( ));

p = new glass( ); add( p, BorderLayout.NORTH );

d = new digits( ); add( d, BorderLayout.CENTER );

d.b0.addActionListener(this);

d.b1.addActionListener(this);

d.b2.addActionListener(this);

d.b3.addActionListener(this);

d.b4.addActionListener(this);

d.b5.addActionListener(this);

d.b6.addActionListener(this);

d.b7.addActionListener(this);

d.b8.addActionListener(this);

d.b9.addActionListener(this);

d.bp.addActionListener(this);

d.bs.addActionListener(this);

d.bm.addActionListener(this);

d.bd.addActionListener(this);

d.bpo.addActionListener(this);

d.be.addActionListener(this);

d.bon.addActionListener(this);

}

public void actionperformed(ActionEvent e)

{

if( e.getSource( ) == d.bo)

p.t1.setText( (p.t1.getText( ) + d.b0.getlabel( ) );

if( e.getSource( ) == d.b1)

p.t1.setText( (p.t1.getText( ) + d.b1.getlabel( ) );

if( e.getSource( ) == d.b2)

p.t1.setText( (p.t1.getText( ) + d.b2.getlabel( ) );

if( e.getSource( ) == d.b3)

p.t1.setText( (p.t1.getText( ) + d.b3.getlabel( ) );

if( e.getSource( ) == d.b4)

p.t1.setText( (p.t1.getText( ) + d.b4.getlabel( ) );

if( e.getSource( ) == d.b5)

p.t1.setText( (p.t1.getText( ) + d.b5.getlabel( ) );

if( e.getSource( ) == d.b6)

p.t1.setText( (p.t1.getText( ) + d.b6.getlabel( ) );

if( e.getSource( ) == d.b7)

p.t1.setText( (p.t1.getText( ) + d.b7.getlabel( ) );

if( e.getSource( ) == d.b8)

p.t1.setText( (p.t1.getText( ) + d.b8.getlabel( ) );

if( e.getSource( ) == d.b9)

p.t1.setText( (p.t1.getText( ) + d.b9.getlabel( ) );

if( e.getSource( ) == d.bp) {

m = Float.parseFloat(p.t1.getText( ) );

p.t1.setText(" ");

f = 'p';

}

if ( f == 'm') {

n = (m * Float.parseFloat(p.t1.getText( ) ) );

p.t1.setText( String.valueOf(n));

}

if ( f == 'd') {

n = (m / Float.parseFloat(p.t1.getText( ) ) );

p.t1.setText( String.valueOf(n));

} } } }

Example 130 : Demo of Menus

class Testwind extends Frame implements ActionListener {

MenuBar mb;

Menu m1,m2;

TextField t;

MenuItem mi1, mi2,mi3, mi4;

Testwind(String title) {

super(title);

t = new TextField(20);

setLayout(new GridLayout(1, 1));

add(t);

mb = new MenuBar();

m1 = new Menu("study");

mi1 = new MenuItem("MCA");

mi1.addActionListener(this);

m1.add(mi1);

mi2 = new MenuItem("B.Tech");

mi2.addActionListener(this);

m1.add(mi2);

m2 = new Menu("Place");

mi3 = new MenuItem("University");

mi3.addActionListener(this);

m2.add(mi3);

mi4 = new MenuItem("Aff College");

mi4.addActionListener(this);

m2.add(mi4);

setMenuBar(mb);

addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent e) {

setVisible(false);

} } );

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == mi1)

t.setText("Your study is MCA");}

else

if(e.getSource() == mi2)

t.setText("Your study is B.Tech");

else

if(e.getSource() == mi3)

t.setText("You are in Univ");

else

if(e.getSource() == mi4)

t.setText("You are in affl coll");

} }

public class Demomenu extends Applet implements ActionListener {

Button b1,b2;

Testwind w;

public void init() {

b1 = new Button("Display");

add(b1);

b1.addActionListener(this);

b2 = new Button("hide");

add(b2);

b2.addActionListener(this);

w = new Testwind("Student info");

w.setSize(200, 200);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == b1) { w.setVisible(true); } if(e.getSource() == b2) { w.setVisible(false); } }

}

Example 131 : Demo of Panel

class Coursemenu extends Panel {

Checkbox c1, c2,c3;

TextField t1;

CheckboxGroup cg;

Coursemenu( ) {

setLayout( new GridLayout(4,1));

cg = new CheckboxGroup( );

c1 = new Checkbox("Basics", cg, false);

add(c1) ;

c2 = new Checkbox("Web", cg, false);

add(c2) ;

c3 = new Checkbox("graphics", cg, false);

add(c3);

t1 = new TextField(30); add(t1);

} }

class Contentmix extends Panel {

Checkbox i1,i2,i3,i4,i5,i6;

Contentmix( ) {

setLayout( new GridLayout(3,2) );

i1 = new Checkbox("Ms office "); add(i1);

i2 = new Checkbox("oracle"); add(i2);

i3 = new Checkbox("JEE 5.0 "); add(i3);

i4 = new Checkbox(" .NET "); add(i4);

i5 = new Checkbox("Animation "); add(i5);

i6 = new Checkbox("Maya "); add(i6);

} }

public class DemoITschool extends Applet implements ItemListener {

Coursemenu p1; Contentmix p2;

public void init( ) {

setLayout( new GridLayout(1,2) );

p1 = new Coursemenu(); add(p1);

p2 = new Contentmix(); add(p2);

p1.c1.additemListener(this);

p1.c2.additemListener(this);

p1.c3.additemListener(this);

}

public void itemStateChanged(ItemEvent e) {

if( e.getItemSelectable() == p1.c1 ) {

p2.i1.setState(true);

p2.i2.setState(true);

p2.i3.setState(false);

p2.i4.setState(false);

p2.i5.setState(false);

p2.i6.setState(false);

p1.t1.setText("course fee Rs 4500/-");

}

if( e.getItemSelectable() == p1.c2 ) {

p2.i1.setState(false);

p2.i2.setState(false);

p2.i3.setState(true);

p2.i4.setState(true);

p2.i5.setState(false);

p2.i6.setState(false);

p1.t1.setText("course fee Rs 9500/-");

}

if( e.getItemSelectable() == p1.c3 ) {

p2.i1.setState(false);

p2.i2.setState(false);

p2.i3.setState(false);

p2.i4.setState(false);

p2.i5.setState(true);

p2.i6.setState(true);

p1.t1.setText("course fee Rs 14500/-");

}

} }

Example 132 : Demo of Moving Marquee

public class Marquee extends Applet implements Runnable {

int x=0,y=0,w=0;

Thread p = null;

public void init( ) {

x = size().width;

y = size().height;

w = x;

}

public void start( ) {

p = new Thread(this);

p.start();

}

public void run() {

while(true) {

repaint();

x = x-10;

if(x <0 ) x = w;

try {

Thread.sleep(500);

}

catch(InterruptedException e) { }

} }

public void paint(Graphics g) {

g.drawString("java", x,y);

} }

Chapter 11

In document Java Notes (Page 148-189)