关于java系统中活动倒计时的信息

博主:adminadmin 2022-12-01 02:03:09 51

今天给各位分享java系统中活动倒计时的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何在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 中 倒计时 一分钟 怎么写?

代码如下:

public class TimeCountDown {

public static void countDown(int seconds) {

System.err.println("倒计时" + seconds + "秒,倒计时开始:");

int i = seconds;

while (i 0) {

System.err.println(i);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

i--;

}

System.err.println(i);

System.err.println("倒计时结束");

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

countDown(60);// 倒计时1分钟

}

}

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

}

}

}

}

用java编写一个倒计时器代码。

import java.awt.BorderLayout;import java.awt.Container;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class TimerDemo extends JFrame implements ActionListener { private static final long serialVersionUID = 201306211111L; private JTextField screen = new JTextField("0"); private JButton start = new JButton("开始"); private JButton reset = new JButton("重置"); private JPanel panel = new JPanel(); private boolean isRunning; private int time; private int timeBetween; public TimerDemo(int timeBetween) { super("计时器"); this.timeBetween = timeBetween; try { init(); } catch (Exception e) { e.printStackTrace(); } } public TimerDemo() { super("计时器"); this.timeBetween = 100; try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { panel.setLayout(new GridLayout()); panel.add(start); panel.add(reset); start.addActionListener(this); reset.addActionListener(this); screen.setFont(new Font("幼圆", Font.BOLD, 60)); screen.setHorizontalAlignment(JTextField.CENTER); screen.setEditable(false); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(panel, BorderLayout.SOUTH); c.add(screen, BorderLayout.CENTER); this.setSize(200, 150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new TimerDemo(1);// 设定 1ms/次 // new TimerDemo(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { if (start.getText().equals("开始")) { start.setText("暂停"); isRunning = true; } else if (start.getText().equals("暂停")) { start.setText("开始"); isRunning = false; } } if (e.getSource() == reset) { start.setText("开始"); screen.setText("0"); isRunning = false; time = 0; } new Thread(new TimeZone()).start(); } class TimeZone implements Runnable { @Override public void run() { while (isRunning) { time++; if (time = Integer.MAX_VALUE) { screen.setText("ERROR"); JOptionPane.showMessageDialog(null, "ERROR"); isRunning = false; } screen.setText(String.valueOf(time)); try { Thread.sleep(timeBetween); } catch (Exception e) { e.printStackTrace(); } } } }}

java线程怎么做个时间倒计时

不知道你是要计时还是要线程挂起。

计时 你可以使用timer,自己去看一下timer的API, void schedule(TimerTask task, Date firstTime, long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行,应该是用这个Timer的成员函数。

如果你只是想要线程挂起延时,直接sleep 就OK了。

怎么编写一个倒计时的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系统中活动倒计时和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。