首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JComboBox ItemListener误差

JComboBox ItemListener误差
EN

Stack Overflow用户
提问于 2015-11-13 14:11:46
回答 1查看 162关注 0票数 1

查看如何向JComboBoxes添加事件侦听器。我已经完成了通常的窗口,等等,创建了一个新的JComboBox,然后将.addItem()岛放入其中。然后,我尝试在我新创建的组合框上使用.addItemListener(这个),但是有一个问题,它提到抽象类,这意味着我没有做什么。有人能看到我哪里出了问题吗?

我尝试过在单个条目上使用.addItemListener(这个),但这不起作用。我尝试在构造函数内外声明JComboBox。

也许值得注意的是,itemStateChange的方法是从这本书,我已经建立了围绕该区块。

代码语言:javascript
复制
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);
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-13 14:29:49

代码语言:javascript
复制
@Override
public void itemStateChanged(ItemEvent event)
{
    String choice = event.getItem().toString();
    selection.setText("chose" + choice);
}

试着把它换成那个。上面是@Override。这样就不会为我抛出一个错误并且有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33694684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档