[英]Passing Arguments to ActionListener
public class start implements ActionListener {
public void actionPerformed(ActionEvent aL) {
method(arguments);
}
}
method(arguments) {
//stuff
}
I want a JButton to tell the program to start a method using the arguments "stuff" So I put the actionListener above on the button however the actionListener does not have access to the arguments and I don't know how to give it them (I looked at the oracle docs and couldn't figure it out).我想要一个 JButton 告诉程序使用参数“东西”启动一个方法所以我把 actionListener 放在按钮上面但是 actionListener 没有访问参数,我不知道如何给它们(我查看了oracle docs,无法弄清楚)。 I tried this:我试过这个:
actionPerformed(ActionEvent aL, stuff) {
and that doesn't work, I get the error这不起作用,我收到错误消息
Compute.java:45: error: Compute.start is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
public class start implements ActionListener {
^
And I tried putting an @Override but you can't override that according to the compiler.我尝试放置一个@Override,但根据编译器您无法覆盖它。 So, how can I give the arguments to the ActionListener or have the method outside of the button but only start working once the button is pressed, however I am using a JProgressBar within the JFrame the button is in which uses the method as it's timer thing so it would be preferable to pass the arguments to the actionListener那么,我如何将参数提供给 ActionListener 或在按钮之外使用该方法,但只有在按下按钮后才开始工作,但是我在 JFrame 中使用了 JProgressBar,该按钮所在的按钮使用该方法作为计时器的事情所以最好将参数传递给 actionListener
You can pass the arguments by constructor.您可以通过构造函数传递参数。 But of course it means with this way you can pass the arguments only when creating the ActionListener
.但当然这意味着通过这种方式,您只能在创建ActionListener
时传递参数。
public class Start implements ActionListener {
SomeType arguments;
public Start (SomeType arguments) {
this.arguments = arguments;
}
public void actionPerformed(ActionEvent aL) {
method(arguments);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.