zoukankan      html  css  js  c++  java
  • 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);
    			
    		}
    	}
    	
    }
    

      

  • 相关阅读:
    Java ConcurrentModificationException 异常分析与解决方案
    android studio 怎么做屏幕适配?
    java Class.getSimpleName() 的用法
    LeetCode——Remove Element
    用WidgeDuino创建一个SCADA(监控与数据採集)系统
    【Oracle】OCR的备份和恢复之导出导入
    Java大数类介绍
    POJ 1113 Wall 凸包
    OPENCV中滑动条的使用
    Android
  • 原文地址:https://www.cnblogs.com/qurui1997/p/10570192.html
Copyright © 2011-2022 走看看