图像不在eclipse中工作

时间:2017-12-21 14:46:48

标签: java awt indexoutofboundsexception

我是一个相对较新的日食,以及一般的java编程。我有问题让我的图像按照他们应该的方式工作。当我运行我的程序时,它应该显示一个带有图像的下拉菜单。如果您想了解有关目标的更多信息,请点击YouTube链接,观看以下视频教程(第68,69页)。

当我运行我的程序而不是播放图像时,它只显示它的名称,这发生在我写过的其他程序中。如果您需要有关代码的更多信息,请询问。图像没有显示,所以请帮助!!!

我的代码:

import java.util.Scanner;

import javax.swing.JFrame;

public class Initial {
    public static void main(String[] args) {

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

    }   
}



   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 = {"test1.png", "test2.png"};
    private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0]))};

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

        JComboBox<String> 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]);
    }
}
单击下拉框中的一个图像时出现

错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at GUI$1.itemStateChanged(GUI.java:23)
    at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1223)
    at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1280)
    at javax.swing.JComboBox.contentsChanged(JComboBox.java:1330)
    at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
    at javax.swing.JComboBox.setSelectedItem(JComboBox.java:576)
    at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:861)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at com.apple.laf.AquaComboBoxPopup$1.processMouseEvent(AquaComboBoxPopup.java:157)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案