我有一个JComboBox,我想显示我创建的类型为“ Verb”的HashSet中的所有元素。但是,当我执行程序时,我收到一个异常,内容为:“。IllegalArgumentException:setSelectedIndex:0超出范围”。我了解这可能意味着,当访问由集合构成的数组时,它无法在ComboBox中找到默认索引(0)的任何元素。我想知道我使用ComboBox的方式是否不正确。
我正在尝试创建的应用程序是一个GUI,它将允许我输入动词,这些动词将被存储到集合中,然后显示在组合框中。我使用了自定义ComboBoxModel来允许使用我的自定义Verb对象,但是我觉得也许我应该为Combo Box使用自定义渲染器,也许这就是为什么我要面对这个问题。
应用程序类
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
public class App extends JPanel {
private HashSet<Verb> verbs = new HashSet<>();
private JTextField field = new JTextField(20);
private JCheckBox checkBox = new JCheckBox("Irregular");
private JComboBox<Verb> comboBox = new JComboBox<>(new MyComboBoxModel(verbs));
public App() {
verbs.add(new Verb("Hello", true));
//App Layout
setLayout(new MigLayout("align 50%"));
CardLayout cardLayout = new CardLayout();
//Main Panel
JPanel centerPanel = new JPanel(cardLayout);
//Card Switcher
CardSwitcher switcher = new CardSwitcher(centerPanel, cardLayout);
//Nav Button Panel
ButtonPanel buttons = new ButtonPanel(switcher);
// JPanels for Cards /////////////
//ADD VERB CARD
JPanel pnl_addVerb = new JPanel(new MigLayout());
pnl_addVerb.add(new JLabel("Enter a verb: "), "wrap");
// TEXT FIELD
pnl_addVerb.add(field, "wrap");
// CHECK BOX
pnl_addVerb.add(checkBox, "wrap");
// ADD VERB BUTTON
JButton addVerb = new JButton("Add");
addVerb.addActionListener(new BtnHandler(this, 1));
pnl_addVerb.add(addVerb, "wrap");
centerPanel.add(pnl_addVerb, "A");
//VIEW VERBS CARD
JPanel pnl_ViewVerb = new JPanel(new MigLayout());
pnl_ViewVerb.add(new JLabel("View Verb"), "wrap");
// COMBO BOX TO DISPLAY VERBS
pnl_ViewVerb.add(comboBox);
comboBox.setSelectedIndex(0);
centerPanel.add(pnl_ViewVerb, "B");
//Add nav buttons and center panel to app
add(buttons, "span");
add(centerPanel, "span");
}
class ButtonPanel extends JPanel{
public ButtonPanel(CardSwitcher switcher){
setLayout(new GridBagLayout());
JButton buttonA = new JButton("Add Verbs");
add(buttonA);
buttonA.addActionListener(new NavHandler(1, switcher));
JButton buttonB = new JButton("View Verbs");
add(buttonB);
buttonB.addActionListener(new NavHandler( 2, switcher));
}
}
//Handling of card switching
class NavHandler implements ActionListener {
int state;
CardSwitcher switcher;
public NavHandler(int state, CardSwitcher switcher){
this.state = state;
this.switcher = switcher;
}
public void actionPerformed(ActionEvent e){
switch(state){
case 1: switcher.switchTo("A");
break;
case 2: switcher.switchTo("B");
break;
}
}
}
//Handling of other buttons/items
class BtnHandler implements ActionListener{
App app;
int state;
public BtnHandler(App app, int state){
this.app = app;
this.state = state;
}
public void actionPerformed(ActionEvent e){
switch(state){
case 1:
String inf = app.field.getText();
if(!app.checkBox.isSelected()){
verbs.add(new Verb(inf, true));
field.setText("");
}
for(Verb v : verbs){
System.out.println(v.toString());
}
break;
case 2:
break;
}
}
}
class CardSwitcher {
CardLayout layout;
Container container;
public CardSwitcher(Container container, CardLayout layout) {
this.layout = layout;
this.container = container;
}
public void switchTo(String card) {
layout.show(container, card);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Verb Program");
App app = new App();
frame.getContentPane().add(app);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
});}
}
动词类
public class Verb {
public String infinitive;
boolean regular;
public Verb(String infinitive, boolean regular){
this.infinitive = infinitive;
this.regular = regular;
System.out.println("Created verb obj " + infinitive);
}
@Override
public String toString() {
return this.infinitive;
}
public boolean equals(Object o){
Verb v = (Verb)o;
if(v.infinitive.equals(this.infinitive)){
return true;
}
return false;
}
public int hashCode(){
int hash = 1;
hash = hash + this.infinitive.hashCode();
return hash;
}
}
MyComboBoxModel类
import javax.swing.*;
import java.util.HashSet;
public class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {
HashSet<Verb> set;
Verb[] arr;
String selection = null;
public MyComboBoxModel(HashSet<Verb> set){
this.set = set;
this.arr = set.toArray(new Verb[set.size()]);
System.out.println("Created Combo Box");
}
public Object getElementAt(int index){
return arr[index];
}
public int getSize(){
return arr.length;
}
public void setSelectedItem(Object o){
selection = (String)o;
}
public Object getSelectedItem(){
return selection;
}
}
我希望输出结果是动词应显示在ComboBox中,但是程序运行时会发生异常。如果删除代码行“ comboBox.setSelectedIndex(0);”,该程序将运行,但在组合框内不显示任何动词。