《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming

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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第四章 MCS-51单片机存储器的扩展.ppt
- 《网络安全设计》 第一章 安全设计简介.ppt
- 《网络安全设计》 第二章 创建网络安全计划.ppt
- 《网络安全设计》 第三章 确定网络安全威胁.ppt
- 《网络安全设计》 第四章 分析安全风险.ppt
- 《网络安全设计》 第五章 创建物理资源安全设计.ppt
- 《网络安全设计》 第六章 创建计算机安全设计.ppt
- 《网络安全设计》 第七章 创建账户安全设计.ppt