《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 方法工作,你必须点击你的应用程序,使它成为最前端的那个窗口.
相关文章推荐
- 《Java 编程技巧1001条》第415条:使用keyDown方法
- 《Java 编程技巧1001条》 第400条: 使用数组复制
- 《Java 编程技巧1001条》 第420...条: 使用handle event 方法
- 《Java 编程技巧1001条》第416条:使用keyUp方法
- 《Java 编程技巧1001条》 第397+398条:了解和使用快速排序
- 《Java 编程技巧1001条》 第405条: 使用mouseDoun方法
- 《Java 编程技巧1001条》 第393条: 使用2分查找
- 《Java 编程技巧1001条》 第408条: 使用mouseMove方法,
- 《Java 编程技巧1001条》 第406条: 使用mouseUp方法
- 《Java 编程技巧1001条》 第399条:对原始类型数组使用数组复制
- 《Java 编程技巧1001条》 第395与396条 :了解和使用冒泡排序
- 《Java 编程技巧1001条》第409...条: 使用crickCount方法,
- 《Java 编程技巧1001条》 第407条:使用mouseDrag方法,
- 《Java 编程技巧1001条》 第386条: 声明多维数组
- 《Java 编程技巧1001条》 第384条: 建立对象数组..
- 《Java 编程技巧1001条》第401条:数组中存不同对象
- 《Java 编程技巧1001条》 第382+283条: 了解数组的长度和引用
- 《Java 编程技巧1001条》第402-406条: 了解事件驱动程序,
- 《Java 编程技巧1001条》 第403条: 了解事件类
- 【编程技巧】java不使用第三个变量处理两个变量的交换