[英]Java Actionlistener on Button
import java.awt.*;
public class TestButton {
private Frame f;
protected Button b;
public TestButton() {
f = new Frame("Test");
b = new Button("Press Me!");
b.setActionCommand("ButtonPressed");
}
public void launchFrame() {
b.addActionListener(new ButtonHandler());
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public static void main(String args[]) {
TestButton guiApp = new TestButton();
guiApp.launchFrame();
}
}
import java.awt.*;
import java.awt.event.*;
public class ButtonHandler extends TestButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==b)
{
System.out.println("Action occurred");
System.out.println("Button's command is: "
+ e.getActionCommand());
}
}
}
I'm trying to invoke a ActionEvent when the button b is pressed but not working with getSource. 我正在尝试在按下按钮b但不使用getSource时调用ActionEvent。
You're misusing inheritance. 你滥用继承权。 The ButtonHandler class should not extend the TestButton class, since the b variable in the handler class refers to a completely different Button object from the one displayed.
ButtonHandler类不应扩展TestButton类,因为处理程序类中的b变量引用与显示的完全不同的Button对象。 I suggest:
我建议:
getSource()
method and use it directly. getSource()
方法中获取JButton并直接使用它。 For example: 例如:
import java.awt.event.ActionEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestButton extends JPanel {
private JButton btn = new JButton(new ButtonAction("Press Me!", "ButtonPressed"));
public TestButton() {
add(btn);
}
private static void createAndShowGUI() {
TestButton testButton = new TestButton();
JFrame frame = new JFrame("TestButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testButton );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
@SuppressWarnings("serial")
class ButtonAction extends AbstractAction {
public ButtonAction(String name, String actionCommand) {
super(name);
putValue(ACTION_COMMAND_KEY, actionCommand);
}
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Button's actionCommand is: " + evt.getActionCommand());
}
}
That happens because you extends TestButton
in your ButtonHandler
, because of you have 2 different instances of your Button
and thea are not equals. 之所以会发生这种情况,是因为你在
ButtonHandler
扩展了TestButton
,因为你有两个不同的Button
实例,而且它们不等于。
To fix that you can remove extends TestButton
and make ButtonHandler
as inner class of TestButton
要解决此问题,您可以删除
extends TestButton
并将ButtonHandler
作为TestButton
内部类
or you can compare action commands instead of Button
's like next: 或者您可以比较动作命令而不是下一步的
Button
:
if(((Button)source).getActionCommand().equals("ButtonPressed"))
I think you need to remove extends TestButton as the 2 different instances of the buttons are not equal. 我认为您需要删除扩展TestButton,因为按钮的2个不同实例不相等。 You should go for a ButtonHandler as inner class or anonymous class to implement this.
您应该将ButtonHandler作为内部类或匿名类来实现它。
Check this question: Java Button Handler 检查这个问题: Java Button Handler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.