单机五子棋游戏,实现鼠标单击下棋出错

qq_43673679 2019-01-21 03:56:29
鼠标在棋盘上单击后出现如图错误 ,请问怎么回事啊
...全文
509 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2019-01-28
  • 打赏
  • 举报
回复
引用 4 楼 qq_43673679 的回复:
Five.java import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JToolBar; public class Five extends JFrame { private JToolBar toolbar; private JButton startButton,backButton,exitButton; public Five(){ super("单机版五子棋"); toolbar=new JToolBar(); startButton=new JButton("重新开始"); backButton=new JButton("悔棋"); exitButton=new JButton("退出"); toolbar.add(startButton); toolbar.add(backButton); toolbar.add(exitButton); this.add(toolbar,BorderLayout.NORTH); boardPanel=new ChessBoard(); this.add(boardPanel,BorderLayout.CENTER); this.setLocation(200,200); this.pack(); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); ActionMonitor monitor=new ActionMonitor(); startButton.addActionListener(monitor); backButton.addActionListener(monitor); exitButton.addActionListener(monitor); } public static void main(String[] args) { new Five(); } private ChessBoard boardPanel; class ActionMonitor implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource()==startButton){ boardPanel.restartGame(); } else if(e.getSource()==backButton){ boardPanel.goback(); } else if(e.getSource()==exitButton){ System.exit(0); } } } }
是ChessBoard.java中第160行问题,但是你的代码还是没有贴出,应该是数组指针问题,你的数组可能是不能扩容数组,然后下一个棋子无法加入数组,指针异常,棋盘是围棋棋盘数组下标可以设置为最大360,0-360一共可以下361对应棋盘所有格子,要么用list数组可以添加新的元素。
qq_43673679 2019-01-21
  • 打赏
  • 举报
回复
Five.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;


public class Five extends JFrame {
private JToolBar toolbar;
private JButton startButton,backButton,exitButton;
public Five(){
super("单机版五子棋");
toolbar=new JToolBar();
startButton=new JButton("重新开始");
backButton=new JButton("悔棋");
exitButton=new JButton("退出");
toolbar.add(startButton);
toolbar.add(backButton);
toolbar.add(exitButton);
this.add(toolbar,BorderLayout.NORTH);
boardPanel=new ChessBoard();
this.add(boardPanel,BorderLayout.CENTER);
this.setLocation(200,200);
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
ActionMonitor monitor=new ActionMonitor();
startButton.addActionListener(monitor);
backButton.addActionListener(monitor);
exitButton.addActionListener(monitor);

}
public static void main(String[] args) {
new Five();
}
private ChessBoard boardPanel;
class ActionMonitor implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==startButton){
boardPanel.restartGame();
}
else if(e.getSource()==backButton){
boardPanel.goback();
}
else if(e.getSource()==exitButton){
System.exit(0);
}
}
}
}
qq_43673679 2019-01-21
  • 打赏
  • 举报
回复

这是Five.java
qq_43673679 2019-01-21
  • 打赏
  • 举报
回复
十八道胡同 2019-01-21
  • 打赏
  • 举报
回复
chessBoard.java 第160行 有空指针异常

最好贴出代码 看看

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