public StartChessJFrame setTitle五子棋;设置标题 chessBoard=new ChessBoard; Container contentPane=getContentPane; contentPaneaddchessBoard; chessBoardsetOpaquetrue; 创
//在工具面板中添加暂停和继续按钮 pauseButton = new JButton("暂停"); resumeButton = new JButton("继续"); toolbar.add(pauseButton); toolbar.add(resumeButton); //将暂停按钮添加事件监听 pauseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { chessBoard.setPaused(true); //将游戏暂停 } }); //将继续按钮添加事件监听 resumeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { chessBoard.setPaused(false); //将游戏继续 } });
同时,在 `ChessBoard` 类中添加 `setPaused` 方法,用于设置游戏是否暂停:
```java
public void setPaused(boolean paused) {
this.paused = paused;
}
在 paintComponent 方法中,加入判断游戏是否暂停,如果暂停则不进行绘制:
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=0;i<ROWS;i++) {
for(int j=0;j<COLS;j++) {
...
}
}
//判断游戏是否暂停,如果暂停则不进行绘制
if(!paused) {
if(gameOver) {
...
} else {
...
}
}
}
``
原文地址: http://www.cveoy.top/t/topic/g1mT 著作权归作者所有。请勿转载和采集!