[JAVA]如何调用方法"itemStateChanged"

[JAVA]How to call the method "itemStateChanged"

所以,我有这个方法;

public void itemStateChanged(ItemEvent event){
        if(event.getSource() == temasJogo){
            if(event.getStateChange() == ItemEvent.SELECTED){
                indiceTema = indiceTemas[ temasJogo.getSelectedIndex() ];
            }

        }
    }

就是为了这个JComboBox

temasJogo = new JComboBox(temas);
        temasJogo.addActionListener(this);

我需要它来修改我的 class 的属性,以便它选择游戏的其他主题。问题是,我无法在任何地方调用此方法。我知道答案会很简单,但我真的需要帮助。

理论上,itemStateChangedItemListener 的一种方法,假设您已经以某种方式实现了 interface

为了调用它,您需要用 JComboBox

注册 ItemListener 的实例
temasJogo.addItemListener(this);

举个例子

有关详细信息,请参阅 How to Use Combo Boxes and How to Write an Item Listener

只是实现了class中的接口。

例如

class sample implements ItemListener

在 JComboBox 上应用侦听器

        temasJogo = new JComboBox(temas);
        temasJogo.addItemListener(this);

之后,当项目发生变化时将调用以下函数。

public void itemStateChanged(ItemEvent event){
        if(event.getSource() == temasJogo){
            if(event.getStateChange() == ItemEvent.SELECTED){
                indiceTema = indiceTemas[ temasJogo.getSelectedIndex() ];
            }

        }
    }