[英]How can I add the ActionEvent to the JButton so that when i click it it will fire the event?
I want to be able to add the ActionListener to the JButton but cannot seem to get it to work properly. 我希望能够将ActionListener添加到JButton,但似乎无法使其正常工作。
I have tried to add the ActionListeneer and also the ActionEvent and neither seems to fire the ActionPerformed method. 我尝试添加ActionListeneer和ActionEvent,但似乎都没有触发ActionPerformed方法。
I did not one curious aspect was the the compiler made me take off the @Override keyword since the interface is used to create a variable and not implemented. 我没有一个奇怪的方面是编译器使我无法使用@Override关键字,因为该接口用于创建变量且未实现。
Does this make a difference? 这有什么不同吗? I am certain that you can do it this way but I think I am just a bit off the mark.
我敢肯定,您可以这样做,但我认为我的能力还差得远。
Code: 码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class testInterfaces2 {
static ActionListener m;
static ActionEvent me;
testInterfaces2() {
}
public void actionPerformed(ActionEvent e) {
System.out.println("Mouse Clicked");
}
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JButton pButton = new JButton("Print");
pButton.addActionListener(m);
//pButton.addActionListener(m.actionPerformed(me));
f.add("Center", pButton);
f.pack();
f.setVisible(true);
}
}
It should be like this and remove ActionListener
and ActionEvent
variables that is not needed. 应该是这样,并删除不需要的
ActionListener
和ActionEvent
变量。
public class testInterfaces2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Mouse Clicked");
}
...
JButton pButton = new JButton("Print");
pButton.addActionListener(this);
}
@Override
doesn't do any thing extra other than compile time checking of the overridden method. @Override
除了对覆盖的方法进行编译时检查外没有做任何其他事情。
In simple term, You need a class that implements ActionListener
and obviously implements actionPerformed()
method. 简单来说,您需要一个实现
ActionListener
并显然实现actionPerformed()
方法的类。 Simply create a object of that class and pass in addActionListener()
method. 只需创建该类的对象并传递
addActionListener()
方法即可。
I did not one curious aspect was the the compiler made me take off the @Override keyword since the interface is used to create a variable and not implemented.
我没有一个奇怪的方面是编译器使我无法使用@Override关键字,因为该接口用于创建变量且未实现。
This should have highlighted your first problem, your testInterfaces2
class can't override actionPerformed
as it's not defined in any part of the parent class or it's parents. 这应该突出了您的第一个问题,您的
testInterfaces2
类不能覆盖actionPerformed
因为它没有在父类或其父级的任何部分中定义。 This is because testInterfaces2
doesn't implement ActionListener
directly or indirectly (via inheritance). 这是因为
testInterfaces2
不能直接或间接(通过继承)实现ActionListener
。
Your second problem is m
is null
, you've never initialised it 您的第二个问题是
m
为null
,您从未初始化过
Take a closer look at How to write ActionListeners for more details 详细了解如何编写ActionListeners
I think it's best to define a new ActionListener for each button. 我认为最好为每个按钮定义一个新的ActionListener。 Like this
像这样
JButton pButton = new JButton();
pButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
System.exit(0);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.