查看如何向JComboBoxes添加事件侦听器。我已经完成了通常的窗口,等等,创建了一个新的JComboBox,然后将.addItem()岛放入其中。然后,我尝试在我新创建的组合框上使用.addItemListener(这个),但是有一个问题,它提到抽象类,这意味着我没有做什么。有人能看到我哪里出了问题吗?
我尝试过在单个条目上使用.addItemListener(这个),但这不起作用。我尝试在构造函数内外声明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);
}
}

发布于 2015-11-13 14:29:49
@Override
public void itemStateChanged(ItemEvent event)
{
String choice = event.getItem().toString();
selection.setText("chose" + choice);
}试着把它换成那个。上面是@Override。这样就不会为我抛出一个错误并且有效。
https://stackoverflow.com/questions/33694684
复制相似问题