简体   繁体   English

如何在内存中设置ActionListener?

[英]How ActionListeners are set up in the memory?

I have Submit JButton which has two ActionListeners 我有具有两个ActionListeners的Submit JButton

Category cat = new Category();

//this is launched second
submit.addActionListener(new ConfirmListener(new CategoryService(), cat));

//this is launched first. Set values
submit.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        String title = field.getText();
        cat.setTitle(title);
    }

});

Are they put on stack and they are launched as they pop? 它们是否堆叠并在弹出时启动? The second listener sets object's values and the first one is using the object. 第二个侦听器设置对象的值,第一个侦听器正在使用对象。

The 'ActionListener's are stored in a list data structure which is a member of the component. “ ActionListener”存储在作为组件成员的列表数据结构中。 They are usually called in the order in which they are added, but this is not guaranteed and can be handled differently by, for example, subclasses. 通常按添加顺序来调用它们,但这不能保证并且可以通过例如子类来不同地进行处理。

Therefore, it is considered bad practice to rely on a specific invocation order of listeners. 因此,依赖于侦听器的特定调用顺序被认为是不好的做法。 If an order is needed, create a composite listener which in turn calls your actual listener methods in the order in which you need them. 如果需要订单,请创建一个复合侦听器,该侦听器又会按您需要的顺序依次调用实际的侦听器方法。

ActionListener位于存储在EventListenerList中的幕后,因此将按照添加顺序执行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM