我正在制作记忆游戏。我已经运行了,如果点击它的按钮正在翻转,但是当点击第3和第4行中的按钮时,它不会翻转,它会显示ff作为输出:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
at Example.actionPerformed(Example.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
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)
这是我程序的代码
//package MemoryGame;
import java.lang.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;
public class Example extends JFrame implements ActionListener {
/**
*
*/
String front[] = {"1.png", "2.jpg", "3.png", "4.png", "5.jpg", "6.png", "7.png", "8.png"};
ImageIcon icons[];
private static final long serialVersionUID = 1L;
private JButton exitBtn, replayBtn;
private JButton[] gameBtn = new JButton[16];
private ArrayList<Integer> gameList = new ArrayList<Integer>();
private int counter = 0;
private int[] btnID = new int[2];
private int[] btnValue = new int[2];
public Example() {
init();
panel();
setArrayListText();
setTitle("MemoryGame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700, 700);
setVisible(true);
}
public void init() {
for (int i = 0; i < gameBtn.length; i++) {
gameBtn[i] = new JButton();
gameBtn[i].setIcon(new ImageIcon("back.jpg"));
gameBtn[i].setFont(new Font("Serif", Font.BOLD, 28));
gameBtn[i].addActionListener(this);
}
exitBtn = new JButton("Exit");
exitBtn.addActionListener(this);
replayBtn = new JButton("Replay");
replayBtn.addActionListener(this);
}
public void panel() {
Panel gamePnl = new Panel();
gamePnl.setLayout(new GridLayout(4,4));
for (int i = 0; i < gameBtn.length; i++) {
gamePnl.add(gameBtn[i]);
}
Panel buttonPnl = new Panel();
buttonPnl.add(replayBtn);
buttonPnl.add(exitBtn);
buttonPnl.setLayout(new GridLayout(1, 0));
add(gamePnl, BorderLayout.CENTER);
add(buttonPnl, BorderLayout.SOUTH);
}
public void setArrayListText() {
for (int i = 0; i < 2; i++) {
for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++) {
gameList.add(ii);
}
}
shuffle(gameList);
// ///////////////////
int newLine = 0;
for (int a = 0; a < gameList.size(); a++) {
newLine++;
// System.out.print(" " + gameList.get(a));
if (newLine == 4) {
System.out.println();
newLine = 0;
}
}
}
public boolean sameValues() {
if (btnValue[0] == btnValue[1]) {
return true;
}
return false;
}
@Override
public void actionPerformed(ActionEvent e) {
if (exitBtn == e.getSource()) {
System.exit(0);
}
if (replayBtn == e.getSource()) {
new Example();
}
for (int i = 0; i < gameBtn.length; i++) {
if (gameBtn[i] == e.getSource()) {
gameBtn[i].setIcon(new ImageIcon(front[i]));
gameBtn[i].setEnabled(false);
counter++;
if (counter == 3) {
if (sameValues()) {
gameBtn[btnID[0]].setEnabled(false);
gameBtn[btnID[1]].setEnabled(false);
} else {
gameBtn[btnID[0]].setEnabled(true);
gameBtn[btnID[0]].setText("");
gameBtn[btnID[1]].setEnabled(true);
gameBtn[btnID[1]].setText("");
}
counter = 1;
}
if (counter == 1) {
btnID[0] = i;
btnValue[0] = gameList.get(i);
}
if (counter == 2) {
btnID[1] = i;
btnValue[1] = gameList.get(i);
}
}
}
}
public static void main(String[] args) {
new Example();
}
}
帮助:(
答案 0 :(得分:0)
您正在访问front
中超出范围的数组索引。
在显示错误的第104行中,您有以下内容:
gameBtn[i].setIcon(new ImageIcon(front[i]));
虽然我不确定你对所有变量做了什么,但是在for循环上面的两行你正在迭代到gameBtn.length
,这是16,而front
只是长度8。
基本上,这些行是发生错误的地方:
for (int i = 0; i < gameBtn.length; i++) {
if (gameBtn[i] == e.getSource()) {
gameBtn[i].setIcon(new ImageIcon(front[i]));
同样,我不确定变量的确切含义,但也许您希望front
数组的每个元素与gameBtn
数组的两个元素配对,
在这种情况下,您可以尝试更改
gameBtn[i].setIcon(new ImageIcon(front[i]))
到
gameBtn[i].setIcon(new ImageIcon(front[i/2]))
此外,您提供的长错误中唯一的实际例外是第一行,
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 8
每隔一行只是调用堆栈的一个工件。