[英]Change value when mouse is clicked
I have JTextfield
. 我有
JTextfield
。 Now I want to change value when in this component is mouse clicked. 现在,我想在此组件中单击鼠标时更改值。 For example: score (2 big
JTextField
) and when I click to one of these field it increase the value from 0:0 to 1:0. 例如:score(2 big
JTextField
),当我单击这些字段之一时,它将值从0:0增加到1:0。
Should I implement MouseListener
or there is some easy way how I can do this? 我应该实现
MouseListener
还是有一些简单的方法可以做到这一点? In mouse listener I need override just one method mouseClick
and other method will be empty. 在鼠标侦听器中,我只需要覆盖一个方法
mouseClick
,其他方法将为空。
And another question: when should I implement MouseListener
? 另一个问题是:什么时候应该实现
MouseListener
? e.getButton()
return always 1 for left button and 3 for right button? e.getButton()
总是返回1表示左键,3表示右键?
Should I implement MouseListener or there is some easy way how I can do this?
我应该实现MouseListener还是有一些简单的方法可以做到这一点? In mouse listner I need override just one method mouseClick and other method will be empty.
在鼠标列表器中,我只需要重写一个方法mouseClick,其他方法将为空。
Use a MouseAdapter
. 使用
MouseAdapter
。
An abstract adapter class for receiving mouse events.
用于接收鼠标事件的抽象适配器类。 The methods in this class are empty.
此类中的方法为空。 .. Extend this class to create a MouseEvent (including drag and motion events) or/and MouseWheelEvent listener and override the methods for the events of interest.
..扩展此类,以创建MouseEvent(包括拖动和运动事件)或/和MouseWheelEvent侦听器,并覆盖感兴趣事件的方法。
Now I want to change value when in this component is mouse clicked
JTextComponents是Focusable的,请查找FocusListener
Implementing MouseListener
on your class is one way to do it, but if you just want to react to clicks, it's easier to use an anonymous class extending MouseAdapter
在类上实现
MouseListener
是一种方法,但是,如果您只想对单击做出反应,则使用匿名类扩展MouseAdapter
更容易
textField.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// do your thing here
}
});
As for the second question, the API documentation quite nicely documents the return values of MouseEvent.getButton()
. 至于第二个问题,API文档很好地记录了
MouseEvent.getButton()
的返回值 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.