[英]How can I listen my all components with only one event?
Here is the problem, I have 3 textfield, 3 button and 1 label. 这是问题,我有3个文本字段,3个按钮和1个标签。 Their text are text1, text2 text3, but1,but2,but3.
他们的文字是text1,text2 text3,but1,but2,but3。
I give you an example about what I want to do; 我举一个关于我想做什么的例子; When I double click on a button, button will change label's text as button's text.
当我双击按钮时,按钮会将标签的文本更改为按钮的文本。 I mean when I double click to but2, label's text should be but2.
我的意思是当我双击到but2时,标签的文字应该是but2。
I can do this with that code; 我可以用这个代码做到这一点;
MouseAdapter ml = new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (me.getClickCount()==2) {
jLabel1.setText(jButton1.getText);
}
}
};
jButton1.addMouseListener(ml);
So It works but it works only for jButton1. 所以它可以工作,但它只适用于jButton1。 I have to write different mouseListener's for all components(textfields and jbuttons).
我必须为所有组件(文本字段和jbuttons)编写不同的mouseListener。 How can I do this with one listener?
我怎么能用一个听众做到这一点? or one event ?
还是一件事? Do you have any idea?
你有什么主意吗?
Take a look at MouseEvent#getSource
... although, to be honest, if you're using JButton
s you shouldn't be using a MouseListener
, but ActionListener
instead. 看一下
MouseEvent#getSource
...虽然,老实说,如果你使用的是JButton
你不应该使用MouseListener
,而应该使用ActionListener
。 Also, generally speaking, most users won't double click a JButton
as it's not intuitive for them to do so, buttons only need a single click to activate 另外,一般来说,大多数用户不会双击
JButton
因为它们不直观,按钮只需要点击一下即可激活
Remember, buttons can be activated by the keyboard as well, which MouseListener
won't be notified about 请记住,键盘也可以激活按钮,不会通知
MouseListener
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.