在JAVA中怎样判断按下两个键
1个回答
2017-02-11 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
方法如下
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
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();
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
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();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询