发布网友 发布时间:2022-04-23 22:45
共2个回答
热心网友 时间:2023-08-25 15:50
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个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("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
热心网友 时间:2023-08-25 15:51
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;
//Graate 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 19/04/09/09:22:34");
/*here you should judge if the time is the right time, such as not 19/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.schele(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="-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="-highlight">毫秒</a>数。
currentTime = new Date().getTime();
distTime = longTime - currentTime;
day = ((distTime / 1000) / (3600 * 24));
hour = ((distTime / 1000) - day * 800) / 3600;
minutes = ((distTime / 1000) - day * 800 - hour * 3600) / 60;
seconds = (distTime / 1000) - day * 800 - hour * 3600
- minutes * 60;
jl.setText(eventname + day + " 天 " + hour + "小时 :" + minutes
+ "分钟 :" + seconds + "秒");
}
}, 0, 1000);
}
public static void main(String[] args) {
new CountDown("CountDown Timeer");
}
}