重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java倒计时器代码插件 java 倒计时器

用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 javax.swing.*;

public class Exercise6_41 extends JApplet implements ActionListener

{

Thread timeSub;//倒计时器

JButton buttonStart, buttonRetry; // 开始 和 结束按钮

JLabel questionLabel, answerLabel, levelLabel, styleLabel;

JLabel TimeCount;// 问题,答案,等级和类型的标签

JTextField questionField, answerField, levelField, styleField;

public JTextField timeField;// 问题,回答,等级和类型 的输入框

JTextArea outputArea; // 程序记录域

JScrollPane scroller; // 载入记录

int MAX = 20; // 规定了一套试题的题量

int level = 1; // 规定了试题的等级,1到4 分别代表一位数,两位数,三位数,四位数的运算

int style = 3; // 规定了试题的类型,1到5分别代表加法,减法,乘法,求余数,和混合类型

int answer; // 存储用户输入的答案

int rightAnswer; // 存储标准答案

int rightCounter = 0; // 已经正确回答的问题 计数器

int totalCounter = 0; // 总的问题数 计数器

boolean presentQuestionAnswered = false; // 用来标记系统所给出的当前问题是否已被回答,false 代表未回答

public void init()

{

Container container = getContentPane();

container.setLayout(new FlowLayout());

levelLabel = new JLabel("请再此输入做几位数的运算(1-4):");

container.add(levelLabel);

levelField = new JTextField(5);

levelField.addActionListener(this);

container.add(levelField);

styleLabel = new JLabel("请再此输入试题的类型(1:加法 2:减法 3:乘法 4:MOD 5:混合):");

container.add(styleLabel);

styleField = new JTextField(5);

styleField.addActionListener(this);

container.add(styleField);

buttonStart = new JButton("开始");

buttonStart.addActionListener(this);

container.add(buttonStart);

questionLabel = new JLabel("请回答下面的问题:");

container.add(questionLabel);

questionField = new JTextField(20);

questionField.setEditable(false);

container.add(questionField);

answerLabel = new JLabel("在此输入您的答案:");

container.add(answerLabel);

answerField = new JTextField(20);

answerField.setText("");

answerField.addActionListener(this);

container.add(answerField);

outputArea = new JTextArea(20, 40);

outputArea.setEditable(false);

scroller = new JScrollPane(outputArea);

container.add(scroller);

buttonRetry = new JButton("再来一次");

buttonRetry.addActionListener(this);

container.add(buttonRetry);

JLabel TimeCount = new JLabel("计时器");

container.add(TimeCount);

timeField = new JTextField(20);

timeField.setEditable(false);

container.add(timeField);

}

public void actionPerformed(ActionEvent actionEvent)

{

if (actionEvent.getSource() == levelField)

{

level = Integer.parseInt(levelField.getText());

if (level 5 || level 1)

{

JOptionPane.showMessageDialog(null,

"您输入了未定义的等级!\n请重新输入!\n如果您坚持系统将会使用默认的等级!", "错误信息!",

JOptionPane.INFORMATION_MESSAGE);

level = 1;

}

else

{

levelField.setEditable(false);

}

}

else if (actionEvent.getSource() == styleField)

{

style = Integer.parseInt(styleField.getText());

if (style 5 || style 1)

{

JOptionPane.showMessageDialog(null,

"您输入了未定义的试题类型!\n请重新输入!如果您坚持系统将会使用默认的等级!", "错误信息!",

JOptionPane.INFORMATION_MESSAGE);

style = 1;

}

else

{

styleField.setEditable(false);

}

}

else if (actionEvent.getSource() == buttonStart)

{

int time=Integer.parseInt(JOptionPane.showInputDialog(null,"输入定时时间!","60").trim());//获得自定义时间

timeSub=new Exercise6_41TimeSub(time,timeField);//创建计时器

timeSub.start();//启动计时器

answerField.setEditable(true);

answerField.setText("");

totalCounter = 0;

rightCounter = 0;

{

createQuestion(style);

}

}

else if (actionEvent.getSource() == answerField)

{

answer = Integer.parseInt(answerField.getText());

judgeAnswer();

presentQuestionAnswered = true;

totalCounter++;

answerField.setText("");

if (totalCounter MAX)

{

createQuestion(style);

}

else

{

questionField.setText("你已经回答完了规定数量的试题!");

answerField.setText("");

questionField.setEditable(false);

answerField.setEditable(false);

caculateResult();

}

}

else if (actionEvent.getSource() == buttonRetry)

{

level = 1;

style = 1;

levelField.setText("");

levelField.setEditable(true);

styleField.setText("");

styleField.setEditable(true);

totalCounter = 0;

rightCounter = 0;// /再次初始化

}

}

public void createQuestion(int style)

{

if (style == 1)

{

long multi = 0;

if (level 0 level = 5)

{

multi = (long) (Math.pow(10, level));

}

int number1 = (int) (Math.random() * multi);

int number2 = (int) (Math.random() * multi);

rightAnswer = number1 + number2;

questionField.setText(number1 + " + " + number2 + " = ? ");

outputArea.append("\nQuestion " + (int) (totalCounter + 1) + " :"

+ number1 + " + " + number2 + " = ? ");

}

else if (style == 2)

{

long multi = 0;

if (level 0 level = 5)

{

multi = (long) (Math.pow(10, level));

}

int number1 = (int) (Math.random() * multi);

int number2 = (int) (Math.random() * multi);

rightAnswer = number1 - number2;

questionField.setText(number1 + " - " + number2 + " = ? ");

outputArea.append("\nQuestion " + (int) (1 + totalCounter) + " :"

+ number1 + "- " + number2 + " = ? ");

}

else if (style == 3)

{

long multi = 0;

if (level 0 level = 5)

{

multi = (long) (Math.pow(10, level));

}

int number1 = (int) (Math.random() * multi);

int number2 = (int) (Math.random() * multi);

rightAnswer = number1 * number2;

questionField.setText(number1 + " X " + number2 + " = ? ");

outputArea.append("\nQuestion " + (int) (totalCounter + 1) + " :"

+ number1 + " X " + number2 + " = ? ");

}

else if (style == 4)

{

long multi = 0;

if (level 0 level 5)

{

multi = (long) (Math.pow(10, level));

}

int number1 = (int) (Math.random() * multi);

int number2 = (int) (Math.random() * multi);

rightAnswer = number1 % number2;

questionField.setText(number1 + " % " + number2 + " = ? ");

outputArea.append("\nQuestion " + (int) (1 + totalCounter) + " :"

+ number1 + " % " + number2 + " = ? ");

}

else if (style == 5)

{

int temp;

temp = (int) (Math.random() * 100) % 4 + 1;

createQuestion(temp);

}

presentQuestionAnswered = false;

}

public void judgeAnswer()

{

if (answer == rightAnswer)

{

rightCounter++;

outputArea.append(" 回答正确! ");

}

else if (answer != rightAnswer)

{

outputArea.append(" 回答错误! ");

answerField.setText("");

answer = -1;

}

outputArea.append("还剩余 " + (MAX - totalCounter - 1) + " 个问题!\n");

}

public void caculateResult()

{

if (totalCounter == 0)

{

outputArea.append("\n您还未做任何题目!");

JOptionPane.showMessageDialog(null, "无法计算你的正确率!", "统计信息!",

JOptionPane.INFORMATION_MESSAGE);

}

else

{

double result = rightCounter / totalCounter * 100;

outputArea.append("\n正确的概率为:" + result + "%\n");

JOptionPane.showMessageDialog(null, "正确的概率为:" + result + "%",

"统计信息!", JOptionPane.INFORMATION_MESSAGE);

}

}

}

