java----利用线程制作时间跟随鼠标动

package com.lideng.work320;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
 * 实现时间功能
 * @author 李登
 *
 */
public class TimeDome extends JFrame implements ActionListener {

	private JLabel time=null;
	private MyPanel jp=null;
	public static Image img=Toolkit.getDefaultToolkit().createImage(TimeDome.class.getResource("/rose.jpg"));
	private DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static void main(String[] args) {
		new TimeDome();
	}
	
	public TimeDome() {
		
		
		//创建定时器
		 Timer  t=new Timer(1000, this);
		 t.start();
		 jp=new MyPanel();
			
		 jp.setLayout(null);
		//设置时间到面板
		time=new JLabel(format.format(new Date()));
		
		time.setFont(new Font("微软雅黑",Font.ITALIC,36));
		
		time.setSize(400, 200);
		time.setLocation(15, 10);
		time.setForeground(Color.RED);
		time.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		jp.add(time);
		this.add(jp);
		
		this.setTitle("JAVA NOW TIME!");
		this.setSize(400, 300);
		//this.setLocationRelativeTo(null);
		this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width-400, 0);
		this.setResizable(false);
		this.setUndecorated(true);
		this.setAlwaysOnTop(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		//初始化时间对象 添加到标签
		time.setText(format.format(new Date()));
	}
	
class MyPanel extends JPanel{
		
		 
		@Override
		protected void paintComponent(Graphics g) {
			
			g.drawImage(img,0,0,this);
			
		}
	}
	
}

  

转载于:https://www.cnblogs.com/qurui1997/p/10570192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值