Java 倒计时【收藏】

Java 倒计时【收藏】
http://haidii.javaeye.com/blog/284821

最近用java编写了一个倒计时的程序,贴出来与大家共享。
先看看截图吧:
1.开始打开程序时的界面
Java 倒计时【收藏】_第1张图片

2.设定好时间,点击“开始计时”时的界面
Java 倒计时【收藏】_第2张图片

3.定时时间到时的界面
Java 倒计时【收藏】_第3张图片

4.退出程序时的界面
Java 倒计时【收藏】_第4张图片

5.同时,本程序具有检测功能,对您的时间输入进行检测
Java 倒计时【收藏】_第5张图片

下面是源码:

  1. package test;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.sql.Timestamp;
  12. import java.util.Calendar;
  13. import java.util.GregorianCalendar;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JTextField;
  20. import javax.swing.UIManager;
  21. import javax.swing.UnsupportedLookAndFeelException;
  22. public class Countdown extends JFrame implements ActionListener{
  23.     private static final long serialVersionUID = -3439634693767815667L;
  24.     private JLabel startTime,pastTime,label1,label2,lyear,lmonth,lday,lhour,lmin,lsec;
  25.     private JTextField year,month,day,hour,min,sec;
  26.     private JButton ok;
  27.     private String str;
  28.     private int y,m,d,h,mi,s;
  29.     private boolean runflag=true
  30.     
  31.     public Countdown(){
  32.         super("倒计时牌");
  33.         init();
  34.         this.setSize(450,150);
  35.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  36.         int h = (int) d.getHeight() / 3;
  37.         int w = (int) d.getWidth() / 3;
  38.         this.setLocation(w, h);
  39.         this.setVisible(true);
  40.         this.setResizable(false);
  41.         this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  42.         this.addWindowListener(new WindowAdapter(){
  43.             public void windowClosing(WindowEvent e) {
  44.                 int exit=JOptionPane.showConfirmDialog(Countdown.this"确定退出吗?""退出", JOptionPane.OK_CANCEL_OPTION);
  45.                 if(exit==JOptionPane.OK_OPTION)
  46.                     System.exit(0);
  47.             }
  48.         });
  49.     }
  50.     private void init(){
  51.         label1=new JLabel("现在距规定时间还有:");
  52.         label2=new JLabel();
  53.         startTime=new JLabel("开始时间:");
  54.         pastTime=new JLabel("已过时间:");
  55.         
  56.         JPanel north=new JPanel();
  57.         north.setLayout(new BorderLayout());
  58.         north.add(label1,BorderLayout.CENTER);
  59.         north.add(startTime,BorderLayout.NORTH);
  60.         
  61.         this.add(north,BorderLayout.NORTH);
  62.         this.add(label2,BorderLayout.CENTER);
  63.         
  64.         JPanel south=new JPanel();
  65.         south.setLayout(new BorderLayout());
  66.         JPanel south_center=new JPanel();
  67.         Calendar c=Calendar.getInstance();
  68.         year=new JTextField(c.get(Calendar.YEAR)+"",6);
  69.         month=new JTextField(c.get(Calendar.MONTH)+1+"",4);
  70.         day=new JTextField(c.get(Calendar.DAY_OF_MONTH)+"",4);
  71.         hour=new JTextField(c.get(Calendar.HOUR_OF_DAY)+"",4);
  72.         min=new JTextField(c.get(Calendar.MINUTE)+"",4);
  73.         sec=new JTextField(c.get(Calendar.SECOND)+"",4);
  74.         lyear=new JLabel("年");
  75.         lmonth=new JLabel("月");
  76.         lday=new JLabel("日");
  77.         lhour=new JLabel("时");
  78.         lmin=new JLabel("分");
  79.         lsec=new JLabel("秒");
  80.         ok=new JButton("开始计时");
  81.         south_center.add(year);
  82.         south_center.add(lyear);
  83.         south_center.add(month);
  84.         south_center.add(lmonth);
  85.         south_center.add(day);
  86.         south_center.add(lday);
  87.         south_center.add(hour);
  88.         south_center.add(lhour);
  89.         south_center.add(min);
  90.         south_center.add(lmin);
  91.         south_center.add(sec);
  92.         south_center.add(lsec);
  93.         south_center.add(ok);
  94.         south.add(south_center,BorderLayout.CENTER);
  95.         south.add(pastTime,BorderLayout.NORTH);
  96.         this.add(south,BorderLayout.SOUTH);
  97.         
  98.         ok.addActionListener(this);
  99.         label1.setFont(new Font("宋体",Font.BOLD,20));
  100.         label2.setFont(new Font("宋体",Font.BOLD,26));
  101.         label1.setForeground(new Color(154,0,255));
  102.         label2.setForeground(Color.RED);
  103.     }
  104.     
  105.     public synchronized void stopthread(){
  106.         runflag=false;
  107.     }
  108.     
  109.     public synchronized void startthread(){
  110.         runflag=true;
  111.     }
  112.     public synchronized boolean getrunflag(){
  113.         return runflag;
  114.     }
  115.     
  116.     class MyThread extends Thread{
  117.         Calendar target=null;
  118.         Calendar startTime=null;
  119.         long targetInMill=0;
  120.         long startInMill=0;
  121.         
  122.         private MyThread(int y,int m,int d,int h,int mi,int s){
  123.             target=new GregorianCalendar(y,m-1,d,h,mi,s); 
  124.             startTime=Calendar.getInstance();
  125.             startInMill=startTime.getTimeInMillis();
  126.             targetInMill=target.getTimeInMillis();
  127.         }
  128.         public void run(){
  129.             while(runflag){
  130.                 runflag=true;
  131.                 Calendar today=new GregorianCalendar(); 
  132.                 long todayInMill=today.getTimeInMillis();
  133.                 long dd=targetInMill-todayInMill;
  134.                 long dis=dd/1000;
  135.                 long ms=(dd/100)%10;
  136.                 long mms=(dd/10)%10;
  137.                 String d;
  138.                 int day=(int) (dis/(3600*24));
  139.                 int hour=(int) (dis%(3600*24)/3600);
  140.                 int min=(int) (dis%3600/60);
  141.                 int sec=(int) (dis%60);
  142.                 if(day<10)
  143.                     d="  "+day;
  144.                 else if(day>=10&&day<100)
  145.                     d=" "+day;
  146.                 else
  147.                     d=day+"";
  148.                 String s=d+" 天 "+intToString(hour)+" 小时 "+intToString(min)+" 分 "+intToString(sec)+" 秒"+ms+" "+mms;
  149.                 label2.setText(s);
  150.                 pastTime();
  151.                 if(dd<=0){
  152.                     ok.setText("重新开始");
  153.                     JOptionPane.showMessageDialog(Countdown.this"时间到");
  154.                     break;
  155.                 }
  156.                 try {
  157.                     Thread.sleep(10);
  158.                 } catch (InterruptedException e) {
  159.                     e.printStackTrace();
  160.                 }
  161.             }
  162.         }
  163.         
  164.         private void pastTime(){
  165.             Calendar today=new GregorianCalendar(); 
  166.             long nowInMill=today.getTimeInMillis();
  167.             long dd=nowInMill-startInMill;
  168.             long dis=dd/1000;
  169.             long ms=(dd/100)%10;
  170.             long mms=(dd/10)%10;
  171.             String d;
  172.             int day=(int) (dis/(3600*24));
  173.             int hour=(int) (dis%(3600*24)/3600);
  174.             int min=(int) (dis%3600/60);
  175.             int sec=(int) (dis%60);
  176.             if(day<10)
  177.                 d="  "+day;
  178.             else if(day>=10&&day<100)
  179.                 d=" "+day;
  180.             else
  181.                 d=day+"";
  182.             String s=d+" 天 "+intToString(hour)+" 小时 "+intToString(min)+" 分 "+intToString(sec)+" 秒"+ms+" "+mms;
  183.             pastTime.setText("已过时间:"+s);
  184.         }
  185.     }
  186.     public String intToString(int i){
  187.         if(i<10){
  188.             str="0"+i;
  189.             return str;
  190.         }
  191.         else 
  192.             return i+"";
  193.     }
  194.     
  195.     public void actionPerformed(ActionEvent e) {
  196.         String str=e.getActionCommand();
  197.         MyThread t=null;
  198.         if(str.equals("开始计时")){
  199.             try {
  200.                 this.startthread();
  201.                 startTime.setText("开始时间:"+new Timestamp(System.currentTimeMillis()));
  202.                 y=Integer.parseInt(year.getText());
  203.                 m=Integer.parseInt(month.getText());
  204.                 d=Integer.parseInt(day.getText());
  205.                 h=Integer.parseInt(hour.getText());
  206.                 mi=Integer.parseInt(min.getText());
  207.                 s=Integer.parseInt(sec.getText());
  208.                 year.setEditable(false);
  209.                 month.setEditable(false);
  210.                 day.setEditable(false);
  211.                 hour.setEditable(false);
  212.                 min.setEditable(false);
  213.                 sec.setEditable(false);
  214. //              ok.setEnabled(false);
  215.                 ok.setText("停止");
  216.                 t=new MyThread(y,m,d,h,mi,s);
  217.                 t.start();
  218.             } catch (NumberFormatException e1) {
  219.                 JOptionPane.showMessageDialog(this"对不起,请输入数字!""错误提示", JOptionPane.ERROR_MESSAGE);
  220.             }
  221.         }
  222.         if(str.equals("停止") || str.equals("重新开始")){
  223.             this.stopthread();
  224.             year.setEditable(true);
  225.             month.setEditable(true);
  226.             day.setEditable(true);
  227.             hour.setEditable(true);
  228.             min.setEditable(true);
  229.             sec.setEditable(true);
  230.             ok.setText("开始计时");
  231.         }
  232.         
  233.     }
  234.     
  235.     public static void main(String[] args) {
  236.         try {
  237.             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  238.             new Countdown();
  239.         } catch (ClassNotFoundException e) {
  240.             e.printStackTrace();
  241.         } catch (InstantiationException e) {
  242.             e.printStackTrace();
  243.         } catch (IllegalAccessException e) {
  244.             e.printStackTrace();
  245.         } catch (UnsupportedLookAndFeelException e) {
  246.             e.printStackTrace();
  247.         }
  248.     }
  249. }

你可能感兴趣的:(Java 倒计时【收藏】)