Java Event Handling: MouseListener, KeyListener, and BorderLayout, Lab Reports of Computer Science

Java code examples for handling mouse and keyboard events using mouselistener and keylistener interfaces, as well as arranging components using borderlayout. The examples demonstrate the use of these interfaces through the creation of simple java applications.

Typology: Lab Reports

Pre 2010

Uploaded on 08/16/2009

koofers-user-1ky-1
koofers-user-1ky-1 🇺🇸

10 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS402
Java Handout For Java Lab3
Event Driven Help
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Java Event Handling: MouseListener, KeyListener, and BorderLayout and more Lab Reports Computer Science in PDF only on Docsity!

CS

Java Handout For Java Lab

Event Driven Help

Methods of interface

MouseListener

  • public void mousePressed( MouseEvent event )
    • Called when a mouse button is pressed with the mouse cursor on a

component.

  • public void mouseClicked( MouseEvent event )
    • Called when a mouse button is pressed and released on a component

without moving the mouse cursor.

  • public void mouseReleased( MouseEvent event )
    • Called when a mouse button is released after being pressed. This event is

always preceded by a mousePressed event.

  • public void mouseEntered( MouseEvent event )
    • Called when the mouse cursor enters the bounds of a component.
  • public void mouseExited( MouseEvent event )
    • Called when the mouse cursor leaves the bounds of a component.

Example: MouseDetails

1 // MouseDetails.java 2 // Demonstrating mouse clicks and 3 // distinguishing between mouse buttons. 4 5 // Java core packages 6 import java.awt.; 7 import java.awt.event.; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 public class MouseDetails extends JFrame { 13 private int xPos, yPos; 14 15 // set title bar String, register mouse listener and size 16 // and show window 17 public MouseDetails() 18 { 19 super( "Mouse clicks and buttons" ); 20 21 addMouseListener( new MouseClickHandler() );

Example: MouseDetails

22 23 setSize( 350, 150 ); 24 setVisible( true ); 25 } 26 27 // draw String at location where mouse was clicked 28 public void paint( Graphics g ) 29 { 30 // call superclass's paint method 31 super.paint( g ); 32 33 g.drawString( "Clicked @ [" + xPos + ", " + yPos + "]", 34 xPos, yPos ); 35 } 36 37 // execute application 38 public static void main( String args[] ) 39 { 40 MouseDetails application = new MouseDetails(); 41 42 application.setDefaultCloseOperation( 43 JFrame.EXIT_ON_CLOSE );

Example: MouseDetails

66 67 // left mouse button 68 else 69 title += " with left mouse button"; 70 71 setTitle( title ); // set title bar of window 72 repaint(); 73 } 74 75 } // end private inner class MouseClickHandler 76 77 } // end class MouseDetails

Keyboard Event Handling

  • Interface KeyListener
  • Handles key events
  • Generated when keys on keyboard are pressed and released
  • KeyEvent
  • Contains virtual key code that represents key

Example: KeyDemo

21 // set up JTextArea 22 textArea = new JTextArea( 10, 15 ); 23 textArea.setText( "Press any key on the keyboard..." ); 24 textArea.setEnabled( false ); 25 getContentPane().add( textArea ); 26 27 // allow frame to process Key events 28 addKeyListener( this ); 29 30 setSize( 350, 100 ); 31 setVisible( true ); 32 } 33 34 // handle press of any key 35 public void keyPressed( KeyEvent event ) 36 { 37 line1 = "Key pressed: " + 38 event.getKeyText( event.getKeyCode() ); 39 setLines2and3( event ); 40 } 41

Example: KeyDemo

42 // handle release of any key 43 public void keyReleased( KeyEvent event ) 44 { 45 line1 = "Key released: " + 46 event.getKeyText( event.getKeyCode() ); 47 setLines2and3( event ); 48 } 49 50 // handle press of an action key 51 public void keyTyped( KeyEvent event ) 52 { 53 line1 = "Key typed: " + event.getKeyChar(); 54 setLines2and3( event ); 55 } 56 57 // set second and third lines of output 58 private void setLines2and3( KeyEvent event ) 59 { 60 line2 = "This key is " + 61 ( event.isActionKey()? "" : "not " ) + 62 "an action key";

Example: KeyDemo

BorderLayout

• BorderLayout Arranges components into five regions

  • NORTH (top of container)
  • SOUTH (bottom of container)
  • EAST (left of container)
  • WEST (right of container)
  • CENTER (center of container)

Example: BorderLayoutDemo

22 super( "BorderLayout Demo" ); 23 24 layout = new BorderLayout( 5, 5 ); 25 26 // get content pane and set its layout 27 Container container = getContentPane(); 28 container.setLayout( layout ); 29 30 // instantiate button objects 31 buttons = new JButton[ names.length ]; 32 33 for ( int count = 0; count < names.length; count++ ) { 34 buttons[ count ] = new JButton( names[ count ] ); 35 buttons[ count ].addActionListener( this ); 36 } 37 38 // place buttons in BorderLayout; order not important 39 container.add( buttons[ 0 ], BorderLayout.NORTH ); 40 container.add( buttons[ 1 ], BorderLayout.SOUTH ); 41 container.add( buttons[ 2 ], BorderLayout.EAST ); 42 container.add( buttons[ 3 ], BorderLayout.WEST ); 43 container.add( buttons[ 4 ], BorderLayout.CENTER );

Example: BorderLayoutDemo

45 setSize( 300, 200 ); 46 setVisible( true ); 47 } 48 49 // handle button events 50 public void actionPerformed( ActionEvent event ) 51 { 52 for ( int count = 0; count < buttons.length; count++ ) 53 54 if ( event.getSource() == buttons[ count ] ) 55 buttons[ count ].setVisible( false ); 56 else 57 buttons[ count ].setVisible( true ); 58 59 // re-layout the content pane 60 layout.layoutContainer( getContentPane() ); 61 } 62 63 // execute application 64 public static void main( String args[] ) 65 {