我的java程序为何鼠标拖动和键盘事件无法响应
importjava.awt.event.*;importjava.awt.*;publicclassAppWindowextendsFrame{intmouseX=0,...
import java.awt.event.*;
import java.awt.*;
public class AppWindow extends Frame {
int mouseX =0,mouseY =0;
String keymsg ="";
String mousemsg ="";
public AppWindow() {
super("test");
addMouseListener(new MyMouseAdapter(this));
addKeyListener(new MyKeyAdapter(this));
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawString(keymsg,10,20);
g.drawString(mousemsg,mouseX,mouseY);
}
public static void main(String[] args) {
AppWindow aw =new AppWindow();
aw.setSize(new Dimension(400,300));
aw.setVisible(true);
}
}
class MyMouseAdapter extends MouseAdapter {
AppWindow aw;
public MyMouseAdapter(AppWindow aw) {
this.aw =aw;
}
public void mousePressed(MouseEvent me) {
int button =me.getButton();
aw.mouseX =me.getX();
aw.mouseY =me.getY();
mousemsg ="down";
aw.repaint();
}
public void mouseReleased(MouseEvent me) {
int button =me.getButton();
aw.mouseX =me.getX();
aw.mouseY =me.getY();
mousemsg ="up";
aw.repaint();
}
//鼠标拖动
public void mouseDragged(MouseEvent me) {
aw.mouseX =me.getX();
aw.mouseY =me.getY();
aw.mousemsg ="*";
aw.repaint();
}
}
class MyKeyAdapter extends KeyAdapter {
AppWindow aw;
public MyKeyAdapter(AppWindow aw) {
this.aw =aw;
}
//键盘事件
public void keyTyped(KeyEvent ke) {
int key =ke.getKeyCode();
switch(key) {
case KeyEvent.VK_UP:
aw.keymsg +="up";
break;
case KeyEvent.VK_DOWN:
aw.keymsg +="down";
break;
}
aw.repaint();
}
} 展开
import java.awt.*;
public class AppWindow extends Frame {
int mouseX =0,mouseY =0;
String keymsg ="";
String mousemsg ="";
public AppWindow() {
super("test");
addMouseListener(new MyMouseAdapter(this));
addKeyListener(new MyKeyAdapter(this));
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.drawString(keymsg,10,20);
g.drawString(mousemsg,mouseX,mouseY);
}
public static void main(String[] args) {
AppWindow aw =new AppWindow();
aw.setSize(new Dimension(400,300));
aw.setVisible(true);
}
}
class MyMouseAdapter extends MouseAdapter {
AppWindow aw;
public MyMouseAdapter(AppWindow aw) {
this.aw =aw;
}
public void mousePressed(MouseEvent me) {
int button =me.getButton();
aw.mouseX =me.getX();
aw.mouseY =me.getY();
mousemsg ="down";
aw.repaint();
}
public void mouseReleased(MouseEvent me) {
int button =me.getButton();
aw.mouseX =me.getX();
aw.mouseY =me.getY();
mousemsg ="up";
aw.repaint();
}
//鼠标拖动
public void mouseDragged(MouseEvent me) {
aw.mouseX =me.getX();
aw.mouseY =me.getY();
aw.mousemsg ="*";
aw.repaint();
}
}
class MyKeyAdapter extends KeyAdapter {
AppWindow aw;
public MyKeyAdapter(AppWindow aw) {
this.aw =aw;
}
//键盘事件
public void keyTyped(KeyEvent ke) {
int key =ke.getKeyCode();
switch(key) {
case KeyEvent.VK_UP:
aw.keymsg +="up";
break;
case KeyEvent.VK_DOWN:
aw.keymsg +="down";
break;
}
aw.repaint();
}
} 展开
1个回答
展开全部
以下代码,仅供参考,至于具体原因,自己去查:
import java.awt.event.*;
import java.awt.*;
public class AppWindow extends Frame implements MouseMotionListener,
KeyListener {
private static final long serialVersionUID = 1L;
int mouseX = 0, mouseY = 0;
String keymsg = "";
String mousemsg = "";
public AppWindow() {
super("test");
addMouseListener(new MyMouseAdapter(this));
addKeyListener(new MyKeyAdapter(this));
this.addMouseMotionListener(this);
this.addKeyListener(this);
}
public void paint(Graphics g) {
g.setColor(Color.blue);
简塌 g.drawString(keymsg, 10, 20);
g.drawString(mousemsg, mouseX, mouseY);
}
public void repaint_thread() {
new Thread(new Runnable() {
@SuppressWarnings("static-access")
@Override
public void run() {
while (true) {
try {
repaint();
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
拦岁圆 }).start();
}
public static void main(String[] args) {
AppWindow aw = new AppWindow();
aw.setSize(new Dimension(400, 300));
aw.setVisible(true);
aw.repaint_thread();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
mouseX = e.getX();
mouseY = e.getY();
mousemsg = "*";
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.getKeyCode()); //响应按键按下
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.getKeyCode()); //响应按键松开
}
@Override
public void keyTyped(KeyEvent ke) {
}
}
@SuppressWarnings("unused")
class MyMouseAdapter extends MouseAdapter {
AppWindow aw;
public MyMouseAdapter(AppWindow aw) {
this.aw = aw;
}
public void mousePressed(MouseEvent me) {
int button = me.getButton();
雀前 aw.mouseX = me.getX();
aw.mouseY = me.getY();
aw.mousemsg = "down";
}
public void mouseReleased(MouseEvent me) {
int button = me.getButton();
aw.mouseX = me.getX();
aw.mouseY = me.getY();
aw.mousemsg = "up";
}
}
class MyKeyAdapter extends KeyAdapter {
AppWindow aw;
public MyKeyAdapter(AppWindow aw) {
this.aw = aw;
}
// 键盘事件
public void keyTyped(KeyEvent ke) {
int key = ke.getKeyCode();
switch (key) {
case KeyEvent.VK_UP:
aw.keymsg += "up";
break;
case KeyEvent.VK_DOWN:
aw.keymsg += "down";
break;
}
aw.repaint();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询