⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right5_2_3_2.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top"> <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"><span class="pt9-black"><font color="000099">例5.10             </font><br>              import java.awt.*;<br>              import java.awt.event.*;<br>                public class ThreeListener implements MouseMotionListener,MouseListener,WindowListener             { <br>                <font color="339900">//实现了三个接口</font><br>                private Frame f;<br>                private TextField tf; <br>                public static void main(String args[])<br>                {<br>                 ThreeListener two = new ThreeListener();<br>                 two.go(); } </span>             <p class="pt9-black">     public void go() {<br>                  f = new Frame(&quot;Three listeners example&quot;); <br>                  f.add(new Label(&quot;Click and drag the mouse&quot;),&quot;North&quot;);<br>                  tf = new TextField(30);<br>                  f.add(tf,&quot;South&quot;); <font color="339900">//使用缺省的布局管理器</font><br>                  f.addMouseMotionListener(this); <font color="339900">//注册监听器MouseMotionListener</font><br>                  f.addMouseListener(this); <font color="339900">//注册监听器MouseListener</font><br>                  f.addWindowListener(this); <font color="339900">//注册监听器WindowListener</font><br>                  f.setSize(300,200);<br>                  f.setVisible(true); <br>                    }</p>            <p class="pt9-black">     public void mouseDragged (MouseEvent e)               { <br>                  <font color="339900">//实现mouseDragged方法</font><br>                  String s = &quot;Mouse dragging : X=&quot;+e.getX()+&quot;Y               = &quot;+e.getY();<br>                  tf.setText(s);<br>                    }<br>                  public void mouseMoved(MouseEvent e){} <br>                  <font color="339900">//对其不感兴趣的方法可以方法体为空</font><br>                  public void mouseClicked(MouseEvent e){}<br>                  public void mouseEntered(MouseEvent e){<br>                    String s = &quot;The mouse entered&quot;;<br>                    tf.setText(s);<br>                      }</p>            <p class="pt9-black">    public void mouseExited(MouseEvent e){<br>                    String s = &quot;The mouse has left the building&quot;;<br>                    tf.setText(s);<br>                      } <br>              <br>                  public void mousePressed(MouseEvent e){}<br>                  public void mouseReleased(MouseEvent e){ }<br>                  public void windowClosing(WindowEvent e) { <br>                  <font color="339900">//为了使窗口能正常关闭,程序正常退出,需要实现windowClosing方法</font><br>                    System.exit(1);<br>                      }</p>            <p class="pt9-black">    public void windowOpened(WindowEvent e) {}               <br>                  <font color="339900">//对其不感兴趣的方法可以方法体为空</font><br>                  public void windowIconified(WindowEvent e) {}<br>                  public void windowDeiconified(WindowEvent e) {}<br>                  public void windowClosed(WindowEvent e) {}<br>                  public void windowActivated(WindowEvent e) { }<br>                  public void windowDeactivated(WindowEvent e) {}</p>            <p class="pt9-black">    }</p>            <p><span class="pt9-black">   上例中有如下几个特点:<br>                1.可以声明多个接口,接口之间用逗号隔开。<br>                  ……implements MouseMotionListener, MouseListener, WindowListener;<br>                <br>                2.可以由同一个对象监听一个事件源上发生的多种事件:<br>                f.addMouseMotionListener(this);<br>                f.addMouseListener(this);<br>                f.addWindowListener(this);<br>                则对象f 上发生的多个事件都将被同一个监听器接收和处理。<br>              <br>                3.事件处理者和事件源处在同一个类中。本例中事件源是Frame f,事件处理者是类ThreeListener,其中事件源Frame               f是类ThreeListener的成员变量。<br>              <br>                4.可以通过事件对象获得详细资料,比如本例中就通过事件对象获得了鼠标发生时的坐标值。<br>                public void mouseDragged(MouseEvent e) {<br>                 String s=&quot;Mouse dragging :X=&quot;+e.getX()+&quot;Y=&quot;+e.getY();<br>                 tf.setText(s);<br>                }<br>              <br>               <font color="000099"> Java语言类的层次非常分明,因而只支持单继承,为了实现多重继承的能力,Java用接口来实现,一个类可以实现多个接口,这种机制比多重继承具有更简单、灵活、更强的功能。在AWT中就经常用到声明和实现多个接口。记住无论实现了几个接口,接口中已定义的方法必须一一实现,如果对某事件不感兴趣,可以不具体实现其方法,而用空的方法体来代替。但却必须所有方法都要写上。<br>              </font> </span> </p>            <p class="pt9-black">  </p>          </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -