⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 20080921.txt

📁 用java语言实现倒计时功能
💻 TXT
字号:
一、
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;

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

public class TimeCount extends JFrame implements Runnable
{
    private static final long serialVersionUID = 3484639121843864203L;

    private JLabel lbNow, lbNowTitle, lbLeftSecTitle, lbLeftSec, lbLeftMinTitle, lbLeftMin;

    private Thread clocker;

    public TimeCount()
    {
        this.setLayout(new FlowLayout());
        this.setResizable(false);
        this.setSize(200, 150);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocation(830, 580);
        initUI();
        clocker = new Thread(this);
        clocker.start();
    }

    private void initUI()
    {
        lbNowTitle = new JLabel("当前时间为:");
        lbNow = new JLabel();
        lbLeftSecTitle = new JLabel("离考试结束还有:");
        lbLeftSec = new JLabel();
        lbLeftMinTitle = new JLabel("离考试结束还有:");
        lbLeftMin = new JLabel("");
        
        this.add(lbNowTitle);
        this.add(lbNow);
        this.add(lbLeftSecTitle);
        this.add(lbLeftSec);
        this.add(lbLeftMinTitle);
        this.add(lbLeftMin);
    }

    public void run()
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
        Calendar startCalendar = Calendar.getInstance();
        long startTime = startCalendar.getTime().getTime(); // 获得开始时候的那个时间点
        long endTime = startTime + 2 * 60 * 60 * 1000; // 从开始时刻开始 加两个小时
        long nowTime, leftTime, leftSec, leftMin;
        Calendar now;
        
        while(true)
        {
            now = Calendar.getInstance();
            nowTime = now.getTime().getTime();
            leftTime = endTime - nowTime;
            leftSec = leftTime / 1000;
            leftMin = leftTime / (60 * 1000);
            
            lbNow.setText(dateFormat.format(now.getTime()));
            lbLeftSec.setText(leftSec + " 秒");    //若后面不加字符,可以lbLeftSec.setText(leftSec + ""); 不用String.valueOf
            lbLeftMin.setText(leftMin + " 分钟");
            
            if(leftSec == 0)
            {
                JOptionPane.showMessageDialog(this, "对不起!答题时间已到!", "提示", JOptionPane.OK_OPTION);
                break;
            }
            
            try
            {
                Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args)
    {
        new TimeCount();
    }
}

二、
import java.applet.Applet;
import java.awt.*;
import java.util.Calendar;

public class Test2 extends Applet {
    public void paint(Graphics g) {
        Calendar now = Calendar.getInstance();
        Calendar future = Calendar.getInstance();
        future.set(2005, Calendar.JANUARY, 1);

        int a1 = now.get(Calendar.YEAR);
        int a2 = now.get(Calendar.MONTH) + 1;
        int a3 = now.get(Calendar.DAY_OF_MONTH);

        int days = getDays(now, future);
        g.drawString("Today is " + a1 + "-" + a2 + "-" + a3, 10, 20);
        g.drawString(days + " days to 2005-01-01", 10, 40);
    }

    private int getDays(Calendar from, Calendar to) {
        int i = 0;
        while (! from.equals(to)) {

            if (from.before(to)) {
                from.add(Calendar.DATE, 1);
                i++;
            } else {
                from.add(Calendar.DATE, -1);
                i--;
            }
        }
        return i;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -