java 键盘事件不响应.新手求助 求大佬.
importjava.awt.*;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;impor...
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class paintdemo1 extends JFrame
{
public void langchFrame()
{
this.setTitle("动画练习");
this.setBounds(300, 250, 400, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MyPanel jp = new MyPanel();
add(jp);
this.setVisible(true);
jp.langchJp();
}
public static void main(String[] args){
paintdemo1 p = new paintdemo1();
p.langchFrame();
}
}
class MyPanel extends JPanel implements KeyListener
{
Image img = Tollsdemo.getImage("image/滑稽.png"); //一个获取图片在工具类
int x = 100; int y = 100;
int dx = 10; int dy = 0;
public void langchJp ()
{
new Re().start();
this.addKeyListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
Color c = new Color(200,0,0);
g.setColor(c);
g.drawImage(img,x,y,null);
x+=dx; y+=dy;
if(x>330)
dx=-10;
if(x<0)
dx=10;
if(y>310)
dy=-10;
if(y<0)
dy=10;
}
class Re extends Thread
{
public void run()
{
while(true)
{
repaint();
try
{
this.sleep(40);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP){
dy=-10; dx = 0;}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
dy=10; dx = 0;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
dx=-10; dy=0;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
dx=10; dy=0;
}
}
public void keyReleased(KeyEvent e) {
}
} 展开
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class paintdemo1 extends JFrame
{
public void langchFrame()
{
this.setTitle("动画练习");
this.setBounds(300, 250, 400, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
MyPanel jp = new MyPanel();
add(jp);
this.setVisible(true);
jp.langchJp();
}
public static void main(String[] args){
paintdemo1 p = new paintdemo1();
p.langchFrame();
}
}
class MyPanel extends JPanel implements KeyListener
{
Image img = Tollsdemo.getImage("image/滑稽.png"); //一个获取图片在工具类
int x = 100; int y = 100;
int dx = 10; int dy = 0;
public void langchJp ()
{
new Re().start();
this.addKeyListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
Color c = new Color(200,0,0);
g.setColor(c);
g.drawImage(img,x,y,null);
x+=dx; y+=dy;
if(x>330)
dx=-10;
if(x<0)
dx=10;
if(y>310)
dy=-10;
if(y<0)
dy=10;
}
class Re extends Thread
{
public void run()
{
while(true)
{
repaint();
try
{
this.sleep(40);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP){
dy=-10; dx = 0;}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
dy=10; dx = 0;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
dx=-10; dy=0;
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
dx=10; dy=0;
}
}
public void keyReleased(KeyEvent e) {
}
} 展开
1个回答
展开全部
你的Java程序键盘事件不响应,是因为当前的焦点不在MyPanel上.
解决办法,在MyPanel类的langchJp函数里最后加一句this.requestFocus();就行了.
因为其它的地方不变,所以我这里就给出MyPanel类的langchJp函数更改后的代码,改动的地方见注释.
public void langchJp ()
{
new Re().start();
this.addKeyListener(this);
this.requestFocus();//这里加一句
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询