gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException : 9 error 异常

转载 作者:行者123 更新时间:2023-11-29 06:28:34 24 4
gpt4 key购买 nike

关于这个post .我试图显示 9 个图标,9 个文本字段,但出现错误

java.lang.ArrayIndexOutOfBoundsException: 9

标签代码如下

static void addIt(JTabbedPane tabbedPane, String text) throws IOException {

JPanel panel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();

foodLabel = new JLabel[ELEMENTS];
qtyField = new JTextField[ELEMENTS];
file = new File[ELEMENTS];
imageIcon = new ImageIcon[ELEMENTS];
image = new BufferedImage[ELEMENTS];

for (int i = 0; i < ELEMENTS; i++) {
try {
file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
file[i + 2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
file[i + 3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
file[i + 4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
file[i + 5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
file[i + 6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
file[i + 7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
file[i + 8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

image[i] = ImageIO.read(file[i]);
imageIcon[i] = new ImageIcon(image[i]);
} catch (Exception e) {
e.printStackTrace();
}
}

for (int i = 0; i < ELEMENTS; i++) {
foodLabel[i] = new JLabel(imageIcon[i]);
qtyField[i] = new JTextField(3);
}
gbc.gridx =0;
for (int i = 0; i < ELEMENTS; i++) {
if (i % 3 == 0) {
gbc.gridy += 2;
gbc.gridx = 0;
}
panel.add(foodLabel[i], gbc);
gbc.gridy++;
panel.add(qtyField[i], gbc);
gbc.gridx++;
gbc.gridy--;
tabbedPane.addTab(text, panel);
}
}

错误指向

file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");

最佳答案

您将 file 定义为:

    file = new File[ELEMENTS];

然后像这样访问它:

    for (int i = 0; i < ELEMENTS; i++) {
try {
file[i] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
file[i + 1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
file[i + 2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
file[i + 3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
file[i + 4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
file[i + 5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
file[i + 6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
file[i + 7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
file[i + 8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

image[i] = ImageIO.read(file[i]);
imageIcon[i] = new ImageIcon(image[i]);
} catch (Exception e) {
e.printStackTrace();
}
}

iELEMENTS - 1 时,file[i + 1] 将为 file[ELEMENTS] ,这将超出范围,因为数组中的最后一个元素的索引为 ELEMENTS - 1

你可能想做的是:

    file = new File[ELEMENTS];
...
file[0] = new File("C:\\Users\\tony\\Desktop\\MedSalad.png");
file[1] = new File("C:\\Users\\tony\\Desktop\\JapanesePanNoodles.png");
file[2] = new File("C:\\Users\\tony\\Desktop\\Spaghetti.png");
file[3] = new File("C:\\Users\\tony\\Desktop\\PadThai.png");
file[4] = new File("C:\\Users\\tony\\Desktop\\RamenNoodles.png");
file[5] = new File("C:\\Users\\tony\\Desktop\\SpaghettiAndMeatBalls.png");
file[6] = new File("C:\\Users\\tony\\Desktop\\chickenRice.jpg");
file[7] = new File("C:\\Users\\tony\\Desktop\\thaiFood.jpeg");
file[8] = new File("C:\\Users\\tony\\Desktop\\vietnamFood.jpg");

for (int i = 0; i < ELEMENTS; i++) {
try {
image[i] = ImageIO.read(file[i]);
imageIcon[i] = new ImageIcon(image[i]);
} catch (Exception e) {
e.printStackTrace();
}
}

关于java.lang.ArrayIndexOutOfBoundsException : 9 error 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737498/

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