本文目錄一覽:
- 1、請問用java怎樣用窗口做一個90秒倒計時?
- 2、javaWeb中如何做倒計時
- 3、怎麼編寫一個倒計時的java的程序?求具體步驟!
- 4、用java編一個簡單的倒計時錶
- 5、java 設計一個簡單的倒計時
- 6、用java遍寫元旦倒計時
請問用java怎樣用窗口做一個90秒倒計時?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test extends JFrame
{
private JButton btn = new JButton(“Start”);
private JLabel label = new JLabel();
private int time = 90;
Test() {
setSize(500,300);
setLayout(new FlowLayout());
add(btn);
add(label);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new MyThread()).start();
}
});
setVisible(true);
}
class MyThread implements Runnable
{
public void run() {
while (time 0)
{
time–;
label.setText(time + “”);
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Test.this.dispose();
}
};
public static void main(String[] args)
{
new Test();
}
}
javaWeb中如何做倒計時
input type=”button” value=”倒計時” id=”button1″
onClick=”timedMsg()”
script type=”text/javascript”
var c=5;
var t;
function timedMsg()
{
document.getElementById(‘button1’).value=”倒計時”+c;
document.getElementById(‘button1’).disabled=true;
if(c==0){
clearTimeout(t);
window.location.href=”url”;//為跳轉地址
}else{
t=setTimeout(‘timedMsg()’,1000);
}
c–;
}
/script
點擊按鈕開始倒計時,當計時為0的時候跳轉
setTimeout設置多少時間調用函數,返回值用於清除定時器
怎麼編寫一個倒計時的java的程序?求具體步驟!
基於控制台的話很簡單的,我跟你說一下大體思路吧,二話不說先來個for循環,然後輸出倒計時的數字,程序睡一秒,在輸出倒計時數字,如此循環,簡單吧,下面看程序:
public static void main(String[] args) {
for(int i=10;i0;i–){
System.out.print(i+” “);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.err.print(“爆炸”);
}
其他基於網頁的還是基於用戶界面都可以使用這個思路的
用java編一個簡單的倒計時錶
先不要關閉問題,給我點時間我編出來,我也想斷煉一下。
代碼如下:
我這程序有點問題 ,這倒計時你講的功能都有了,但那個暫停按鈕有問題,只能用兩次,我怎麼也找不出原因, 我想是多線程方面的問題吧,按兩下那暫停按鈕就失去作用了。
————————–MainFrame.java————————————
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends Frame{
Dispose dp= null;
int flag=1; //暫停開始的按鈕,1為計時中,0為暫停。
Button button=null;
Label l1 =null;
Label l2 =null;
Label l3 = null;
TextField tfh =null;
TextField tfm =null;
TextField tfs =null;
public void lanchFrame(){
this.setLocation(200,200);
this.setSize(200,200);
this.setLayout(new FlowLayout());
l1=new Label(“hour”);
tfh = new TextField(“1”,6);
l2=new Label(“minute”);
tfm = new TextField(“3”,6);
l3=new Label(“second”);
tfs = new TextField(“5”,6);
button = new Button(“stop”);
this.add(l1);
this.add(tfh);
this.add(l2);
this.add(tfm);
this.add(l3);
this.add(tfs);
this.add(button);
button.addActionListener(new StartAndStopListener(this));
this.addWindowListener(new MyClosingListener());
this.dp = new Dispose(this);
this.pack();
this.setVisible(true);
}
public static void main(String args[]){
MainFrame mf=new MainFrame();
mf.lanchFrame();
mf.dp.run(mf);
}
private class MyClosingListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
————————–Dispose.java————————————
public class Dispose {
int hour;
int minute;
int second;
public Dispose(MainFrame mf) {
this.hour = Integer.parseInt(mf.tfh.getText());
this.minute = Integer.parseInt(mf.tfm.getText());
this.second = Integer.parseInt(mf.tfs.getText());
}
public void run(MainFrame mf) {
while (!(hour == 0 minute == 0 second == 0) mf.flag == 1) {
if (second == 0) {
if (minute 0) {
second = 59;
minute–;
}
}
if (minute == 0) {
if (hour 0) {
minute = 59;
hour–;
}
}
second–;
mf.tfs.setText(second + “”);
mf.tfm.setText(minute + “”);
mf.tfh.setText(hour + “”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
——————————StarAndStopListener.java—————————–
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StartAndStopListener implements ActionListener {
MainFrame mf = null;
public StartAndStopListener(MainFrame mf){
this.mf = mf;
}
public void actionPerformed(ActionEvent arg0) {
if(mf.flag==0){
mf.flag =1;
mf.button.setLabel(“stop”);
mf.dp.run(mf);
}
if(mf.flag == 1){
mf.flag = 0;
mf.button.setLabel(“start”);
}
}
}
java 設計一個簡單的倒計時
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TimeThreadFrame extends JFrame{
// 定義組件
private JLabel lblTime;
private JTextField txtInput;
private JButton btnEnter;
// 構造方法
public TimeThreadFrame(){
// 設置窗體的相關屬性
super(“TimerThread”);
this.setSize(300,200);
this.setLayout(null);
this.setLocation(100,50);
// 創建組件
this.lblTime = new JLabel(“請輸入倒計時時間”);
this.lblTime.setBounds(30,20,200,30);
this.txtInput = new JTextField();
this.txtInput.setBounds(30,70,100,30);
this.btnEnter = new JButton(“確定”);
this.btnEnter.setBounds(150,70,70,30);
// 給JTextField註冊監聽
this.txtInput.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent ke) {
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
txtInput_KeyTyped(ke);
}
});
// 給JButton註冊監聽
this.btnEnter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnEnter_ActionPerformed(ae);
}
});
// 將各組件添加到窗體上
add(lblTime);
add(txtInput);
add(btnEnter);
// 顯示窗體
this.setVisible(true);
}
// 輸入時的事件處理,控制用戶只能輸入數字
public void txtInput_KeyTyped(KeyEvent ke){
if(ke.getKeyChar() ‘0’ || ke.getKeyChar() ‘9’){
ke.setKeyChar(‘\0’);
}
}
// 點擊按鈕時的事件處理,核心!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 獲得用戶輸入的倒計時時間
String strTime = this.txtInput.getText();
if(strTime.equals(“”)){
// 檢測用戶是否輸入
this.lblTime.setText(“您尚未輸入,請輸入!”);
}
else{
Integer time = Integer.parseInt(strTime);
// 創建線程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 創建Timer
Timer timer = new Timer();
timer.schedule(new TimerTask(){
// 啟動其他程序
public void run() {
System.out.print(“ok”);
}
}, time * 1000);
}
}
// 啟動窗體
public static void main(String[] args){
new TimeThreadFrame();
}
}
// 時間線程類
class TimeThread extends Thread{
private JLabel lblTime;
private int time;
// 構造方法傳入,顯示事件的JLabel和倒計時的時間。
public TimeThread(JLabel lblTime, int time){
this.lblTime = lblTime;
this.time = time;
}
// run方法
public void run(){
while(time 0){
// 顯示所剩時間
this.lblTime.setText(“所剩時間:” + time);
// 所剩時間減少
time–;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
用java遍寫元旦倒計時
import java.util.Calendar;
import java.util.Date;
public class Countdown2 implements Runnable {
public static void main(String[] args) {
Thread cd = new Thread(new Countdown2());
cd.start();
}
@Override
public void run() {
// 設置日期2012-12-21
Calendar c = Calendar.getInstance();
c.set(2016, 1, 1, 0, 0, 0);
// 單獨設置年、月、日、小時、分鐘、秒
c.set(Calendar.YEAR, 2015);
c.set(Calendar.MONTH, Calendar.DECEMBER); // 0 表示1月,11 表示12月
c.set(Calendar.DAY_OF_MONTH, 21);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// 獲取2012-12-21 0:0:0時間點對應的毫秒數
long endTime = c.getTimeInMillis();
// 獲取系統當前時間
Date now = new Date();
// 獲取當前時間點對應的毫秒數
long currentTime = now.getTime();
// 計算兩個時間點相差的秒數
long seconds = (endTime – currentTime) / 1000;
while (true) {
long days = seconds / (3600 * 24);
long h = seconds % (3600 * 24) / 3600;
long m = seconds % (3600 * 24) % 3600 / 60;
long s = seconds % (3600 * 24) % 3600 % 60;
System.out.println(“離2016年元旦還剩: ” + days + “天” + h + “小時” + m + “分” + s + “秒”);
seconds–;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/236849.html