关于javaKeyEvent按键反应的。懂的前辈帮忙看下
发布网友
发布时间:2022-05-01 13:55
我来回答
共2个回答
热心网友
时间:2023-10-16 12:39
你把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) {
}
}
热心网友
时间:2023-10-16 12:39
应该写 e.getKeyCode 上是KeyEvent_VK.UP 以此类推
热心网友
时间:2023-10-16 12:39
你把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) {
}
}
热心网友
时间:2023-10-16 12:39
应该写 e.getKeyCode 上是KeyEvent_VK.UP 以此类推
热心网友
时间:2023-10-16 12:39
你把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) {
}
}
热心网友
时间:2023-10-16 12:39
应该写 e.getKeyCode 上是KeyEvent_VK.UP 以此类推
热心网友
时间:2023-10-16 12:39
你把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) {
}
}
热心网友
时间:2023-10-16 12:39
应该写 e.getKeyCode 上是KeyEvent_VK.UP 以此类推