当前位置: 代码迷 >> J2SE >> 求高手解答!多谢
  详细解决方案

求高手解答!多谢

热度:246   发布时间:2016-04-24 01:33:19.0
求高手解答!谢谢!
package first;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo1 extends JFrame{

MyPanel mp=null;
public static void main(String[] args) {
Demo1 demo1=new Demo1();
}
public Demo1()
{
mp=new MyPanel();

this.add(mp);
this.addMouseListener(mp);
this.addKeyListener(mp);
this.addMouseMotionListener(mp);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}
}
class MyPanel extends Panel implements MouseListener,KeyListener,MouseMotionListener
{
public void paint(Graphics g)
{
super.paint(g);
}

public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("鼠标点击了 x="+arg0.getX()+"y"+arg0.getY());
}

public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("鼠标来了");
}

public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}


public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.getKeyChar()+"键被按下");
}

public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("鼠标到了 x+"+arg0.getX());
}
}

代码无错误!
运行后,鼠标监听没反应;键盘监听有反应;

------解决方案--------------------
class MyPanel extends Panel implements改为class MyPanel extends JPanel implements
  相关解决方案