[英]Can two JButton have the same mouse clicked event function?
I am developing a calculator in Java language.我正在用 Java 语言开发一个计算器。 The problem is that, i put ten buttons for digits(0,1,2..9) and i want that when i clicked one of them, all perform the same mouse clicked function.
问题是,我为数字(0,1,2..9)放置了十个按钮,我希望当我单击其中一个按钮时,都执行相同的鼠标单击 function。 Is it possible?
可能吗? In netbeans, it does not let me do that, or i couldnt achieve.
在 netbeans 中,它不允许我这样做,或者我无法实现。 Thank you for helping.
感谢您的帮助。
Yes.是的。 Add the same listener to both buttons you are using.
为您正在使用的两个按钮添加相同的侦听器。 For example, suppose you are using actionListener then:
例如,假设您正在使用 actionListener 然后:
public class ListenerClass implements Action{
@override
public void actionPerformed(ActionEvent e) {
//here retrieve information on which button has generated the event
}
}
ListenerClass listener = new ListenerClass();
JButton first = new JButton();
JButton second = new JButton();
first.addActionListener(listener);
second.addActionListener(listener);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.