我有JTextfield
。现在我想在单击该组件时更改值。例如:分数(2 个大 JTextField
),当我单击其中一个字段时,它会将值从 0:0 增加到 1:0。
我应该实现 MouseListener 还是有一些简单的方法可以做到这一点?在鼠标侦听器中,我只需要重写一个方法 mouseClick
,其他方法将为空。
还有一个问题:我什么时候应该实现MouseListener
? e.getButton()
左按钮总是返回 1,右按钮总是返回 3?
I have 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.
Should I implement MouseListener
or there is some easy way how I can do this? In mouse listener I need override just one method mouseClick
and other method will be empty.
And another question: when should I implement MouseListener
? e.getButton()
return always 1 for left button and 3 for right button?
发布评论
评论(3)
使用
MouseAdapter
< /a>.Use a
MouseAdapter
.JTextComponents 是可聚焦的,查找 FocusListener
JTextComponents are Focusable, look for FocusListener
在您的类上实现
MouseListener
是实现此目的的一种方法,但如果您只想对点击做出反应,则使用扩展MouseAdapter
至于第二个问题,API 文档很好地记录了MouseEvent 的返回值.getButton()。
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 extendingMouseAdapter
As for the second question, the API documentation quite nicely documents the return values of
MouseEvent.getButton()
.