网站建设资讯

NEWS

网站建设资讯

倒数计时java代码,java倒计时三种简单实现方式

怎么编写一个倒计时的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.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 倒计时

//搞定,代码自己理解哈.

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 stub

label=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 stub

this.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 stub

this.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 stub

onetime = 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;

else

this.Sminitues = "" + minitues;

if (sound 10)

this.Ssound = "0" + sound;

else

this.Ssound = "" + sound;

return this.Sminitues + ":" + this.Ssound;

}

public void display() {

/*

* 显示倒计时

*/

l.setText(this.LabelTime);

}

}

}

}


文章名称:倒数计时java代码,java倒计时三种简单实现方式
网页地址:http://cdweb.net/article/dsieipe.html