当以如下方式创建了JButton b1 = new JButton("Open folder");
那么e.getActionCommand()=="Open folder"成否识别这个ActionEvent是由b1发出的呢???

解决方案 »

  1.   

    e.getActionCommand().equals("Open folder")
      

  2.   


    if(e.getSource().equals(b1))
        Do;
      

  3.   


    JButton b1 = new JButton("open folder");
    b1.setActionCommand("command");
    if(b1.getActionCommand()=="command")
    {
        ................................
    }
      

  4.   

    同意二楼的说法,当你处理按钮点击事件 时,你可以利用方法setActionCommand方法设定命令字符串(ActionCommand),或者不设定。
    如果你不设定,那么ActionCommand将采用默认的ActionCommand(就是你按钮的标签--名字)。三楼的---if(b1.getActionCommand()== "command ") ---应该改成
    if(b1.getActionCommand().equals("command "))  因为==表示的是两个对象是否是同一个,而不能表示其内容是否相同。
    当然,最好是不用ActionCommand(命令字符串),因为命令字符串存在着风险。后续待查
      

  5.   

    == 比地址,识别不了。要用equals
      

  6.   

    刚才我说错了,我同意一楼的说法,其他的都对。对于5楼的说说法,请解释一下,为什么是比地址?
    我们还有 一种方法来确定我们到底是点击了哪个按钮。
    JButton b1 = new JButton("left");
    JButton b2 = new JButton("right");.....
    public void actionPerformed(ActionEvent e){
    JButton b =(JButton)e.getSoure();
    if(b == b1){...}
    else {...}
    }
    其中getSource方法得到的是事件源,是ActionEvent类,可以用强制类型转换,转到JButton类,然后通过判断事件源和哪个对象是同一个。
    自己的一点理解,写出来和大家探讨一下。有不对的请大家指正!谢谢!