java swing中如何实现对于鼠标监听悬停事件
1个回答
展开全部
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.Timer;
public class MyFrame extends JFrame {
private Date lastTime;
public Date getLastTime() {
return lastTime;
}
public void setLastTime(Date lastTime) {
this.lastTime = lastTime;
}
public void createAndShowUI(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
lastTime = Calendar.getInstance().getTime();
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
lastTime = null;
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
setPreferredSize(new Dimension(300,200));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
final MyFrame frame = new MyFrame();
frame.createAndShowUI();
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(frame.getLastTime()!=null){
Date last = frame.getLastTime();
Date now = Calendar.getInstance().getTime();
if((now.getTime()-last.getTime())>3000){
System.out.println("悬浮了3秒");
}
}
}
};
int delay = 1000;
Timer timer = new Timer(delay,listener);
timer.start();
}
}
from@网页链接
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询