本文介绍了Java Swing 匿名操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以像下面的代码示例那样设置操作吗?是否有任何可能由此引起的垃圾收集问题?如果不是,最好的方法是什么?

Is it ok to set actions like in the code sample below? Are there any possible garbage collection problems that might be caused by this? And if not what would be the best way to do this?

btnAwesomeButton=new JButton(new AbstractAction("Awesome Button") {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        //Do stuff here
        //Refer to the components on parent windows through ParentWindowClass.this.componentName
    }

});

推荐答案

这是一个很常见的习语.不存在垃圾回收问题,前提是您不将 AbstractAction 子类实例的引用隐藏在某处(如哈希表),在 actionPerformed 返回后它将持续存在.

This is a very common idiom. There are no garbage collection issues, provided you don't stash a reference to the AbstractAction subclass instance somewhere (like a hash table) where it will persist after actionPerformed returns.

这篇关于Java Swing 匿名操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:22