import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 例98
{
public void designFrame() {
JFrame mywindow=new JFrame("MouseDemo");
Container container=mywindow.getContentPane();
mywindow.setVisible(true);
mywindow.setBounds(0,0,300,100);
container.addMouseListener(new MouseHandler());
container.addMouseMotionListener(new MouseHandler());
mywindow.setDefaultCloseOperation(mywindow.EXIT_ON_CLOSE);
}
class MouseHandler implements MouseListener,MouseMotionListener{
public void mouseClicked(MouseEvent e) {
int x=e.getX();
int y=e.getY();
System.out.println("鼠标点击的位置X:"+x+"\ty:"+y);
}
public void mousePressed(MouseEvent e) {
int i=e.getButton();
if(i==MouseEvent.BUTTON1);
System.out.println("按下鼠标左键");
if(i==MouseEvent.BUTTON2);
System.out.println("按下鼠标中建");
if(i==MouseEvent.BUTTON3);
System.out.println("按下鼠标右键");
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {
System.out.println("鼠标移出窗口");
}
public void mouseDragged(MouseEvent e) {
System.out.println("鼠标在窗体上拖曳");
}
public void mouseMoved(MouseEvent e) {
System.out.println("鼠标在窗体上移动");
}
}
public static void main(String[] args)
{
例98 myframe=new 例98();
myframe.designFrame();// TODO Auto-generated method stub
}
}
文章来源:https://www.toymoban.com/news/detail-662015.html
文章来源地址https://www.toymoban.com/news/detail-662015.html
到了这里,关于Java程序设计——鼠标监视器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!