/**********************************************************************

计时器线程

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class Exercise6_41TimeSub extends Thread

{

private int time;

private JTextField showTime;

public Exercise6_41TimeSub(int time,JTextField showTime)

{

this.time=time;

this.showTime=showTime;

}

public void run()

{

while(true)

{

time--;

showTime.setText("剩余时间:"+time);

if(time==0)

{

JOptionPane.showMessageDialog(null,"时间到,请你检查你的答题情况!","时间到",JOptionPane.INFORMATION_MESSAGE);

}

try

{

Thread.sleep(1000);

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

}

java Timer倒数计时器(急)

哎这个太简单了。。。

Timer t = new Timer();

int s = 5;

TimerTask tt = new TimerTask()

{

public void run()

{

if(s 0)

s--;

}

};

t.scheduleAtFixedRate(tt,0,1000);

写一个计时器 JAVA代码是什么?

应该用线程里面的Timer来控制package com.sy.game.test;

import java.util.Timer;

import java.util.TimerTask;

public class TimeTask {

public static void main(String[] args) {

TimeTask tTask=new TimeTask();

tTask.timeVoid();

}

public void timeVoid(){

final Timer timer = new Timer();

TimerTask tt=new TimerTask() {

@Override

public void run() {

System.out.println("到点啦!");

timer.cancel();

}

};

timer.schedule(tt, 3000);

}

}

整合的:

/*

* java倒计时器

* shiyang

* */

