3个回答
2013-08-17
展开全部
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author 阿迪
*/
public class HOHO extends JFrame implements KeyListener{
private JTextField t;
private boolean left = false;
private boolean right = false;
private boolean up = false;
private boolean down = false;
public HOHO() {
JPanel p = new JPanel();
this.add(p);
p.setLayout(null);
t = new JTextField();
t.setEditable(false);
p.add(t);
t.setSize(100,30);
t.setLocation(200,200);
this.setFocusable(true);
this.addKeyListener(this);
this.setSize(480,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=true; break;
case KeyEvent.VK_UP : up= true;break;
case KeyEvent.VK_DOWN :down = true; break;
case KeyEvent.VK_RIGHT : right = true;break;
}
if(up==false&&down==false&&left==false&&right==false) {
t.setText("什么也没按");
}
else if(up==true&&down==false&&left==false&&right==false) {
t.setText("上");
}
else if(up==false&&down==true&left==false&&right==false) {
t.setText("下");
}
else if(up==false&&down==true&left==true&&right==false) {
t.setText("左下");
}
else if(up==true&&down==false&left==false&&right==true) {
t.setText("右上");
}
else if(up==true&&down==false&left==true&&right==false) {
t.setText("左上");
}
else if(up==false&&down==true&left==false&&right==true) {
t.setText("右下");
}
else if(up==false&&down==false&left==true&&right==false) {
t.setText("左");
}
else if(up==false&&down==false&left==false&&right==true) {
t.setText("右");
}
}
public void keyReleased(KeyEvent e) {
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=false; break;
case KeyEvent.VK_UP : up= false;break;
case KeyEvent.VK_DOWN :down = false; break;
case KeyEvent.VK_RIGHT : right = false;break;
}
}
public static void main(String[] args){
new HOHO();
}
} 这样就可以了。HOHO!!!
import java.awt.event.KeyListener;
import javax.swing.JFrame;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author 阿迪
*/
public class HOHO extends JFrame implements KeyListener{
private JTextField t;
private boolean left = false;
private boolean right = false;
private boolean up = false;
private boolean down = false;
public HOHO() {
JPanel p = new JPanel();
this.add(p);
p.setLayout(null);
t = new JTextField();
t.setEditable(false);
p.add(t);
t.setSize(100,30);
t.setLocation(200,200);
this.setFocusable(true);
this.addKeyListener(this);
this.setSize(480,480);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=true; break;
case KeyEvent.VK_UP : up= true;break;
case KeyEvent.VK_DOWN :down = true; break;
case KeyEvent.VK_RIGHT : right = true;break;
}
if(up==false&&down==false&&left==false&&right==false) {
t.setText("什么也没按");
}
else if(up==true&&down==false&&left==false&&right==false) {
t.setText("上");
}
else if(up==false&&down==true&left==false&&right==false) {
t.setText("下");
}
else if(up==false&&down==true&left==true&&right==false) {
t.setText("左下");
}
else if(up==true&&down==false&left==false&&right==true) {
t.setText("右上");
}
else if(up==true&&down==false&left==true&&right==false) {
t.setText("左上");
}
else if(up==false&&down==true&left==false&&right==true) {
t.setText("右下");
}
else if(up==false&&down==false&left==true&&right==false) {
t.setText("左");
}
else if(up==false&&down==false&left==false&&right==true) {
t.setText("右");
}
}
public void keyReleased(KeyEvent e) {
int i = e.getKeyCode();
switch(i) {
case KeyEvent.VK_LEFT : left=false; break;
case KeyEvent.VK_UP : up= false;break;
case KeyEvent.VK_DOWN :down = false; break;
case KeyEvent.VK_RIGHT : right = false;break;
}
}
public static void main(String[] args){
new HOHO();
}
} 这样就可以了。HOHO!!!
2013-08-17
展开全部
唧唧歪歪
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-17
展开全部
键盘码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询