我试图制作一个扫雷游戏到目前为止我去做了布局,我的问题是当我点击一个块时java.lang.ArrayIndexOutOfBoundsException:9发生异常 这是代码:
import javax.swing.BorderFactory;
import javax.swing.JToggleButton;
import java.awt.*;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI.MouseHandler;
public final class minesweeper extends javax.swing.JFrame implements ActionListener, ContainerListener{
public minesweeper() {
initComponents();
setLayout(blockr,blockc,fw,fh);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
panel = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
NewGame = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
EasyM = new javax.swing.JMenuItem();
MediumM = new javax.swing.JMenuItem();
HardM = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
jLabel1.setText("jLabel1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(200, 200));
setPreferredSize(new java.awt.Dimension(200, 200));
setSize(new java.awt.Dimension(200, 200));
panel.setPreferredSize(new java.awt.Dimension(200, 300));
panel.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
panelMouseClicked(evt);
}
});
panel.setLayout(new java.awt.GridLayout(blockr, blockc));
NewGame.setText("File");
jMenu3.setText("New Game");
EasyM.setText("Easy (10mines,9x9blocks)");
EasyM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EasyMActionPerformed(evt);
}
});
jMenu3.add(EasyM);
MediumM.setText("medium (40mines,16x16blocks)");
MediumM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MediumMActionPerformed(evt);
}
});
jMenu3.add(MediumM);
HardM.setText("hard(99mines,16x30blocks)");
HardM.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
HardMActionPerformed(evt);
}
});
jMenu3.add(HardM);
NewGame.add(jMenu3);
Exit.setText("Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
NewGame.add(Exit);
jMenuBar1.add(NewGame);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
class MouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent me){
int i=0,j=0;
for(i=0;i<blockr;i++){
for(j=0;j<blockc;j++){
if(me.getSource()==blocks[i][j]){
found=true;
break;
}
if(found)break;
}
}
if(firstclick){
spawn(i,j);
firstclick=false;
}
blocks[i][j].setSelected(true);
}
}
public void check(int i,int j){
}
public void checkbomb(int x,int y){
if(blockValue[x][y]==-1){
JOptionPane.showMessageDialog(null, "Game Over");
}
}
public void spawn(int x,int y){
int r,c;
for(int k=0;k<bombs;k++){
do{
r = ranr.nextInt(blockr);
c = ranc.nextInt(blockc);
}while(blockValue[r][c]==-1||(r==x&&c==y));
blockValue[r][c]=-1;
}
}
void setLayout(int blockRow,int blockColumm,int fw,int fh){
firstclick=true;
blocks=new JToggleButton[blockRow][blockColumm];
blockValue=new int[blockRow][blockColumm];
setSize(fw, fh);
setResizable(false);
p = this.getLocation();
mh=new MouseListener();
getContentPane().removeAll();
panel.removeAll();
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10), BorderFactory.createLoweredBevelBorder()));
panel.setPreferredSize(new Dimension(fw, fh));
panel.setLayout(new java.awt.GridLayout(blockRow, blockColumm));
for(int i = 0; i < blockRow; i++){
for(int j = 0; j < blockColumm; j++){
blocks[i][j] = new JToggleButton("");
blocks[i][j].setSize(25,25);
blocks[i][j].addMouseListener(mh);
panel.add(blocks[i][j]);
}
}
panel.revalidate();
panel.repaint();
getContentPane().setLayout(new BorderLayout());
getContentPane().repaint();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
}
private void EasyMActionPerformed(java.awt.event.ActionEvent evt) {
fw = 300;
fh = 350;
blockr = 9;
blockc = 9;
bombs = 10;
setLayout(blockr,blockc,fw,fh);
}
private void MediumMActionPerformed(java.awt.event.ActionEvent evt) {
bombs=40;
fw = 320;
fh = 320;
blockr = 16;
blockc = 16;
bombs = 40;
setLayout(blockr,blockc,fw,fh);
}
private void HardMActionPerformed(java.awt.event.ActionEvent evt) {
bombs=99;
fw = 600;
fh = 320;
blockr = 16;
blockc = 30;
bombs= 99;
setLayout(blockr,blockc,fw,fh);
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void panelMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new minesweeper().setVisible(true);
});
}
static int blockr=9, blockc=9, fw=300,fh=350,bombs=10;
Point p;
JToggleButton[][] blocks;
int[][] blockValue;
boolean firstclick=true,found;
Random ranc=new Random();
Random ranr=new Random();
MouseListener mh;
// Variables declaration - do not modify
private javax.swing.JMenuItem EasyM;
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem HardM;
private javax.swing.JMenuItem MediumM;
private javax.swing.JMenu NewGame;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel panel;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentAdded(ContainerEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentRemoved(ContainerEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Excpeption:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 9
at minesweeper$MouseListener.mouseClicked(minesweeper.java:132)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
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:4534)
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:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
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:76)
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)
BUILD SUCCESSFUL (total time: 7 seconds)
我怀疑的是当我尝试调用方法块[i] [j] .setSelected(true); 创建异常,但我不明白为什么或做什么来解决它
答案 0 :(得分:1)
java.lang.ArrayIndexOutOfBoundsException:9
blockr = 9;
blockc = 9;
在我看来,你正在尝试创建一个9x9网格。
Java索引基于0,因此您的循环应为0 - 8.
如果您尝试使用索引9,则循环代码不正确。
找到错误的循环并修复代码。