gpt4 book ai didi

java - JComboBox ItemListener 错误

转载 作者:行者123 更新时间:2023-11-30 07:47:45 26 4
gpt4 key购买 nike

考虑向 JComboBox 添加事件监听器。我已经完成了通常的窗口等。创建了一个新的 JComboBox,然后将 .addItem() 岛放入其中。然后我尝试在新创建的组合框中使用 .addItemListener(this)但有一个问题,它提到了抽象类,这意味着我还没有做任何事情。谁能看出我哪里出了问题?

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

值得注意的是,itemStateChange 方法来自书中,我必须围绕该 block 进行构建。

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);
}
}

The exact error

最佳答案

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

尝试将其更改为该值。 @Override 位于顶部。这样就不会给我带来错误并且有效。

关于java - JComboBox ItemListener 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com