关于javaKeyEvent按键反应的。懂的前辈帮忙看下
上代码:packageDraw;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publiccla...
上代码:
package Draw;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test2 extends JFrame{
/**
* @param args
*/
dong d=null;
public test2(){
d=new dong();
d.addKeyListener(d);
this.addKeyListener(d);
this.add(d);
this.setSize(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
test2 ts=new test2();
}
}
class dong extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 200,200);
g.setColor(Color.BLACK);
g.fillOval(x, y, 10,10);
}
//键被按下
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_UP||e.getKeyChar()==KeyEvent.VK_W){
System.out.print(e.getKeyChar());
y--;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_DOWN||e.getKeyChar()==KeyEvent.VK_S){
y++;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_RIGHT||e.getKeyChar()==KeyEvent.VK_D){
x++;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_LEFT||e.getKeyChar()==KeyEvent.VK_A){
x--;
repaint();
}
}
还有一点东西超了不过不是重点。就是两个空的keyReleased和keyTyped方法
问题是为什么按WSDA有效果,按上下左右就没效果呢System.out.print(e.getKeyChar());也没有
还有最后一个问题怎么让它能识别小写状态下的wsad按键。我只有按大写才有效果怎么改呢 展开
package Draw;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test2 extends JFrame{
/**
* @param args
*/
dong d=null;
public test2(){
d=new dong();
d.addKeyListener(d);
this.addKeyListener(d);
this.add(d);
this.setSize(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
test2 ts=new test2();
}
}
class dong extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 200,200);
g.setColor(Color.BLACK);
g.fillOval(x, y, 10,10);
}
//键被按下
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_UP||e.getKeyChar()==KeyEvent.VK_W){
System.out.print(e.getKeyChar());
y--;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_DOWN||e.getKeyChar()==KeyEvent.VK_S){
y++;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_RIGHT||e.getKeyChar()==KeyEvent.VK_D){
x++;
repaint();
}
if(e.getKeyChar()==KeyEvent.VK_LEFT||e.getKeyChar()==KeyEvent.VK_A){
x--;
repaint();
}
}
还有一点东西超了不过不是重点。就是两个空的keyReleased和keyTyped方法
问题是为什么按WSDA有效果,按上下左右就没效果呢System.out.print(e.getKeyChar());也没有
还有最后一个问题怎么让它能识别小写状态下的wsad按键。我只有按大写才有效果怎么改呢 展开
2个回答
展开全部
你把keyPressed函数里所有的e.getKeyChar()函数改成e.getKeyCode()函数就行了.
因为e.getKeyChar()函数只对字符键起作用,对控制键不亩态起作用.e.getKeyCode()函数既能识别控制键也能识别小写状态下的wsad按键.
完整的程序如下:
package Draw;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test2 extends JFrame{
/**
* @param args
*/
dong d=null;
public test2(){
d=new dong();
d.addKeyListener(d);
this.addKeyListener(d);
this.add(d);
this.setSize(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生则咐成方法存根
test2 ts=new test2();
}
}
class dong extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 200,200);
g.setColor(Color.BLACK);
g.fillOval(x, y, 10,10);
}
//迅盯源键被按下
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP||e.getKeyCode()==KeyEvent.VK_W){
System.out.print(e.getKeyCode());
y--;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN||e.getKeyCode()==KeyEvent.VK_S){
y++;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT||e.getKeyCode()==KeyEvent.VK_D){
x++;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_LEFT||e.getKeyCode()==KeyEvent.VK_A){
x--;
repaint();
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
因为e.getKeyChar()函数只对字符键起作用,对控制键不亩态起作用.e.getKeyCode()函数既能识别控制键也能识别小写状态下的wsad按键.
完整的程序如下:
package Draw;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class test2 extends JFrame{
/**
* @param args
*/
dong d=null;
public test2(){
d=new dong();
d.addKeyListener(d);
this.addKeyListener(d);
this.add(d);
this.setSize(200,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
// TODO 自动生则咐成方法存根
test2 ts=new test2();
}
}
class dong extends JPanel implements KeyListener{
int x=10;
int y=10;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, 200,200);
g.setColor(Color.BLACK);
g.fillOval(x, y, 10,10);
}
//迅盯源键被按下
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_UP||e.getKeyCode()==KeyEvent.VK_W){
System.out.print(e.getKeyCode());
y--;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN||e.getKeyCode()==KeyEvent.VK_S){
y++;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT||e.getKeyCode()==KeyEvent.VK_D){
x++;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_LEFT||e.getKeyCode()==KeyEvent.VK_A){
x--;
repaint();
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |