import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Clock extends JFrame {
private Dialog dialog;
public static void main(String[] args) {
Clock f = new Clock();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
class MyDialog extends Dialog implements WindowListener, ActionListener {
JLabel label;
JPanel panel1, panel2;
JButton button;
public MyDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
// TODO Auto-generated constructor stub
label = new JLabel("时间到!");
button = new JButton("确定");
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add("Center", label);
panel2.add("Center", button);
this.add("Center", panel1);
this.add("South", panel2);
this.setSize(200, 200);
this.setResizable(false);
this.addWindowListener(this);
button.addActionListener(this);
}
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
this.setVisible(false);
}
}
Clock() {
setTitle("倒计时");
setSize(320, 120);
dialog = new MyDialog(this, "提示:", true);
ClockPanel p = new ClockPanel();
add(p);
}
class ClockPanel extends JPanel {
private JButton b;
private boolean onetime = true;;
private JLabel lfen, lmiao, l;
private JTextField tf, tm;
ClockPanel() {
b = new JButton("开始倒计时");
lfen = new JLabel("分");
lmiao = new JLabel("秒");
l = new JLabel("00:00");
tf = new JTextField(3);
tm = new JTextField(3);
l.setFont(new Font("宋体", Font.BOLD, 30));
l.setForeground(Color.RED);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (onetime) {
if (tf.getText().trim().equals("")) {
tf.setText("00");
}
if (tm.getText().trim().equals("")) {
tm.setText("00");
}
new ChangeLabel(tf.getText().trim() + ":"
+ tm.getText().trim()).start();
}
}
});
add(tf);
add(lfen);
add(tm);
add(lmiao);//添加秒
add(b);
add(l);
}
class ChangeLabel extends Thread // 运行秒针线程
{
private int minitues;
private String Sminitues;
private int sound;
private String Ssound;
private String LabelTime;
public ChangeLabel(String time) {
// TODO Auto-generated constructor stub
onetime = false;
this.minitues = Integer.parseInt(time.substring(0,
time.indexOf(':')));
this.sound = Integer
.parseInt(time.substring(time.indexOf(':') + 1));
}
private long time1;
private long time2;
public void run() {
time1 = System.currentTimeMillis();
while (true) {
time2 = System.currentTimeMillis();
while (!(minitues == 0 & sound == 0)
&& time2 >= time1 + 1000) {
time1 = time2;
if (sound == 0) {
sound = 59;
minitues--;
} else {
sound--;
}
LabelTime = this.getTime();
display();
}
if (minitues == 0 && sound == 0) {
dialog.setVisible(true);
onetime = true;
break;
}
}
}
private String getTime() {
if (minitues < 10)
this.Sminitues = "0" + minitues;
else
this.Sminitues = "" + minitues;
if (sound < 10)
this.Ssound = "0" + sound;
else
this.Ssound = "" + sound;
return this.Sminitues + ":" + this.Ssound;
}
public void display() {
/*
* 显示倒计时
*/
l.setText(this.LabelTime);
}
}
}
}
______________________________________________
另外一个
60秒倒计时的一个小JAVASCRIPT
secs = 60; // Number of secs to delay -CHINA-studio
wait = secs * 100;
document.forms.register.Submit.disabled =true;
for(i=1;i<=(wait/100);i++) {
window.setTimeout("doUpdate(" + i + ")", i * 100);
}
window.setTimeout("Timer()", wait);
function doUpdate(num) {
if(num == (wait/100)) {
document.forms.register.Submit.value = " 同意注册 ";
} else {
wut = (wait/100)-num;
document.forms.register.Submit.value = " 同意前请先阅读 (" + wut + ")";
}
}
function Timer() {
document.forms.register.Submit.disabled =false;
}
---另外一种直接可以解析的方法