java中JPanel如何实现键盘监听事件
我有两个类,一个是JFrame和一个JPanel,在JPanel中实现游戏的键盘事件,然后将JPanel添加到JFrame中,请问如何实现JPanel的键盘监听事件?//...
我有两个类,一个是JFrame和一个JPanel,在JPanel中实现游戏的键盘事件,然后将JPanel添加到JFrame中,请问如何实现JPanel的键盘监听事件?
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class stootGame extends JFrame {
public stootGame()
{
super("stootGame");
myMenuBar menubar=new myMenuBar();
this.setJMenuBar(menubar);
myGamePanel gamePanel=new myGamePanel();
this.add(gamePanel,BorderLayout.CENTER);
//gameFrame gameframe=new gameFrame();
this.add(gamePanel);
//this.setContentPane(gameframe);
Thread panelThread=new Thread(gamePanel);
panelThread.start();
//this.setSize(350,350);
this.setBounds(200,200,400,600);
this.setVisible(true);
}
public static void main(String []args)
{
stootGame stootgame= new stootGame();
//stootgame.setTitle("stootGame");
}
}
//JPanel类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class myGamePanel extends JPanel implements KeyListener, Runnable {
int x=150,y=430,dx,dy;
int bx,by,dbx,dby=-10,flag=0;
Image img,imgBomb;
JTextField text,text2;
public myGamePanel()
{
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage(myGamePanel.class.getResource("fly40.png"));
imgBomb=tk.getImage(myGamePanel.class.getResource("bomb1.png"));
//this.addKeyListener(this);
this.setLayout(null);
text=new JTextField();
text2=new JTextField();
//text.addKeyListener(this);
text.setBounds(5,5,50,25);
text2.setBounds(5,30,50,25);
//this.add(text);
//this.add(text2);
this.addKeyListener(this);
//enableEvents(AWTEvent.KEY_EVENT_MASK);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
if(by<0)by=0;
if(flag==0)by=by+dby;
repaint();
try{
Thread.sleep(10);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.drawImage(img,x,y,this);
g.drawImage(imgBomb, bx, by,this);
}
@Override
public void keyTyped(KeyEvent e) {//键盘按键敲击事件
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {//键盘按键按下事件
// TODO Auto-generated method stub
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
dx=5;dy=0;
break;
case KeyEvent.VK_LEFT:
dx=-5;dy=0;
break;
case KeyEvent.VK_UP:
dx=0;dy=-5;
break;
case KeyEvent.VK_DOWN:
dx=0;dy=5;
break;
case KeyEvent.VK_SPACE:
dx=0;dy=0;
bx=x+15;
by=y-5;
flag=1;
default:
dx=0;dy=0;
}
x=x+dx;
y=y+dy;
}
@Override
public void keyReleased(KeyEvent e) {//键盘按键释放事件
// TODO Auto-generated method stub
}
}
JPanel的键盘监听事件没有响应,请问怎么实现? 展开
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class stootGame extends JFrame {
public stootGame()
{
super("stootGame");
myMenuBar menubar=new myMenuBar();
this.setJMenuBar(menubar);
myGamePanel gamePanel=new myGamePanel();
this.add(gamePanel,BorderLayout.CENTER);
//gameFrame gameframe=new gameFrame();
this.add(gamePanel);
//this.setContentPane(gameframe);
Thread panelThread=new Thread(gamePanel);
panelThread.start();
//this.setSize(350,350);
this.setBounds(200,200,400,600);
this.setVisible(true);
}
public static void main(String []args)
{
stootGame stootgame= new stootGame();
//stootgame.setTitle("stootGame");
}
}
//JPanel类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class myGamePanel extends JPanel implements KeyListener, Runnable {
int x=150,y=430,dx,dy;
int bx,by,dbx,dby=-10,flag=0;
Image img,imgBomb;
JTextField text,text2;
public myGamePanel()
{
Toolkit tk=Toolkit.getDefaultToolkit();
img=tk.getImage(myGamePanel.class.getResource("fly40.png"));
imgBomb=tk.getImage(myGamePanel.class.getResource("bomb1.png"));
//this.addKeyListener(this);
this.setLayout(null);
text=new JTextField();
text2=new JTextField();
//text.addKeyListener(this);
text.setBounds(5,5,50,25);
text2.setBounds(5,30,50,25);
//this.add(text);
//this.add(text2);
this.addKeyListener(this);
//enableEvents(AWTEvent.KEY_EVENT_MASK);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
if(by<0)by=0;
if(flag==0)by=by+dby;
repaint();
try{
Thread.sleep(10);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
g.drawImage(img,x,y,this);
g.drawImage(imgBomb, bx, by,this);
}
@Override
public void keyTyped(KeyEvent e) {//键盘按键敲击事件
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {//键盘按键按下事件
// TODO Auto-generated method stub
switch(e.getKeyCode())
{
case KeyEvent.VK_RIGHT:
dx=5;dy=0;
break;
case KeyEvent.VK_LEFT:
dx=-5;dy=0;
break;
case KeyEvent.VK_UP:
dx=0;dy=-5;
break;
case KeyEvent.VK_DOWN:
dx=0;dy=5;
break;
case KeyEvent.VK_SPACE:
dx=0;dy=0;
bx=x+15;
by=y-5;
flag=1;
default:
dx=0;dy=0;
}
x=x+dx;
y=y+dy;
}
@Override
public void keyReleased(KeyEvent e) {//键盘按键释放事件
// TODO Auto-generated method stub
}
}
JPanel的键盘监听事件没有响应,请问怎么实现? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询