Chapter 13 GUI Objects and Event-Driven Programming 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 13-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 1 Chapter 13 GUI Objects and Event-Driven Programming
Chapter 13 Objectives After, you have read and studied this chapter, you shoula be able to Write GUI application programs using Frame, Dialog, and Button objects from the java. awt package e Write Gui application programs with menus using Menu MenuItem and menu bar objects from the java. awt package e Write event-driven programs using Java's delegation based event model e Write GUI application programs that process mouse events e Understand how the sketchPad class introduced in Chapter 1 is implemented e Run applets as applications C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 2 Chapter 13 Objectives After you have read and studied this chapter, you should be able to Write GUI application programs using Frame, Dialog, and Button objects from the java.awt package. Write GUI application programs with menus using Menu, MenuItem, and MenuBar objects from the java.awt package. Write event-driven programs using Java’s delegationbased event model. Write GUI application programs that process mouse events. Understand how the SketchPad class introduced in Chapter 1 is implemented. Run applets as applications
GUi Objects ATesting Menu 回区 MenuBar File Edit Cut Menu Menuitem Paste Let's Play HiLo Enter your guess TextField Dialog Label You can make up to 6 guesses Let' s have fun with the hilo game Button Guess Frame C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 13-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 3 GUI Objects MenuBar MenuItem TextField Label Button Frame Dialog Menu
Interacting with Buttons r Place two buttons labeled oK and cancel on a frame r Change the frame title when either button is clicked 感 You clicked oK 回x CANCEL myfirstframe C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 13-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 4 Interacting with Buttons Place two buttons labeled OK and CANCEL on a frame. Change the frame title when either button is clicked
Creating a Frame object 150 200 300 import Java. awt private static final int FRAME WIDTH 300; private static final int FRAME HEIGHT 200 private static final int FRAME X ORIGIN private static final int FRAME Y ORIGIN= 250i setsize FRAME WIDTH, FRAME HEIGHT rEsizable( fa etritle (Program MyFirstErame")i setlocation( FRAME X ORIGIN, FRAME Y ORIGIN C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 5 Creating a Frame Object import java.awt.*; class MyFirstFrame extends Frame { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 200; private static final int FRAME_X_ORIGIN = 150; private static final int FRAME_Y_ORIGIN = 250; public MyFirstFrame( ) { setSize ( FRAME_WIDTH, FRAME_HEIGHT ); setResizable ( false ); setTitle ( "Program MyFirstFrame" ); setLocation ( FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); } } 150 250 300 200
Placing a button on a Frame 感 Program MyFirstFrame 回 150 < 100 30 60 setLayout( null )i okButton new Button(oK")i okButton setBounds( 100, 150, 60, 30)i add( okButton i C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 6 Placing a Button on a Frame setLayout( null ); okButton = new Button( “OK” ); okButton.setBounds( 100, 150, 60, 30); add( oKButton ); 100 150 60 30
Handling action Events-Action Listener import java. awt event. *i class MyFirstFrame extends Frame implements ActionListener Declare MyF irstFrame as an ActionListene public MyFirstFrame() Register MyF irstFrame as the action listener of both buttons cancelButton addActionListener( this okButton addActionListener( this C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 13-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 7 Handling Action Events - ActionListener import java.awt.event.*; class MyFirstFrame extends Frame implements ActionListener { . . . } public MyFirstFrame ( ) { . . . cancelButton.addActionListener( this ); okButton.addActionListener( this ); . . . } Declare MyFirstFrame as an ActionListener. Register MyFirstFrame as the action listener of both buttons
Handling action Events-action Performed public void actionPerformed( ActionEvent event Button clickedButton =(Button) event. getsource()i f(clickedButton = cancelButton) setTitle( You clicked CANCEL") else( //the event source is okButton setTitle( You clicked OK") Define the action Performed method in the class that implements the actionlistener interface C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 8 Handling Action Events - actionPerformed public void actionPerformed( ActionEvent event ) { Button clickedButton = (Button) event.getSource(); if (clickedButton == cancelButton) { setTitle( "You clicked CANCEL" ); } else { //the event source is okButton setTitle( "You clicked OK" ); } } Define the actionPerformed method in the class that implements the ActionListenerinterface
Handling Window Events class ProgramTerminator implements Windowlistener public void windowClosing( WindowEvent event System. exit(0) public void windowActivated( WindowEvent event ) t public void windowClosed WindowEvent event ) public void window Deactivated( WindowEvent event public void window Deiconified( WindowEvent event public void windowIconified( WindowEvent event ) I public void windowopened WindowEvent event I C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 9 Handling Window Events class ProgramTerminator implements WindowListener { public void windowClosing( WindowEvent event ) { System.exit(0); } public void windowActivated ( WindowEvent event ) { } public void windowClosed ( WindowEvent event ) { } public void windowDeactivated( WindowEvent event ) { } public void windowDeiconified( WindowEvent event ) { } public void windowIconified ( WindowEvent event ) { } public void windowOpened ( WindowEvent event ) { } }
Adding menus to a frame Menu fileMenu new Menu(File) Create a Menu object MenuItem menuItem new MenuItem(Open. Create a menuitem item addActionListener( this object, associate an fileMenu. add( item )i action listener to it. and add it to the Menu object MenuBar menuBar new MenuBar()i Create a menu Bar object setMenuBar( menuBar and add it to a frame Add menuBar. add( filemenu )i Menu objects to it C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 13-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 10 Adding Menus to a Frame Menu fileMenu = new Menu( “File” ); . . . MenuBar menuBar = new MenuBar( ); setMenuBar( menuBar ); menuBar.add( fileMenu ); Create a MenuItem object, associate an action listener to it, and add it to the Menu object. Create a MenuBar object and add it to a frame. Add Menu objects to it. MenuItem menuItem = new MenuItem( “Open…” ); item.addActionListener( this ); fileMenu.add( item ); . . . Create a Menu object