ActionListener什么也没做 [英] ActionListener not doing anything
本文介绍了ActionListener什么也没做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java的新手,所以请多多包涵。
我试图获得一个按钮来打开一个名为AboutFrame的新框架,但是每当我按下按钮时,什么都不会发生。
I am new to Java, so please bear with me. I've tried to get a button to open up a new frame called AboutFrame, but whenever I press the button nothing happens.
我首先实现了ActionListener :
I implement the ActionListener first:
class MainFrame extends JFrame implements ActionListener {
然后我设置按钮(在通常的super( blabla)之后...)
Then I set the button (after the usual super("blabla");...)
JButton info = new JButton("About Failsafe");
info.addActionListener(this);
然后:
public void actionPerformed(ActionEvent event) {
String command = event.getSource().toString();
if (command == "info") {
AboutFrame abt = new AboutFrame();
}
}
那么我在这里做错了吗?我看不到任何错误。
So what am I doing wrong here? I can't see any mistakes..
推荐答案
您没有正确获得命令文本:
You're not getting the command text correctly:
JButton button = (JButton) event.getSource();
String command = button.getText();
if (command.equals("About Failsafe"))
{
AboutFrame abt = new AboutFrame();
abt.setVisible(true);
}
或者,如果您的 JButton信息;
声明是一个实例变量(而不是局部变量),您可以进行if检查:
Or, if your JButton info;
declaration is an instance variable (instead of a local one), you could make your if-check:
if (event.getSource() == info)
这篇关于ActionListener什么也没做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文