怎么编写一个倒计时的java的程序?求具体步骤!

 我来答
月之子4
推荐于2017-11-22 · TA获得超过396个赞
知道小有建树答主
回答量:289
采纳率:0%
帮助的人:240万
展开全部
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个for循环,然后输出倒计时的数字,程序睡一秒,在输出倒计时数字,如此循环,简单吧,下面看程序:
public static void main(String[] args) {
for(int i=10;i>0;i--){
System.out.print(i+" ");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.err.print("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
在晴天的雨伞
2016-06-02 · TA获得超过6869个赞
知道大有可为答主
回答量:5761
采纳率:86%
帮助的人:1219万
展开全部
package com.nokia;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
//import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

//Graduate time from java school: 2010-8-19
public class CountDown extends JFrame{
private long longTime;
private long currentTime;
private long distTime;
private long day, hour, minutes, seconds;
private JTextField t_eventname;
private JTextField t_eventtime;
private JButton ok;
private JPanel mainframe;
private GridLayout grid;
private BorderLayout border;
private JLabel jl;
private ActionListener actionlistener;
private String eventname;
private Calendar cal;

public CountDown(){
this("CountDown Timer");
}
public CountDown(String frameName) {
super(frameName);
initial();
setSize(400, 200);
//this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
this.setVisible(true);
this.requestFocus();
//CDown();
}

private void initial() {
createComponent();
layOut();
listener();

}
private void createComponent() {
cal = Calendar.getInstance();
t_eventname = new JTextField();
t_eventtime = new JTextField();
mainframe = new JPanel();
border = new BorderLayout();
grid = new GridLayout(4, 1);
ok = new JButton("OK");
jl = new JLabel();

}
private void layOut() {
this.getContentPane().add(mainframe);
mainframe.setLayout(grid);
mainframe.add(t_eventname);
mainframe.add(t_eventtime);
mainframe.add(ok);
mainframe.add(jl);
}
private void listener() {
actionlistener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
eventname = t_eventname.getText();
String eventtime = t_eventtime.getText();
if (eventname.equals("")){
JOptionPane.showMessageDialog(mainframe, "please input the Event name");
return;
}
if (eventtime.equals("")){
JOptionPane.showMessageDialog(mainframe, "please input the Event Time");
return;
}
if (!eventtime.matches("\\d{4}/\\d{2}/\\d{2}/\\d{2}:\\d{2}:\\d{2}")){
JOptionPane.showMessageDialog(mainframe, "please input the right format Time like 1989/04/09/09:22:34");
/*here you should judge if the time is the right time, such as not 1989/23/34 ...
* after that, you should be sure here the time is in the future! good luck
* */
return;
}
String [] temp = eventtime.split("/|:");
//System.out.println(Arrays.toString(temp));
cal.set(Integer.valueOf(temp[0]), Integer.valueOf(temp[1]),
Integer.valueOf(temp[2]), Integer.valueOf(temp[3]),
Integer.valueOf(temp[4]), Integer.valueOf(temp[5]));
CDown();
}
};
ok.addActionListener(actionlistener);
}
public void CDown() {
Timer timer = new Timer();
//JFrame jf = new JFrame();

//jf.add(jl);
//jf.setVisible(true);
//jf.setSize(400, 150);
//jf.getDefaultCloseOperation();
timer.schedule(new TimerTask() {
@Override
public void run() {

// 返回历元到指定时间的<a href="https://www.baidu.com/s?wd=%E6%AF%AB%E7%A7%92&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhnhwWPAF9PAmLmynvrjwW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsnWb1P10YnH0krH6snWb4" target="_blank" class="baidu-highlight">毫秒</a>数。
longTime = cal.getTimeInMillis();
// 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的<a href="https://www.baidu.com/s?wd=%E6%AF%AB%E7%A7%92&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhnhwWPAF9PAmLmynvrjwW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3ErHcsnWb1P10YnH0krH6snWb4" target="_blank" class="baidu-highlight">毫秒</a>数。
currentTime = new Date().getTime();
distTime = longTime - currentTime;
day = ((distTime / 1000) / (3600 * 24));
hour = ((distTime / 1000) - day * 86400) / 3600;
minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;
seconds = (distTime / 1000) - day * 86400 - hour * 3600
- minutes * 60;
jl.setText(eventname + day + " 天 " + hour + "小时 :" + minutes
+ "分钟 :" + seconds + "秒");
}
}, 0, 1000);
}

public static void main(String[] args) {
new CountDown("CountDown Timeer");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式