JComboBox将在数组中显示第一项,但在选择第二项时会抛出异常

时间:2014-09-18 16:45:21

标签: java swing exception jcombobox

我可以切换文件名“dr.png”或“MF4040.png”的顺序,它将默认为它应该这样做,代码编译,运行和ALWAYS工作,同时显示数组中的第一项,但会抛出程序运行时在JComboBox中选择第二项时的异常。我也在使用JDK1.6和Eclipse 4.2。来自Bucky的JAVA教程https://www.youtube.com/watch?v=XS4-5GmRnp8&index=69&list=PL484D73FA74A11AC9

这是代码......

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

public class gui extends JFrame{

private JComboBox box;
private JLabel picture;

private static String[] filename = {"dr.png", "MF4040.png" };
private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0]))};

public gui(){
    super("The title");
    setLayout(new FlowLayout());

    box = new JComboBox(filename);

    box.addItemListener(
        new ItemListener(){             
            public void itemStateChanged(ItemEvent event){
                if(event.getStateChange()==ItemEvent.SELECTED)
                    picture.setIcon(pics[box.getSelectedIndex()]);

             }
         }      
);

    add(box);
    picture=new JLabel(pics[0]);

     add(picture);

}
}   

和我的主要......

 import javax.swing.JFrame;

 public class apples {


public static void main(String[] args) {

    gui go = new gui();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(300,200);
    go.setVisible(true);
  }
}

以下是抛出的异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
     at gui$1.itemStateChanged(gui.java:24)
     at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1205)
     at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1262)
     at javax.swing.JComboBox.contentsChanged(JComboBox.java:1309)
     at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
     at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
     at javax.swing.JComboBox.setSelectedItem(JComboBox.java:557)
     at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:603)
     at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:831)
     at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
     at java.awt.Component.processMouseEvent(Component.java:6414)
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
     at com.apple.laf.AquaComboBoxPopup$1.processMouseEvent(AquaComboBoxPopup.java:132)
     at java.awt.Component.processEvent(Component.java:6179)
     at java.awt.Container.processEvent(Container.java:2084)
     at java.awt.Component.dispatchEventImpl(Component.java:4776)
     at java.awt.Container.dispatchEventImpl(Container.java:2142)
     at java.awt.Component.dispatchEvent(Component.java:4604)
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4279)
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4209)
     at java.awt.Container.dispatchEventImpl(Container.java:2128)
     at java.awt.Window.dispatchEventImpl(Window.java:2492)
     at java.awt.Component.dispatchEvent(Component.java:4604)
     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
     at java.awt.EventQueue.access$400(EventQueue.java:82)
     at java.awt.EventQueue$2.run(EventQueue.java:676)
     at java.awt.EventQueue$2.run(EventQueue.java:674)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
     at java.awt.EventQueue$3.run(EventQueue.java:690)
     at java.awt.EventQueue$3.run(EventQueue.java:688)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1 个答案:

答案 0 :(得分:2)

您没有设置第二个图标:

private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0]))};

解决它替换为:

private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])), 
       new ImageIcon(getClass().getResource(filename[1]))};