import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class snake extends JFrame implements KeyListener,Runnable,ActionListener
{
private Thread thread_snake;
private JPanel game;
private button[][] a=new button[20][40];
private static int x=10,y=11,x1,y1;
private JButton start,pause;
private static int direct=2;//1表示向上,-1表示向下,2表示向左,-2表示向右.
private JButton up,down,left,right;
private boolean make=false,pause1;//pause1判断是否点了pause
private JComboBox difficulty;
private static int speed=1000;//控制速度
private JLabel fenShu,fenShu1,grade,grade1,title;
private static String g="一级";//记录级别
private static int total=0;
public snake()
{
this.setTitle("super snake");
this.setLocation(0,0);
this.setSize(700,700);
this.setLayout(null);
//thread_snake=new Thread(this,"");
game=new JPanel();
game.setLayout(new GridLayout(20,40));
this.add(game);
game.setBounds(0,100,700,400);
title=new JLabel("欢迎光临!贪吃蛇____ 制作者:钢三连");
this.add(title);
title.setBounds(230,10,300,20);
grade=new JLabel("级别");
this.add(grade);
grade.setBounds(10,50,50,20);
grade1=new JLabel(g);
this.add(grade1);
grade1.setBounds(60,50,50,20);
fenShu=new JLabel("分数");
this.add(fenShu);
fenShu.setBounds(300,50,50,20);
fenShu1=new JLabel("0");
this.add(fenShu1);
fenShu1.setBounds(360,50,50,20);
up=new JButton("上");
this.add(up);
up.setBounds(100,520,50,50);
up.addActionListener(this);
down=new JButton("下");
this.add(down);
down.setBounds(100,570,50,50);
down.addActionListener(this);
right=new JButton("右");
this.add(right);
right.setBounds(150,570,50,50);
right.addActionListener(this);
left=new JButton("左");
this.add(left);
left.setBounds(50,570,50,50);
left.addActionListener(this);
start=new JButton("start");
this.add(start);
start.setBounds(300,550,100,50);
start.addActionListener(this);
pause=new JButton("pause");
this.add(pause);
pause.setBounds(300,600,100,50);
pause.addActionListener(this);
Object jiBie[]={"一级","二级","三级","四级","五级","六级","七级"};
difficulty=new JComboBox(jiBie);
this.add(difficulty);
difficulty.setBounds(450,550,150,30);
difficulty.addActionListener(this);
for(int i=0;i<20;i++)
for(int j=0;j<40;j++)
{a[i][j]=new button(null);
a[i][j].body=false;
a[i][j].setEnabled(false);
a[i][j].setBackground(Color.WHITE);
game.add(a[i][j]);
}
//a[0][0].setEnabled(true);///!!!!!!!!!!原来这里出了问题
a[0][0].addActionListener(this);
button.head=button.rear=a[10][15];
button.head.body=true;
for(int i=15;i>11;i--)
{
a[10][i].front=a[10][i-1];
a[10][11].front=button.head;
a[10][i].body=false;
a[10][i].setBackground(Color.BLACK);
}
button.head=a[10][11];
button.head.setBackground(Color.BLACK);
this.setVisible(true);
addKeyListener(this);
this.requestFocus();
}
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
direct=1;
System.out.print("aaa");
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
direct=-1;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
direct=2;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
direct=-2;
}
}
public void keyReleased(KeyEvent e) { }
public void run()
{if(!pause1)
{x1=(int)(Math.random()*20)-1;
y1=(int)(Math.random()*40)-1;
a[x1][y1].setBackground(Color.BLACK);
//a[x1][y1].setBackground(Color.WHITE);
}
while(1==1)
{
if(make)
{
x1=(int)(Math.random()*20)-1;
y1=(int)(Math.random()*40)-1;
a[x1][y1].setBackground(Color.BLACK);
make=false;
}
switch(direct)
{
case(1):
{
if(direct!=-1)
x--;
}
case(-1):
{
if(direct!=1)
x++;
}
case(2):
{
if(direct!=-2)
y--;
}
case(-2):
{
if(direct!=2)
y++;
}
}
if(!a[x][y].body)//判断是否撞上自己。
{
if(a[x1][y1]==a[x][y])//判断前方是不是能吃。
{make=true;
total=total+10;
fenShu1.setText(""+total);
}
else
{button.rear.setBackground(Color.WHITE);
button.rear.body=false;
button.rear=button.rear.front;
button.rear.body=true;
}
button.head.front=a[x][y];
button.head=a[x][y];
button.head.body=true;
button.head.setBackground(Color.BLACK);
}
else
{
break;
}
try{
thread_snake.sleep(speed);
}
catch(InterruptedException e)
{break;}
}
}
public void actionPerformed(ActionEvent e)
{if(e.getActionCommand()=="start")
{thread_snake=new Thread(this,"");
thread_snake.start();
start.setEnabled(false);
pause1=false;
pause.setEnabled(true);
}
if(e.getActionCommand()=="上")
{if(direct!=-1)
direct=1;
}
if(e.getActionCommand()=="下")
{if(direct!=1)
direct=-1;
}
if(e.getActionCommand()=="左")
{if(direct!=-2)
direct=2;
}
if(e.getActionCommand()=="右")
{if(direct!=2)
direct=-2;
}
if(difficulty.getSelectedIndex()==0)
{speed=500; grade1.setText("一级");}
if(difficulty.getSelectedIndex()==1)
{speed=400; grade1.setText("二级");}
if(difficulty.getSelectedIndex()==2)
{speed=300; grade1.setText("三级");}
if(difficulty.getSelectedIndex()==3)
{speed=200; grade1.setText("四级");}
if(difficulty.getSelectedIndex()==4)
{speed=100; grade1.setText("五级");}
if(difficulty.getSelectedIndex()==5)
{speed=50; grade1.setText("六级");}
if(difficulty.getSelectedIndex()==6)
{speed=25; grade1.setText("七级");}
if(e.getActionCommand()=="pause")
{thread_snake.interrupt();
start.setEnabled(true);
pause.setEnabled(false);
pause1=true;
}
}
public static void main(String args[])
{
snake sn=new snake();
}
}class button extends JButton
{public static button head=null,rear=null;
public button front;
public boolean body;//判断一个方格是不是自己。
public button(){}
public button(button a)
{
if(head==null)
head=a;
else
{
head=front=a;
}
}
}
if(e.getActionCommand()=="start")
{thread_snake=new Thread(this,"");
thread_snake.start();
start.setEnabled(false);
pause1=false;
pause.setEnabled(true);
//加上
this.requestFocus(true);
}
就能在一开始的时候监听按键了.(已测试成功)但是你点其它按钮(如:上下左右),就又失去焦点了.是不是在每事件后加入一个this.requestFocus(true);
还是有其他的解决办法,就看LZ自己的需要了.!