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

《Java 编程技巧1001条》 第406条: 使用mouseUp方法

2017-12-24 20:05 633 查看



《Java 编程技巧1001条》第11章 事件驱动 第406条 使用mouseUp方法

406 Using the mouseUp method
405 使用mouseUp方法 
In the previous tip, you learned about the mouseDown event which Java calls when the user holds down the mouse button. Conversely, Java provides the mouseUp event when the user releases the mouse button. If you are not familiar with writing graphical user interfaces, you may wonder why there are two mouse-click events. A common use of the mouse-down event is to highlight a control, such as a button, that the user has selected. If the user releases the mouse button over the control, the program knows the user has accepted the button. However, if the user moves the mouse off the control before the mouse is released, the program knows the user did not select the button.
在以上TIP中,你已知道了mouseDown方法是用户按住mouse按钮时Java所调用的. 相反地, Java也提供一个mouseUp方法是用户释放mouse按钮时产生的. 如果你不熟悉用图形用户接口编程, 你可能对为什么要两个mouse击键事键感到奇怪. mouseDown方法的普通用途是强调显示(highlight)用户选择的一个控制,如button(按钮). 如果用户在控制上释放了mouse按钮,程序知道用户是接受了button. 如果用户在mouse释放之前
把mouse移开了控制,程序将知道用户没有选择此button.

The following program, mouseUpApplet.java, displays a message to the screen every time the user holds down the mouse button and also when the user releases the button. The program also displays the mouse-screen coordinates for each event:
以下的mouseUpApplet.java程序在每当用户按着mouse button和释放mouse button时在屏幕上显示一个消息. 程序同时也为每一事件显示mouse光标的屏幕坐标:
import java.awt.*;
import java.applet.*;
 
public class mouseUpApplet extends Applet {
   public boolean mouseDown(Event evt, int x, int y)
     {
       System.out.println("Mouse Down (" + x + "," + y + ")");
       return(true);
     }

   public boolean mouseUp(Event evt, int x, int y)
     {
       System.out.println("Mouse Up (" + x + "," + y + ")");
       return(true);
     }
 }
[b]Take time now to run this applet. Next, move and click the mouse both from 
within and outside of the applet window to learn how Java responds to such mouse operations.
现在来运行这一应用程序.然后移动mouse,并在应用块窗口的内部和外部点击mouse键,以了解Java是怎样响应这些mouse操作的.

[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: