360 likes | 616 Views
第七章 AWT 组件及事件处理(五). 本次课教学目标. 了解窗口事件的处理,熟练使用匿名类来处理窗口的关闭事件。 能处理鼠标按下、释放、移动、拖动等事件 熟悉焦点事件的处理 掌握键盘事件的处理方法. 窗口事件. Frame 是 Window 的子类,凡是 Window 子类创建的对象都可以发生 WindowEvent 类型事件,即窗口事件。 对应的监视器接口为: WindowListener 窗口注册监视器的方法为: addWindowListener( ); 监视器接口中的方法共有 7 个,分别为:
E N D
本次课教学目标 • 了解窗口事件的处理,熟练使用匿名类来处理窗口的关闭事件。 • 能处理鼠标按下、释放、移动、拖动等事件 • 熟悉焦点事件的处理 • 掌握键盘事件的处理方法
窗口事件 • Frame是Window的子类,凡是Window子类创建的对象都可以发生WindowEvent类型事件,即窗口事件。 • 对应的监视器接口为:WindowListener • 窗口注册监视器的方法为:addWindowListener( ); • 监视器接口中的方法共有7个,分别为: • windowActivated(WindowEvent e) • windowDeactivated(WindowEvent e) • windowClosed(WindowEvent e) • windowClosing(WindowEvent e) • windowIconified(WindowEvent e) • windowDeiconified(WindowEvent e) • windowOpened(WindowEvent e)
例1:如下图所示,处理窗口事件,在窗口的文本区记录触发的事件.例1:如下图所示,处理窗口事件,在窗口的文本区记录触发的事件.
import java.awt.*; import java.awt.event.*; class MyFrame extends Frame implements WindowListener { TextArea text; MyFrame() { setBounds(100,100,200,300); setVisible(true); text=new TextArea(); add(text,BorderLayout.CENTER); addWindowListener(this); validate(); } public void windowActivated(WindowEvent e) { text.append("\n我被激活"); validate(); } public void windowDeactivated(WindowEvent e) { text.append("\n我不是激活状态了"); setBounds(0,0,400,400); validate(); }
public void windowClosing(WindowEvent e) { text.append("\n窗口正在关闭呢"); dispose(); } public void windowClosed(WindowEvent e) { System.out.println("程序结束运行"); System.exit(0); } public void windowIconified(WindowEvent e) { text.append("\n我图标化了"); } public void windowDeiconified(WindowEvent e) { text.append("\n我撤消图标化"); setBounds(0,0,400,400); validate(); } public void windowOpened(WindowEvent e){} } public class Example7_25 { public static void main(String args[]) { new MyFrame(); } } 接口中的所有方法都必须实现
当一个类实现一个接口时,即使不准备处理某个方法,也必须给出接口中所有方法的实现。当一个类实现一个接口时,即使不准备处理某个方法,也必须给出接口中所有方法的实现。 • 适配器可以代替接口来处理事件,当接口中的方法多于一个时,Java相应地提供一个适配器类,如WindowAdapter。 • 适配器已经实现了相应的接口。因此,可以使用其子类创建的对象做监视器,在子类中重写所需要的接口方法即可。
import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { TextArea text; Boy police; MyFrame(String s) { super(s); police=new Boy(this); setBounds(100,100,200,300); setVisible(true); text=new TextArea(); add(text,BorderLayout.CENTER); addWindowListener(police); validate(); } } 例2:使用适配器作监视器,只处理窗口关闭事件和激活事件。
class Boy extends WindowAdapter { MyFrame f; public Boy(MyFrame f) { this.f=f; } public void windowActivated(WindowEvent e) { f.text.append("\n我被激活"); } public void windowClosing(WindowEvent e) { System.exit(0); } } public class Example7_26 { public static void main(String args[]) { new MyFrame("窗口"); } }
例3:将上例改成使用匿名类的实例做监视器。例3:将上例改成使用匿名类的实例做监视器。 import java.awt.*; import java.awt.event.*; class MyFrame extends Frame { TextArea text; MyFrame(String s) { super(s); setBounds(100,100,200,300); setVisible(true); text=new TextArea(); add(text,BorderLayout.CENTER); addWindowListener(new WindowAdapter() { public void windowActivated(WindowEvent e) { text.append("\n我被激活"); } public void windowClosing(WindowEvent e) { System.exit(0); } } ); validate(); } } public class Example7_27 { public static void main(String args[]) { new MyFrame("窗口"); } }
鼠标事件 • 任何组件上都可以发生鼠标事件 • 鼠标事件的类型是MouseEvent,即当发生鼠标事件时,MouseEvent类自动创建一个事件对象。 • Java以MouseListener和MouseMotionListener接口当成鼠标事件的监视器接口 • MouseListener接口主要是用来监视下列5项事件的发生: • 单击鼠标按钮(包括鼠标左键或右键) • 鼠标的指针进入事件源 • 鼠标的指针移出事件源 • 按下鼠标的任一个按键 • 放开鼠标被按下的按键
MouseListener接口里声明了五个用来处理不同事件的方法MouseListener接口里声明了五个用来处理不同事件的方法
MouseMotionListener接口则用来监视下列两个事件的发生 : • 当鼠标在事件源上方移动(move)时。 • 当鼠标在事件源上方拖动(drag)时(所谓的“拖动”是指按住鼠标按钮不放移动鼠标)。 • MouseMotionListener接口里声明了两个方法,用来处理“移动”与“拖动”鼠标的事件。 • mouseDragged(MouseEvent e)//负责处理在组件上拖动鼠标触发的鼠标事件 • mouseMoved(MouseEvent e)//负责处理在组件上移动鼠标触发的鼠标事件。
MouseEvent类中的重要方法: • public Point getPoint():获取鼠标在事件源坐标系中的x、y坐标。 • public int getX():获取鼠标在事件源坐标系中的x坐标。 • public int getY():获取鼠标在事件源坐标系中的y坐标。 • public int getModifiers( ):获取鼠标的左键或右键。鼠标的左键和右键分别使用InputEvent类中的常量BUTTON1-MASK和BUTTON3-MASK来表示。 • public int getClickCount():获取单击的次数。 • public void getSource( ):获取发生鼠标事件的事件源。
例4:测试按钮和画布的鼠标事件。效果如下图所示。例4:测试按钮和画布的鼠标事件。效果如下图所示。
import java.awt.*; import java.awt.event.*; import java.applet.*; public class UseMouseEvent extends Applet implements MouseListener, MouseMotionListener { Button btn; public void init() {btn = new Button("演示鼠标事件"); add(btn); btn.addMouseListener(this); //给按钮添加鼠标事件btn.addMouseMotionListener(this); //给按钮添加鼠标移动事件 this.addMouseListener(this); //给画布添加鼠标事件 this.addMouseMotionListener(this); //给画布添加鼠标移动事件 }
public void mouseClicked(MouseEvent e)//单击事件 {Point p = new Point(); if(e.getSource() == btn) { if(e.getClickCount() == 1) { btn.setLabel("单击鼠标"); } else if(e.getClickCount() == 2) {btn.setLabel("双击鼠标"); } } else {if(e.getClickCount() == 1) {p = e.getPoint(); showStatus(p.x + "," + p.y + "单击鼠标");//显示在状态条下 } else if(e.getClickCount() == 2) {p = e.getPoint(); showStatus(p.x + "," + p.y + "双击鼠标"); } } }
public void mouseEntered(MouseEvent e)//进入事件 {if(e.getSource() == btn) btn.setLabel("进入Button"); else showStatus("进入Applet"); } public void mouseExited(MouseEvent e) //离开事件 { if(e.getSource() == btn) btn.setLabel("退出Button"); else showStatus("退出Applet"); } public void mousePressed(MouseEvent e) //按下事件 {if(e.getSource() == btn) btn.setLabel("按下鼠标"); else showStatus("按下鼠标"); }
public void mouseReleased(MouseEvent e) //释放事件 {if(e.getSource() == btn) btn.setLabel("松开鼠标"); else showStatus("松开鼠标"); } public void mouseMoved(MouseEvent e) //移动事件 {if(e.getSource() == btn) btn.setLabel("移动鼠标"); else showStatus("移动鼠标,新位置" + e.getX() + "," + e.getY()); } public void mouseDragged(MouseEvent e) //拖动事件 {if(e.getSource() == btn) btn.setLabel("拖动鼠标"); else showStatus("拖动鼠标"); } }
例5:当在画布上按下鼠标左键时,在鼠标指针位置处绘制一个圆;当按下鼠标右键时,在鼠标指针位置处绘制一个矩形;当鼠标指针退出画布时,清除绘制的全部图形。例5:当在画布上按下鼠标左键时,在鼠标指针位置处绘制一个圆;当按下鼠标右键时,在鼠标指针位置处绘制一个矩形;当鼠标指针退出画布时,清除绘制的全部图形。
import java.awt.*; import java.awt.event.*; class MyCanvas extends Canvas implements MouseListener { int left=-1,right=-1; int x=-1,y=-1; MyCanvas() { setBackground(Color.cyan) ; addMouseListener(this); } public void paint(Graphics g) { if(left==1) { g.drawOval(x-10,y-10,20,20); } else if(right==1) { g.drawRect(x-8,y-8,16,16); } }
publicvoid mousePressed(MouseEvent e) { x=e.getX(); y=e.getY(); if(e.getModifiers()==InputEvent.BUTTON1_MASK) { left=1;right=-1; repaint(); } else if(e.getModifiers()==InputEvent.BUTTON3_MASK) { right=1; left=-1; repaint(); } } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) { left=-1;right=-1; repaint(); } public void mouseClicked(MouseEvent e){} MouseListener接口中的5个方法务必全部实现
public void update(Graphics g) { if(left==1||right==1) { paint(g); } else { super.update(g); } } } public class Example7_29 { public static void main(String args[]) { Frame f=new Frame(); f.setBounds(100,100,200,200);f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(new MyCanvas(),BorderLayout.CENTER); f.validate(); } }
焦点事件 • 组件可以触发焦点事件----FocusEvent • 为组件增加焦点事件监视器:addFocusListener • 使用FocusListener接口处理焦点事件 • FocuListener接口的方法: • public void focusGained(FocusEvent e) //负责处理组件获得焦点时触发的事件 • public void focusLost(FocusEvent e) //负责处理组件失去焦点时触发的事件 • 组件可调用requestFocusInWindow()获得焦点
例6:监视组件上的焦点事件,当组件获得焦点时组件的颜色变成蓝色,当失去焦点时,组件的颜色变成红色。例6:监视组件上的焦点事件,当组件获得焦点时组件的颜色变成蓝色,当失去焦点时,组件的颜色变成红色。
import java.awt.*; import java.awt.event.*; class MyWindow extends Frame implements FocusListener { TextField text; Button button; MyWindow(String s) { super(s); text=new TextField(10); button=new Button("按钮"); text.requestFocusInWindow(); setLayout(new FlowLayout()); add(text); add(button); text.addFocusListener(this); button.addFocusListener(this); setBounds(100,100,150,150); setVisible(true); validate(); }
public void focusGained(FocusEvent e) { Component com=(Component)e.getSource(); com.setBackground(Color.blue); if(com==text) text.setText(null); } public void focusLost(FocusEvent e) {Component com=(Component)e.getSource(); com.setBackground(Color.red); } } public class Example7_32 { public static void main(String args[]) { MyWindow win=new MyWindow("窗口"); } }
键盘事件 • 当按下、释放或敲击键盘上一个键时触发键盘事件----KeyEvent • 事件源使用addKeyListener方法获得监视器 • 使用KeyListener接口处理键盘事件, KeyListener接口中有3个方法: • public void keyPressed(KeyEvent e) • public void keyTyped(KeyEvent e) • public void KeyReleased(KeyEvent e)
KeyEvent类中的方法: • public int getKeyCode() //返回键码值。 • public char getKeyChar() //返回键上的字符。 • public int getModifiers() //返回InputEvent类的类常量:ALT_MASK、CTRL_MASK、SHIFT_MASK,用以处理组合键事件。
例7:按CTRL+X组合键改变按钮大小,按SHIFT+X来改变按钮的位置。例7:按CTRL+X组合键改变按钮大小,按SHIFT+X来改变按钮的位置。 import java.awt.*; import java.awt.event.*; class Win extends Frame implements KeyListener { Button b; Win() {setLayout(new FlowLayout()); b=new Button("我是一个按钮,"); b.addKeyListener(this); add(b); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); setBounds(10,10,300,300); setVisible(true); validate(); }
public void keyPressed(KeyEvent e) { Button button=(Button)e.getSource(); int x=0,y=0,w=0,h=0; x=button.getBounds().x; y=button.getBounds().y; w=button.getBounds().width; h=button.getBounds().height; if(e.getModifiers()==InputEvent.SHIFT_MASK&&e.getKeyCode()==KeyEvent.VK_X) { button.setLocation(y,x); } else if(e.getModifiers()==InputEvent.CTRL_MASK&&e.getKeyCode()==KeyEvent.VK_X) { button.setSize(h,w); } }
public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} } public class Example7_34 { public static void main(String args[]) { Win win=new Win(); } }
import java.awt.*; import java.awt.event.*; class Win extends Frame implements KeyListener { TextField text[]=new TextField[3]; Win() { addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); setLayout(new FlowLayout()); for(int i=0;i<3;i++) { text[i]=new TextField(6); text[i].addKeyListener(this); add(text[i]); } text[0].requestFocusInWindow(); setBounds(10,10,300,300); setVisible(true); validate(); }
public void keyPressed(KeyEvent e) { TextField text=(TextField)e.getSource(); if(text.getCaretPosition()>=5) { text.transferFocus(); } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} } public class Example7_35 { public static void main(String args[]) { Win win=new Win(); } }
实训十八 窗口事件、鼠标事件、焦点事件、键盘事件的处理 上机目的:巩固掌握窗口事件、鼠标事件、焦点事件、键盘事件的处理。 上机内容: 1、调试教材7.28、7.30、7.33。 2、完成教材P212的第12、13、15题。