










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











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() );
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 );
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
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
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: 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 {