我正在用Java编写我的第一个复杂应用程序Swing。当我将ActionListener添加到我的JButton中时。java中的ActionListener对第二次点击执行操作
ActionListener changeButton = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
if(startButton.getText() == "Spustit") {
startButton.setText("STOP");
} else {
startButton.setText("Spustit");
}
}
}
我加入ActionListener添加到按钮本身
private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {
startButton.addActionListener(changeButton);
}
你能告诉我在哪儿编码的ActionListener不好?
谢谢大家!
你调试过该方法是否被调用? – Smutje 2014-10-17 12:17:08
@Smutje:好的,方法被调用,但不是第一次点击。它仅在第二次或第三次点击时“有效” – 2014-10-17 12:22:59
你在哪里编码不好?例如这里:'startButton.getText()==“Spustit”'。将字符串与“equals”进行比较,而不是用“==”进行比较。虽然,由于使用了intented字符串,你的比较可能会奏效,但你应该修正它。 – Tom 2014-10-17 12:23:23