package com.sy.game.test;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.Timer;

@SuppressWarnings("unused")

public class TimeController extends JFrame implements ActionListener {

private static final long serialVersionUID = 4603262282860990473L;

private static final int DEFAULT_WIDTH = 200;

private static final int DEFAULT_HEIGHT = 100;

private static final int width = Toolkit.getDefaultToolkit()

.getScreenSize().width;

private static final int height = Toolkit.getDefaultToolkit()

.getScreenSize().height;

private Container container;

private JButton btn;

private JTextField jtfTime;

private Timer tmr;

public TimeController() {

initComponents();

Timer tmr = new Timer(1000, this);

this.tmr = tmr;

setVisible(true);

}

private void initComponents() {

this.setTitle("SY秒表");

this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

this.setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocation((width - DEFAULT_WIDTH) / 2,

(height - DEFAULT_HEIGHT) / 2);

jtfTime = new JTextField("10");

btn = new JButton("开始倒计时");

container = getContentPane();

JPanel panel = new JPanel();

panel.add(btn);

panel.add(jtfTime);

this.add(panel);

btn.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

if (ae.getSource() == btn) {

jtfTime.setText("10");

tmr.start();

} else {

int t;

t = Integer.parseInt(jtfTime.getText());

t--;

jtfTime.setText("" + t);

if (t = 0) {

tmr.stop();

}

}

}

public static void main(String[] args) {

TimeController timeController = new TimeController();

}

}

求 JAVA 使用 Thread 和 Timer 类来做倒计时的程序代码

抱歉,之前没看到第二个条件,重新写了下。

在本机上可以正确运行。

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.io.IOException;

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;

// 记录所要启动的程序

private Process runningProcess;

// 构造方法

public TimeThreadFrame(){

// 设置窗体的相关属性

super("TimerThread");

this.setSize(300,200);

this.setLayout(null);

this.setLocation(100,50);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 创建组件

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

this.runningProcess = null;

// 给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() {

try {

// 当程序不存在时,会进行创建;存在时直接调用。

runningProcess = Runtime.getRuntime().exec("D:\\Program Files\\Tencent\\QQDoctor\\QQDoctor.exe");

} catch (IOException e) {

e.printStackTrace();

}

}

}, time * 1000);

}

}

// 启动窗体

public static void main(String[] args){

TimeThreadFrame ttf = 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倒计时器代码插件 java 倒计时器
URL网址:http://cqcxhl.cn/article/hpjsjc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP