利用Java製作倒計時

來源:互聯網
上載者:User

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 stublabel = 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 stubthis.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 stubthis.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 stubonetime = 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;elsethis.Sminitues = "" + minitues;if (sound < 10)this.Ssound = "0" + sound;elsethis.Ssound = "" + sound;return this.Sminitues + ":" + this.Ssound;}public void display() {/* * 顯示倒計時 */l.setText(this.LabelTime);}}}}


______________________________________________

另外一個

60秒倒計時的一個小JAVASCRIPT

<table name=agree border="0" cellpadding="10" cellspacing="0" align=center width="80%">
<tr align=center>
<td width="50%" align="right"> 
<FORM name=register method=post action=reg_shang.ASP?action=yes>
<input type="hidden" name="action" value="agree"><INPUT class="go-wenbenkuang" type=submit value=" 同意註冊 " name=Submit>
</FORM> 
</td>
<td width="50%" align="left">
<FORM action=index.asp method=post><INPUT class=go-wenbenkuang type=submit value=" 我不同意 "></FORM></td>
</tr>
</table>

<SCRIPT>
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;
}
</SCRIPT>

---另外一種直接可以解析的方法

<script>

<!--

//獲得目前時間,刻度為一千分一秒

var initializationTime=(new Date()).getTime();

function showLeftTime(){

var now=new Date();

var year=now.getYear();

var month=now.getMonth();

var day=now.getDate();

var hours=now.getHours();

var minutes=now.getMinutes();

var seconds=now.getSeconds();

document.all.show.innerHTML="目前時間:"+year+"年"+month+"月"+day+"日"+hours+"小時"+minutes+"分"+seconds+"秒";

//設定結束時間

//1秒=1000毫秒

endTime=initializationTime+60000;

//設定並顯示剩餘時間

var leftTime=Math.floor((endTime-(new Date()).getTime())/1000);

if(leftTime>0)

{

document.all.showLeft.innerHTML=leftTime+"微秒後停止!";

}

else

{

clearTimeout(timeID);

document.all.showLeft.innerHTML="TimeOut!";

return false;

}

//一秒重新整理一次顯示時間

var timeID=setTimeout(showLeftTime,1000);

}

//-->

</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.