大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.event.ActionEvent;
成都创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供网站制作、成都做网站、成都网页设计、微信平台小程序开发、成都网站开发、成都网站制作、成都软件开发、重庆APP开发是成都本地专业的网站建设和网站设计公司,等你一起来见证!
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();
}
}
}
}
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();
}
}
}
}
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(); } } } }}
基于控制台的话很简单的,我跟你说一下大体思路吧,二话不说先来个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("爆炸");
}
其他基于网页的还是基于用户界面都可以使用这个思路的
//搞定,代码自己理解哈.
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);
}
}
}
}