主题:刚学参加了个免费JAVA培训5天学会了显示奥运时间
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class CountTime extends Frame {
/**
* @param args
这是在松迪科技第五天学习记录.努力就是胜利.
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//创建一个窗口
CountTime ct = new CountTime();
//设置窗口下面的大小
ct.setSize(600, 150);
//加一个关闭事件的监听器,在单击关闭时使系统退出
ct.addWindowListener(new MyWinListener());
//设置窗口位置
ct.setLocation(350, 250);
//设置窗口的背景色
ct.setBackground(Color.BLACK);
//为窗口添加一个Label,因为无法直接在窗口上写文字
Label l = new Label();
l.setForeground(Color.RED);//设置字体颜色
l.setLocation(0, 75); //设置字体垂直位置
l.setAlignment(Label.CENTER);//设置水平居中
Font f = new Font("d", Font.BOLD, 30);//字体风格大小
l.setFont(f);
ct.add(l);
ct.setVisible(true);
Timer t = new Timer();//生成一个定时器
t.schedule(new MyTimerTask(l), 0, 1000);//延迟0毫秒开始执行任务,之后每隔1秒执行一次
}
}
class MyTimerTask extends TimerTask {
private Label l;
public MyTimerTask(Label l) {
this.l = l;
}
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dd = sdf.parse("2008-08-08 20:08:08");
long day = (dd.getTime()-System.currentTimeMillis())/(24*3600*1000l);
long hour = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l)/(3600*1000l);
long minutes = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l)/(60*1000l);
long seconds = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l-minutes*60*1000l)/1000l;
String text = "距2008年奥运会还有"+day+"天"+hour+"时"+minutes+"分"+seconds+"秒";
l.setText(text);
} catch(Exception e) {
}
}
}
-------------------
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyWinListener implements WindowListener {
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
初学者希望提出宝贵建议.
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class CountTime extends Frame {
/**
* @param args
这是在松迪科技第五天学习记录.努力就是胜利.
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//创建一个窗口
CountTime ct = new CountTime();
//设置窗口下面的大小
ct.setSize(600, 150);
//加一个关闭事件的监听器,在单击关闭时使系统退出
ct.addWindowListener(new MyWinListener());
//设置窗口位置
ct.setLocation(350, 250);
//设置窗口的背景色
ct.setBackground(Color.BLACK);
//为窗口添加一个Label,因为无法直接在窗口上写文字
Label l = new Label();
l.setForeground(Color.RED);//设置字体颜色
l.setLocation(0, 75); //设置字体垂直位置
l.setAlignment(Label.CENTER);//设置水平居中
Font f = new Font("d", Font.BOLD, 30);//字体风格大小
l.setFont(f);
ct.add(l);
ct.setVisible(true);
Timer t = new Timer();//生成一个定时器
t.schedule(new MyTimerTask(l), 0, 1000);//延迟0毫秒开始执行任务,之后每隔1秒执行一次
}
}
class MyTimerTask extends TimerTask {
private Label l;
public MyTimerTask(Label l) {
this.l = l;
}
@Override
public void run() {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dd = sdf.parse("2008-08-08 20:08:08");
long day = (dd.getTime()-System.currentTimeMillis())/(24*3600*1000l);
long hour = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l)/(3600*1000l);
long minutes = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l)/(60*1000l);
long seconds = (dd.getTime()-System.currentTimeMillis()-day*24*3600*1000l-hour*3600*1000l-minutes*60*1000l)/1000l;
String text = "距2008年奥运会还有"+day+"天"+hour+"时"+minutes+"分"+seconds+"秒";
l.setText(text);
} catch(Exception e) {
}
}
}
-------------------
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyWinListener implements WindowListener {
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
初学者希望提出宝贵建议.