怎么编写一个倒计时的java的程序?求具体步骤!

怎么编写一个倒计时的java的程序?求具体步骤!,第1张

基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个for循环,然后输出倒计时的数字,程序睡一秒,在输出倒计时数字,如此循环,简单吧,下面看程序:

public static void main(String[] args) {

for(int i=10i>0i--){

System.out.print(i+" ")

try {

Thread.sleep(1000)

} catch (InterruptedException e) {

e.printStackTrace()

}

}

System.err.print("爆炸")

}

其他基于网页的还是基于用户界面都可以使用这个思路的

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()

}

}

}

}

public class Timer implements Runnable {

int i=10

public static void main(String[] args) {

Timer t=new Timer()

Thread th=new Thread(t)

th.start()

}

@Override

public void run() {

try {

while(true){

i--

Thread.sleep(1000)

System.out.println(i)

if(i==7){

System.out.println("还剩7秒爆炸")

}

if(i==1){

System.out.println("差点就挂了")

return

}

}

} catch (Exception e) {

e.printStackTrace()

}

}

}

应该是这样吧


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11320393.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存