本文概述
- MouseListener接口的方法
- Java MouseListener示例
- Java MouseListener示例2
MouseListener接口的方法 在MouseListener界面中找到的5种方法的签名如下:
public abstract void mouseClicked(MouseEvent e);
public abstract void mouseEntered(MouseEvent e);
public abstract void mouseExited(MouseEvent e);
public abstract void mousePressed(MouseEvent e);
public abstract void mouseReleased(MouseEvent e);
Java MouseListener示例
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20, 50, 100, 20);
add(l);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
输出:
文章图片
Java MouseListener示例2
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample2 extends Frame implements MouseListener{
MouseListenerExample2(){
addMouseListener(this);
setSize(300, 300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(), e.getY(), 30, 30);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new MouseListenerExample2();
}
}
【Java MouseListener接口】输出:
文章图片
推荐阅读
- Java MouseMotionListener接口
- 39个无需注册的最佳免费电子书下载网站推荐合集
- 路由器上的WPS按钮是什么(它是如何工作的?)
- 如何在Mac上截屏(Mac截屏的7种最佳方法)
- Windows 10中启动时的Adob??e GC Invoker Utility是什么(提示和指南)
- 推广定制T恤的8个最有效的想法(提示和技巧)
- win10能用office2003吗?
- win10安装office2003的办法
- 最新的!win10企业版联网激活密钥免费分享