⽤C语⾔编写10s倒计时程序,怎样⽤C编写⼀个倒计时程序怎么编写⼀个倒计时java程序?求具体步骤!
kia;
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);
//CDown();
}
private void initial() {
createComponent();
layOut();
listener();
}
private void createComponent() {
cal = Instance();
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() {
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 = Text();
String eventtime = Text();
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.String(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();
}
};
java怎么编写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() {
/
/ 返回历元到指定时间的数。
longTime = TimeInMillis();
// 返回⾃ 1970 年 1 ⽉ 1 ⽇ 00:00:00 GMT 以来此 Date 对象表⽰的数。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");
}
}
收起
阅读全⽂

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。