比如我在面板里面添加了一个按钮,想通过鼠标单击按钮以后改变按钮上面的标签,应该怎样实现?我知道改变按钮标签的方法是 ButtonObject.setLabel(),但是在
public void actionPerformed(ActionEvent e)
{}
中怎样才能得到“按钮”对象呢?

解决方案 »

  1.   

    JButton button = new JButton("你好");

    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    ((JButton)e.getSource()).setText("我好");
    }
    });//button.setLabel(); //该方法已经不推荐使用,用button.setText()方法替代。
      

  2.   

    JButton button = new JButton("你好");
    button.addActionListener(new SymAction());
    class SymAction implements ActionListener
    {
    public void actionPerformed(ActionEvent e) {
    Object object=e.getSource();//得到事件源
    if(object==button){
    button.setText("Hello EveryOne!");
    }else if(){
    }//多个事件源时
    }
    }
      

  3.   

    其它按钮也注册这个listener嘛