《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]
相关文章推荐
- 《Java 编程技巧1001条》 第407条:使用mouseDrag方法,
- 《Java 编程技巧1001条》第415条:使用keyDown方法
- 《Java 编程技巧1001条》 第405条: 使用mouseDoun方法
- 《Java 编程技巧1001条》第416条:使用keyUp方法
- 《Java 编程技巧1001条》 第420...条: 使用handle event 方法
- 《Java 编程技巧1001条》 第408条: 使用mouseMove方法,
- 《Java 编程技巧1001条》第409...条: 使用crickCount方法,
- 《Java 编程技巧1001条》 第404条: 了解实用方法
- 绝对使用JavaMyEclipse使用方法,不会让你失望....提高编程技巧和速度
- 绝对使用JavaEclipse使用方法,不会让你失望....提高编程技巧和速度
- 《Java 编程技巧1001条》 第399条:对原始类型数组使用数组复制
- 《Java 编程技巧1001条》 第397+398条:了解和使用快速排序
- 《Java 编程技巧1001条》 第395与396条 :了解和使用冒泡排序
- 《Java 编程技巧1001条》第410条: 检测双击的另一种方法
- 《Java 编程技巧1001条》第413条:使用mouseEnter 和 mouseExit
- 《Java 编程技巧1001条》 第393条: 使用2分查找
- 《Java 编程技巧1001条》第418条:检测多个修饰键的另一方法
- 《Java 编程技巧1001条》 第400条: 使用数组复制
- java.sql.resultset方法与使用技巧
- java多线程编程之使用Synchronized关键字同步类方法