首页>代码>java swing客户端贪吃蛇小游戏>/Snake/src/com/java4_18/snak05/Button.java
package com.java4_18.snak05;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import com.java4_18.snak05.SnakPanle.SnakeThread;

public class Button extends JPanel{
	SnakPanle snakPanle;
	Button(SnakPanle snakPanle){
		this.snakPanle = snakPanle;
		this.setBounds(0, 440, 706, 60);
		JButton pause = new JButton("暂停游戏");
		JButton continu = new JButton("继续游戏");
		JButton restart = new JButton("重新开始");
		this.add(pause);
		this.add(continu);
		this.add(restart);
		
		pause.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Config.isgone = false;
			}
		});
		 
		continu.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent e) {
				Config.isgone = true;
				snakPanle.setFocusable(true);
				snakPanle.requestFocus();
			}
		});
		
		restart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				snakPanle.snakeThread.stopThread();//把当前线程停掉
				
				//重新在蛇运动的面板上创建原始的蛇和食物
				Food food = new Food();
				snakPanle.food = food;
				snakPanle.snake = new Snake(food);
				//把控制条件还原
				Config.isgone = true;
				Config.islive = true;
				//在面板上创建一个新的线程
				SnakeThread snakeThread = snakPanle.new SnakeThread();
				snakeThread.start();
				snakPanle.snakeThread = snakeThread;
				//获取键盘焦点
				snakPanle.setFocusable(true);
				snakPanle.requestFocus();
				
			}
		});
	}
}
最近下载更多
3456788  LV1 2024年7月4日
lsglsg9  LV23 2024年5月23日
jiemomo  LV12 2023年10月19日
wty1234  LV2 2023年6月27日
sadselect  LV8 2023年5月30日
yuejing  LV1 2022年10月9日
craven  LV2 2022年7月30日
yerwiu  LV10 2022年7月4日
喃喵xxxx  LV6 2022年4月16日
无名氏111  LV33 2021年12月30日
最近浏览更多
3456788  LV1 2024年7月4日
lsglsg9  LV23 2024年5月23日
zattttt  LV1 2024年5月15日
爱情戴罪的羔羊  LV7 2024年4月2日
Eayon  LV19 2023年11月27日
jiemomo  LV12 2023年10月19日
wangjialiang1  LV17 2023年8月23日
wty1234  LV2 2023年6月27日
wty1132 2023年6月27日
暂无贡献等级
baihaushu  LV1 2023年6月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友