本文目录一览:
- 1、java 设计一个简单的倒计时
- 2、怎么编写一个倒计时的java的程序?求具体步骤!
- 3、用java遍写元旦倒计时
- 4、javaWeb中如何做倒计时
- 5、请问用java怎样用窗口做一个90秒倒计时?
- 6、用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的程序?求具体步骤!
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个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遍写元旦倒计时
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();
}
}
}
}
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怎样用窗口做一个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();
}
}
用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");
}
}
}