绘图技术与事件处理机制---鼠标事件

    xiaoxiao2024-12-24  10

    import java.awt.*; import javax.swing.*; import java.awt.event.*; public class L8_5 extends JFrame{ Wdmb5 mb=null; public static void main(String[] args){ L8_5 lx=new L8_5(); } public L8_5(){ mb=new Wdmb5(); this.add(mb); this.addKeyListener(mb); this.addMouseListener(mb); this.addMouseMotionListener(mb); this.setSize(400,300); this.setLocation(300,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class Wdmb5 extends JPanel implements KeyListener,MouseListener,MouseMotionListener{ //MouseMotionListener和MouseListener的区别在于它是实时显示鼠标的操作 public void paint(Graphics g){ super.paint(g); } public void keyTyped(KeyEvent e){} public void keyPressed(KeyEvent e){ System.out.println(e.getKeyChar()+"键被按下"); } public void keyReleased(KeyEvent e){} //MouseListener的三个抽象方法 public void mouseClicked(MouseEvent e){ System.out.println("鼠标点击的位置是:"); System.out.println("横坐标:"+e.getX()); System.out.println("纵坐标:"+e.getY()); } public void mousePressed(MouseEvent e){ System.out.println("鼠标被按下"); } public void mouseRelesed(MouseEvent e){ System.out.println("鼠标被松开"); } public void mouseEntered(MouseEvent e){ System.out.println("鼠标移动到界面中"); } public void mouseExited(MouseEvent e){ System.out.println("鼠标离开界面"); } //MouseMotionListener的两个抽象方法 public void mouseDragged(MouseEvent e){//鼠标按键在组件上按下并拖动时调用。 System.out.println("鼠标当前位置是:"); System.out.println("横坐标:"+e.getX()); System.out.println("纵坐标:"+e.getY()); } public void mouseMoved(MouseEvent e){//鼠标光标移动到组件上但无按键按下时调用。 System.out.println("鼠标当前位置是:"); System.out.println("横坐标:"+e.getX()); System.out.println("纵坐标:"+e.getY()); } }
    转载请注明原文地址: https://ju.6miu.com/read-1294946.html
    最新回复(0)