您的位置:首页 > 编程语言 > Java开发

《Java 编程技巧1001条》第413条:使用mouseEnter 和 mouseExit

2017-12-25 08:42 387 查看

《Java 编程技巧1001条》第11章 事件驱动 第413条 使用mouseEnter 和 mouseExit

413 Using mouseEnter and mouseExit
413 使用mouseEnter和mouseExit
You have learned that your applets can handle mouseMove events when a user moves a mouse around the screen. Within your programs, you can use the event coordinates to manually check when a mouse enters a window or control. However, Java has two functions that can do this for you: mouseEnter and mouseExit. Java calls the mouseEnter when a mouse enters a component. Likewise, Java calls the mouseExit when the mouse leaves that component. For example, you might use these methods to display a pop-up help message that tells a user about a particular control or button. The following program, mouseEnterExit.java, demonstrates how your programs can detect when a mouse enters and leaves an applet window:
你已知道了,当用户利用mouse在屏幕上移动mouse光标时,你的应用程序是怎样来处理mouseMove事件的. 在你的程序中,你可以利用mouse事件的坐标来检测一个mouse是进入一个窗口或者是一个控制对象图标. 但是,Java有两个供你使用的功能:mouseEnter和 mouseExit. Java在mouse进入某一成员范围时调用mouseEnter,同样, Java在mouse离开某一成员范围时调用了mouseExit. 例如,你可以用这些方法来显示一个弹出式消息,告诉用户有关的特殊控制或按钮. 以下的mouseEnterExit.java程序说明了你的程序怎样来检测mouse何时进入/退出一个应用程序窗口:
import java.awt.*;
import java.applet.*;
  
public class mouseEnterExit extends Applet {

   public boolean mouseEnter(Event evt, int x, int y)
     {
        System.out.println("mouseEnter");
        return(true);
     }

   public boolean mouseExit(Event evt, int x, int y)
     {
        System.out.println("mouseExit");
        return(true);
     }
 }
The first message will cause the status window (the window that displays the message "mouseEnter") to be in front of the applet. For the mouseEnter and mouseExit methods to work, you must click the applet so that it becomes the front-most window. 
这里的第一个消息将使状态窗口(显示消息"mouseEnter"的窗口)变为应用程序的front窗口. 为使mouseEnter 和 mouseExit 方法工作,你必须点击你的应用程序,使它成为最前端的那个窗口.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: