JComboBox ItemListener 错误

JComboBox ItemListener error

正在考虑向 JComboBoxes 添加事件侦听器。 我完成了通常的 window 等。创建了一个新的 JComboBox,然后将 .addItem() 岛放入其中。 然后我尝试在我新创建的组合框中使用 .addItemListener(this) 但是有一个问题,它提到 abstract class 这意味着我没有做某事。谁能看出我哪里出错了?

我已经在各个条目上尝试了 .addItemListener(this),但没有用。我试过在构造函数的内部和外部声明 JComboBox。

可能值得注意的是,itemStateChange 方法来自书中,我不得不围绕该块进行构建。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ComboBoxPractice extends JFrame implements ItemListener
{
    //create islands          
    JLabel selection = new JLabel();       
    JComboBox islands = new JComboBox();  
    
    public ComboBoxPractice()
    {
    // set a window
        super("action");
        setSize(300,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    // set a container
        Container content = getContentPane();
        FlowLayout layout = new FlowLayout();
        content.setLayout(layout);
    //add item listener   
        islands.addItemListener(this);
    // add items to list
        islands.addItem("Corfu");
        islands.addItem("Crete");
        islands.addItem("Canada");
        islands.addItem("Canary Islands");
    //add island and label to container  
        content.add(islands);   
        content.add(selection);
    }
    
    public void itemStateChange(ItemEvent event)
    {
    String choice = event.getItem().toString();
    selection.setText("chose" + choice);
    }
}

@Override
public void itemStateChanged(ItemEvent event)
{
    String choice = event.getItem().toString();
    selection.setText("chose" + choice);
}

试试改成那个。 @Override 在上面。然后这不会为我抛出错误并且有效。