有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用公共或单独的actionPerfomed方法有什么区别

请帮助我理解以下两种方法将动作侦听器添加到JComponent之间的区别

第一种方法:将actionListener实现到我的类中,并添加公共actionPerformed方法,该方法根据事件选择选项

class Test implements ActionListener  
{
    JButton jbutton = null;
    public Test(){
        jbutton = new JButton();
        jbutton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        //Perform operation here;
    }
}

第二种方法:为单个JComponent定义动作侦听器

JButton jbutton = new JButton();              
button.addActionListener(new ActionListener() {         
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //Perform operation here    
    }
});

这两种方法之间的区别是什么?哪种方法更干净、更易于维护?是否有任何效率效益


共 (0) 个答案