java myeclipse中如何接收输入方向键?给个例子,简单点的,也可加Q谢谢。
1个回答
2012-07-01 · 知道合伙人软件行家
关注
展开全部
import java.awt.Frame;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class test extends Frame{
public void lauch(){
this.setLocation(200, 200);
this.setSize(200,200);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
}
private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
System.out.println("向上移动");
}
if(key == KeyEvent.VK_DOWN) {
System.out.println("向下移动");
}
if(key == KeyEvent.VK_LEFT) {
System.out.println("向左移动");
}
if(key == KeyEvent.VK_RIGHT) {
System.out.println("向右移动");
}
}
}
public static void main(String[] args) {
new test().lauch();
}
}
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class test extends Frame{
public void lauch(){
this.setLocation(200, 200);
this.setSize(200,200);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
}
private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP) {
System.out.println("向上移动");
}
if(key == KeyEvent.VK_DOWN) {
System.out.println("向下移动");
}
if(key == KeyEvent.VK_LEFT) {
System.out.println("向左移动");
}
if(key == KeyEvent.VK_RIGHT) {
System.out.println("向右移动");
}
}
}
public static void main(String[] args) {
new test().lauch();
}
}
追问
你这个怎么是图形界面的啊,我只要控制台的,再给个例子吧,控制台的,简单点都可以
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询