为什么当您单击“Rock”按钮时,此actionlistener不显示弹出框?溶液
ClickListener cl1 = new ClickListener();
JPanel panel1 = new JPanel();
JButton Rock = new JButton("Rock");
Rock.addActionListener(cl1);
panel1.add(Rock);
this.add(panel1);
this.setVisible(true);
}
private class ClickListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == "Rock"){
int AI = new Random().nextInt(3) + 1;
JOptionPane.showMessageDialog(null, "I have been clicked!");
}
}
}
}
答案 0 :(得分:0)
使用
if("Rock".equals(e.getActionCommand()){
只要您没有为按钮显式设置action命令,e.getActionCommand()
将返回您传递给它的构造函数的字符串文本。