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@网页链接
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询