1. 程式人生 > >Java Swing複習(12)案例:時間倒計時

Java Swing複習(12)案例:時間倒計時

1.TimeFrame.java
package www10m16;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class TimeFrame {
	private JFrame jf;
	private JLabel jl1;
	private JLabel jl2;
	private String title;
	private Date targetDate;
	public TimeFrame(String title,Date targetDate) {
		super();
		this.title = title;
		this.targetDate=targetDate;
		jf=new JFrame(title+"倒計時牌");
		jl1=new JLabel("距離"+title+"還有:");
		jl2=new JLabel("xx天xx時xx分xx秒");
	}
	
	private void init(){
		jf.add(jl1,BorderLayout.NORTH);
		jf.add(jl2);
	}
	
	private void setStyle(){
		jl1.setFont(new Font("宋體",Font.BOLD,20));
		jl2.setFont(new Font("宋體",Font.BOLD,40));
		jl1.setForeground(Color.BLUE);
		jl2.setForeground(Color.RED);
	}
	
	private void addEventHandler(){
		new Thread(){
			public void run() {
				//每1秒更新jl2上的時間
				while(true){
					//當前時間
					Date now=new Date();
					
					//目標時間距離當前時間差的秒
					long time=(targetDate.getTime()-now.getTime())/1000;
					
					if(time<=0){
//						jl2.setText("時間到!");
						
						
//						jf.remove(jl1);
//						jf.remove(jl2);
//						jl2=new JLabel("時間到!");
//						jf.add(jl2);
//						jf.setVisible(true);
						
						jf.dispose();
						new SuccessFrame(title,jf.getX(),jf.getY(),jf.getWidth(),jf.getHeight()).show();
						
						break;
					}
					
					//根據time算出天,時,分,秒
					int day=(int) (time/(60*60*24));
					int hour=(int) ((time%(60*60*24))/(60*60));
					int mins=(int) ((time%(60*60))/60);
					int second=(int) (time%60);
					
					jl2.setText(day+"天"+hour+"時"+mins+"分"+second+"秒");
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	
	public void show(){
		init();
		setStyle();
		addEventHandler();
		jf.pack();
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) throws Exception {
		String title=JOptionPane.showInputDialog("標題");
		String dateStr=JOptionPane.showInputDialog("目標時間");
		Date targetDate=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(dateStr);
		
		new TimeFrame(title,targetDate).show();
	}
}
2.SuccessFrame.java
package www10m16;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class SuccessFrame {
	private JFrame jf;
	private JLabel jl;
	private int x;
	private int y;
	private int width;
	private int height;
	
	public SuccessFrame(String title,int x,int y,int width,int height) {
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
		jf=new JFrame();
		jl=new JLabel(title+"時間到!");
	}
	
	private void init(){
		jf.add(jl);
	}
	
	private void addEventHandler(){
		new Thread(){
			public void run() {
				jf.setExtendedState(JFrame.NORMAL);
				jf.setAlwaysOnTop(true);
				
				for (int i = 0; i < 30; i++) {
					if(i%2==0){
						x+=15;
						y+=15;
					}else{
						x-=15;
						y-=15;
					}
					jf.setLocation(x, y);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
	
	public void show(){
		init();
		addEventHandler();
		jf.setSize(width, height);
		jf.setLocation(x, y);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

分析:(1)兩個輸入框獲取標題和目標時間

        String title=JOptionPane.showInputDialog("標題");
	String dateStr=JOptionPane.showInputDialog("目標時間");
    (2)獲取當前時間
//當前時間,這裡得到的是毫秒單位
					Date now=new Date();

(3)不顯示視窗

jf.dispose();

(4)執行緒改寫方法

new Thread(){
			public void run() {
				jf.setExtendedState(JFrame.NORMAL);
				jf.setAlwaysOnTop(true);
				
				for (int i = 0; i < 30; i++) {
					if(i%2==0){
						x+=15;
						y+=15;
					}else{
						x-=15;
						y-=15;
					}
					jf.setLocation(x, y);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();