【Java精品源码栏目提醒】:网学会员,鉴于大家对Java精品源码十分关注,论文会员在此为大家搜集整理了“【精品】16事件驱动程序设计slide - 其它资料”一文,供大家参考学习!
学习目标 解释事件驱动程序设计的概念 理解事件、事件源和事件类 编写监听器类并编写代码处理事件 会处理ActionEvent,MouseEvent和KeyEvent Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 1 事件驱动程序设计 当发生一个事件时(例如点击按钮或者 移动鼠标)时执行相应的代码。
Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 2public class SimpleEventDemo extends JFrame public SimpleEventDemo JButton jbtOK new JButtonOK setLayoutnew FlowLayout addjbtOK ActionListener listener new OKListener//定义监听器类对象 jbtOK.addActionListenerlistener//注册一个监听器 public static void mainString args JFrame frame new SimpleEventDemo frame.setTitleSimpleEventDemo frame.setVisibletrue class OKListener implements ActionListener //监听器类 public void actionPerformedActionEvent e //事件处理 System.out.println“It is OK” Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 3 事件Event 事件(Event) :程序发生了某些事情的信号。
事件可以是外部行为,例如用户移动了鼠标,点 击了按钮等。
也可以是程序内部动作,例如定时 器。
程序可以选择响应事件或者忽略事件。
事件源源对象source object:能创建一个 事件并触发该事件的组件(例如按钮,面板等) Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 4 Event ClassesJava中每个事件都是一个事件类的实例,事件类的根类是EventObject ActionEvent ContainerEvent AdjustmentEvent FocusEvent MouseEvent EventObject AWTEvent ComponentEvent InputEvent ItemEvent PaintEvent KeyEvent TextEvent WindowEvent ListSelectionEvent Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 5 常见的事件和事件源用户行为 源对象 触发的事件类型点击按钮 JButton ActionEvent点击checkBox JCheckBox ItemEvent ActionEvent点击radioButton JRadioButton ItemEvent ActionEvent在文本框中按回车键 JTextField ActionEvent在comboBox中选择一项 JComboBox ItemEvent ActionEvent窗口打开、关闭等. Window WindowEvent鼠标点击、移动等 Component MouseEvent点击键盘 Component KeyEvent Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 6 动作事件-ActionEvent 按钮点击、在文本框按下回车键都会触发动作事件 ActionEvent
Java为处理动作事件提供接口ActionListener ,接口中的方法为actionPerformed 为组件注册动作监听器的方法为 组件.addActionListener监听器对象 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 7class ActionDemo extends JFrame implements ActionListener ActionDemo JButton redBut new JButtonRed redBut.addActionListenerthis this.addredBut JButton greenBut new JButtonGreen greenBut.addActionListenerthis this.addgreenBut public void actionPerformedActionEvent e ife.getActionCommand.equalsRed//Red按钮 this.getContentPane.setBackgroundColor.RED else ife.getActionCommand.equalsGreen//Green按钮 this.getContentPane.setBackgroundColor.GREEN public static void mainString args ActionDemo frame new ActionDemo frame.setVisibletrue Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 8class TextFieldDemo extends JFrame implements ActionListener JTextField input new JTextField10//文本框 JTextArea display new JTextArea8 10//文本区域 JScrollPane pane new JScrollPanedisplay//滚动条面板 public TextFieldDemo this.addinput BorderLayout.NORTH this.addpane BorderLayout.CENTER input.requestFocus//文本框获得焦点 display.setLineWraptrue//文本区域自动换行 input.addActionListenerthis public void actionPerformedActionEvent e display.appendinput.getText//向文本区域内追加文本 input.selectAll//选中文本框内容 public static void mainString args TextFieldDemo frame new TextFieldDemo frame.pack frame.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE frame.setVisibletrue Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 9 处理 Window Events 在窗口打开、关闭、最大化、最小化时会触发 WindowEvent事件 windowEvent事件由WindowListener处理 WindowListener有7个函数分别对应7种窗口事件 public void windowOpenedWindowEvent e public void windowClosingWindowEvent e public void windowClosedWindowEvent e public void windowIconifiedWindowEvent e public void windowDeiconifiedWindowEvent e public void windowActivatedWindowEvent e public void windowDeactivatedWindowEvent e 为窗口注册windowListener的方法 窗口. addWindowListener事件处理对象 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 10 处理鼠标事件 在组件上发生的鼠标进入、离开、点击、拖动都会触发鼠标事 件。
Java为处理鼠标事件提供两个接口:MouseListener和 MouseMotionListener MouseLinstener能处理5种鼠标事件: 按下鼠标键:mousePressedMouseEvent e 释放鼠标键:mouseReleasedMouseEvent e 点击鼠标键:mouseClickedMouseEvent e 鼠标进入:mouseEnteredMouseEvent e 鼠标离开:mouseExitedMouseEvent e 为组件注册鼠标监听器的方法: 组件.addMouseListener事件处理对象 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 11 事件处理步骤1.定义监听器类,处理事件2.定义组件3.定义监听器类对象4.注册监听器 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 12 实验7 布局管理和事件驱动 掌握容器类控件的使用 掌握常用布局管理器的应用 理解事件驱动程序设计,能够编写常见 的事件响应程序 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 13 内容1 控件的ActionEvent事件 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 14 内容 2 窗口关闭事件 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 15 内容 3 鼠标进入事件 Liang Introduction to
Java Programming Sixth Edition c 2007 Pearson Education Inc. All rights reserved. 0-13-222158-6 16