gpt4 book ai didi

java - 图像数组在下拉菜单中超出范围

转载 作者:行者123 更新时间:2023-12-01 11:13:15 25 4
gpt4 key购买 nike

我正在使用 JCombo 框开发一个基本项目,当在框中选择某些内容时,图像会发生变化。窗口和图像显示,但是当我从 JCombo 框中选择第二个图像时,出现数组越界错误。请看一下:

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


box = new JComboBox (filename); //Automatically put the array in a list for us

box.addItemListener(
new ItemListener(){ //automatically implements itemlistener
public void itemStateChanged(ItemEvent event){ //invoked when dropdown menu button is selected
if (event.getStateChange() == ItemEvent.SELECTED) //what icon did you select, prevents you from clicking on itself
picture.setIcon(firstpictureinarray[box.getSelectedIndex()]); //change it into that selected icon
}

}
);

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

}
}

这是有问题的行:

picture.setIcon(firstpictureinarray[box.getSelectedIndex()]); //change it into that selected icon

在阅读了很多类似的错误后,我得出的结论是在以下数组中添加这两个元素,但我收到了错误;所以我评论了添加的部分:

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

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at Userinterfaces.Dropdown$1.itemStateChanged(main.java:34)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

非常感谢您花时间阅读本文,我非常感谢您为帮助程序员同行所付出的努力!

最佳答案

firstpictureinarray 仅包含单个项目,但您的组合框包含多个项目。

尝试像这样定义和初始化firstpictureinarray,以便数组和组合框始终包含相同数量的项目(即文件数量):

private Icon[] firstpictureinarray = new Icon[filename.length];
for (int i = 0; i < filename.length; i++) {
firstpictureinarray[i] = new ImageIcon(getClass().getResource(filename [i]));
}

此外,您应该考虑将“firstpictureinarray”重命名为更准确的名称,例如“picturearray”。

关于java - 图像数组在下拉菜单中超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32129381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com