十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个for循环,然后输出倒计时的数字,程序睡一秒,在输出倒计时数字,如此循环,简单吧,下面看程序:
成都创新互联公司专业为企业提供江山网站建设、江山做网站、江山网站设计、江山网站制作等企业网站建设、网页设计与制作、江山企业网站模板建站服务,10年江山做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
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("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
//搞定,代码自己理解哈.
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);
}
}
}
}
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(); } } } }}
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();
}
}
}
}
哎这个太简单了。。。
Timer t = new Timer();
int s = 5;
TimerTask tt = new TimerTask()
{
public void run()
{
if(s 0)
s--;
}
};
t.scheduleAtFixedRate(tt,0,1000);
import java.awt.Color;
import java.util.*;
import java.awt.*;
import java.applet.*;
public class Clock extends Applet implements Runnable
{
Thread timer=null;
Label label;
int lastxs=50,lastys=30,lastxm=50,lastym=30,lastxh=50,lastyh=30;
public void init()
{
label=new Label(" ");
setBackground(Color.white);
add(label);
}
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Date rightnow=new Date();
String today=rightnow.toLocaleString();
label.setText(today);
s=rightnow.getSeconds();
m=rightnow.getMinutes();
h=rightnow.getHours();
xcenter=100;
ycenter=80;
xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter);
ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter);
xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*45+xcenter);
ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*45+ycenter);
xh=(int)(Math.cos((h*30+m*2)*3.14f/180-3.14f/2)*30+xcenter);
yh=(int)(Math.sin((h*30+m*2)*3.14f/180-3.14f/2)*30+ycenter);
g.setFont(new Font("TimesToman",Font.PLAIN,14));
g.setColor(Color.orange);
g.fill3DRect(xcenter-50,ycenter-50,100,100,true);
g.setColor(Color.darkGray);
g.drawString("12",xcenter-5,ycenter-37);
g.drawString("3",xcenter+40,ycenter+3);
g.drawString("6",xcenter-3,ycenter+45);
g.drawString("9",xcenter-45,ycenter+3);
g.setColor(Color.orange);
if(xs!=lastxs||ys!=lastys)
{
g.drawLine(xcenter,ycenter,lastxs,lastys);
}
if(xm!=lastxm||ym!=lastym)
{
g.drawLine(xcenter,ycenter-1,lastxm,lastym);
g.drawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
g.drawLine(xcenter-1,ycenter,lastxh,lastyh);
}
g.setColor(Color.red);
g.drawLine(xcenter,ycenter,xs,ys);
g.drawLine(xcenter,ycenter-1,xm,ym);
g.drawLine(xcenter-1,ycenter,xm,ym);
g.drawLine(xcenter,ycenter-1,xh,yh);
g.drawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;
lastys=ys;
lastxm=xm;
lastym=ym;
lastxh=xh;
lastyh=yh;
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void stop()
{
timer=null;
}
public void run()
{
while(timer!=null)
{
try
{
Thread.sleep(1000);
}catch(InterruptedException ie){}
repaint();
}
timer=null;
}
public void update(Graphics g)
{
paint(g);
}
}