怎么编写一个倒计时的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的application编写的倒计时钟代码?package hello;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
public class T extends Applet implements Runnable{
Thread tHour = null,tMinute = null,tSecond = null;//表示时针,分针和秒针的线程
int hour_a,hour_b,minute_a,minute_b,second_a,second_b;//表示时针,分针,秒针端点的整型变量
int hour = 0,minute = 0,second = 0;//获取当前时间的整型变量
//绘制时针,分针和秒针的Graphics对象
Graphics g_second = null,g_minute = null,g_hour =null;
//存放表盘刻度的数组,供指针走动时使用
doublepoint_x[] = new double[61],point_y[] = new double[61];
//存放表盘刻度的数组,供绘制表盘使用
double scaled_x[] = new double[61],scaled_y[] = new double[61];
//判断小程序是否重新开始的变量
int start_count = 0;
public void init(){
g_hour = this.getGraphics();
g_hour.setColor(Color.CYAN);
g_second = this.getGraphics();
g_second.setColor(Color.RED);
g_minute = this.getGraphics();
g_minute.setColor(Color.blue);
g_second.translate(200,200);//进行坐标系统变换,将新坐标系原点设在(200 , 200)处
g_minute.translate(200,200);
g_hour.translate(200,200);
point_x[0] = 0; point_y[0] = -120;//各个时针12点处的位置坐标(按新坐标系的坐标)
scaled_x[0] = 0;scaled_y[0] = -140;//12点处的刻度位置坐标(按新坐标系的坐标)
double jiaodu = 6*Math.PI/180;
//表盘分割成60分,将分割点的坐标存放在数组中
for(int i = 0; i60; i){
point_x[i 1] = point_x[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*point_y[i];
point_y[i 1] = point_y[i]*Math.cos(jiaodu)point_x[i]*Math.sin(jiaodu);
}
point_x[60] = 0; point_y[60] = -120;
for(int i = 0; i60; i){
scaled_x[i 1] = scaled_x[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*scaled_y[i];
scaled_y[i 1] = scaled_y[i]*Math.cos(jiaodu)Math.sin(jiaodu)*scaled_x[i];
}
scaled_x[60]= 0;
scaled_y[60] = -140;
}
public void start(){
//每当小程序重新开始时,首先消灭线程,然后重新开始创建线程
if(start_count = 1){
tSecond.interrupt();
tMinute.interrupt();
tHour.interrupt();
}
tSecond = new Thread(this);
tMinute = new Thread(this);
tHour= new Thread(this);
tSecond.start();
tMinute.start();
tHour.start();
start_count;
if(start_count = 2) start_count = 1;
}
public void stop()
{
tSecond.interrupt();
tMinute.interrupt();
tHour.interrupt();
}
public void paint(Graphics g){
this.start();
g.drawOval(50,50,300,300);//表盘的外圈
g.translate(200,200);
//绘制表盘的小刻度和大刻度
for(int i = 0 ; i60; i){
if(i%5 == 0){
g.setColor(Color.BLACK);
g.fillOval((int) scaled_x[i],(int) scaled_y[i],10,10);
}
else
g.fillOval((int)scaled_x[i],(int)scaled_y[i],5,5);
}
}
public void run(){
//获取本地时间
Date date = new Date();
String s=date.toString();
hour=Integer.parseInt(s.substring(11,13));
minute = Integer.parseInt(s.substring(14,16));
second = Integer.parseInt(s.substring(17,19));
if(Thread.currentThread() == tSecond){
second_a =(int)point_x[second];
second_b = (int)point_x[second];
g_second.drawLine(0,0,second_a,second_b);//秒针的初始位置
g_second.drawString("秒",second_a,second_b);
int i = second;
while(true){
try{
tSecond.sleep(1000);
Color c = getBackground();
g_second.setColor(c);
g_second.drawLine(0,0,second_a,second_b);//用背景色清除前一秒时的秒针
g_second.drawString("秒",second_a,second_b);
//如果秒针与分针重合,恢复分针的显示
if((second_a == minute_a)(second_b == minute_b)){
g_minute.drawLine(0,0,minute_a,minute_b);
g_minute.drawString("分",minute_a,minute_b);
}
//如果秒针与时针重合,恢复时针的显示
if((second_a == hour_a)(second_b == hour_b)){
g_hour.drawLine(0,0,hour_a,hour_b);
g_hour.drawString("时",hour_a,hour_b);
}
}
catch(InterruptedException e){
Color c = getBackground();
g_second.setColor(c);
g_second.drawLine(0,0,second_a,second_b);//用背景色清除秒针
g_second.drawString("秒",second_a,second_b);
return;
}
//秒针向前走一个单位
second_a=(int)point_x[(i 1)`];
second_b = (int)point_y[(i 1)`]; //每一秒走6度(一个单位格)
g_second.setColor(Color.red);
g_second.drawLine(0,0,second_a,second_b);
g_second.drawString("秒",second_a,second_b);
i;
}
}
if(Thread.currentThread() == tMinute){
minute_a = (int)point_x[minute];
minute_b = (int)point_y[minute];
g_minute.drawLine(0,0,minute_a,minute_b);
int i = minute;
while(true){
//第一次过60-second秒就前进一分钟,以后每过60秒前进一分钟
try{
tMinute.sleep(1000*60 - second*1000);
second = 0;
Color c = getBackground();
g_minute.setColor(c);
g_minute.drawLine(0,0,minute_a,minute_b);
g_minute.drawString("分",minute_a,minute_b);
if((hour_a == minute_a)(hour_b== minute_b)){
g_hour.drawLine(0,0,minute_a,minute_b);
g_hour.drawString("时",hour_a,hour_b);
}
}
catch(InterruptedException e){
return;
}
minute_a = (int)point_x[(i 1)`];
minute_b = (int)point_y[(i 1)`];
g_minute.setColor(Color.BLUE);
g_minute.drawLine(0,0,minute_a,minute_b);
g_minute.drawString("分",minute_a,minute_b);
i; second = 0;
}
}
if(Thread.currentThread() == tHour){
int h = hour;
hour_a = (int)point_x[h*5minute/12];
hour_b = (int)point_y[h*5minute/12];
int i = h*5minute/12;
g_hour.drawLine(0,0,hour_a,hour_b);
g_hour.drawString("时",hour_a,hour_b);
while(true){
//第一次过12-minute分钟就前进一个刻度,以后每过12分钟前进一个刻度
try{
tHour.sleep(1000*60*12 - 1000*60*(minute) - second *1000);
minute = 0;
Color c = getBackground();
g_hour.setColor(c);
g_hour.drawLine(0,0,hour_a,hour_b);
g_hour.drawString("时",hour_a,hour_b);
}
catch(InterruptedException e){
return;
}
hour_a = (int)point_x[(i 1)`];
hour_b = (int)point_y[(i 1)`];
g_hour.setColor(Color.CYAN);
g_hour.drawLine(0,0,hour_a,hour_b);
g_hour.drawString("时",hour_a,hour_b);
i;minute = 0;
}
}
}
}
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(){
// 设置窗体java考试倒计时代码的相关属性
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');
}
}
// 点击按钮时的事件处理,核心java考试倒计时代码!
public void btnEnter_ActionPerformed(ActionEvent ae){
// 获得用户输入的倒计时时间
String strTime = this.txtInput.getText();
if(strTime.equals("")){
// 检测用户是否输入
this.lblTime.setText("您尚未输入,请输入java考试倒计时代码!");
}
else{
Integer time = Integer.parseInt(strTime);
// 创建线程
TimeThread tt = new TimeThread(this.lblTime,time);
tt.start();
// 创建Timer
Timer timer = new Timer();
timer.schedule(new TimerTask(){
// 启动其java考试倒计时代码他程序
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(time0){
// 显示所剩时间
this.lblTime.setText("所剩时间:"time);
// 所剩时间减少
time--;
try {
this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
满意请采纳 。
MyEclipse如何编写一个考试系统倒计时,进入考试页面就开始计时?要在MyEclipse中为考试系统创建倒计时计时器,可以使用JavaScript将计时器添加到考试页面 。以下是如何做到这一点的示例:
在MyEclipse中打开检查页面的HTML文件 。
将<script>元素添加到HTML文件的<head>部分 。
在<script>元素中,添加以下代码以定义将显示倒计时计时器的JavaScript函数:
此函数将使用倒计时的总持续时间(以秒为单位)和一个HTML元素来显示计时器 。然后,它将每秒更新显示元素的文本内容 , 以显示剩余时间 。
将<div>元素添加到您希望显示倒计时计时器的检查页面 。给元素一个id属性(例如“timer”) 。
在<script>元素中,添加以下代码以在页面加载时启动倒计时计时器:
该代码将在页面加载时启动计时器 , 使用5分钟(300秒)的持续时间和id为“timer”的<div>元素 。您可以调整显示元素的持续时间和选择器,以满足您的特定需求 。
将此代码添加到考试页面后,页面加载后,倒计时计时器应立即启动,并显示考试结束前的剩余时间 。
写一个计时器 JAVA代码是什么?应该用线程里面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考试倒计时代码的java考试倒计时代码:
/*
* 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编写一个倒计时器代码 。【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();}@Overridepublic 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 {@Overridepublic 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考试倒计时代码和倒计时 java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。
推荐阅读
- 电影院订票管理系统c语言,电影院售票管理系统c语言
- 如何把图插入word,怎样把图放到word 文档
- 男子坡面技巧决赛直播,坡面障碍技巧决赛
- java死锁代码怎么找 java死锁代码示例
- 间的电视什么,电视看什么参数
- linux重启pg库命令,linux里重启命令
- 直播俯拍技巧,直播俯拍技巧视频
- linux脚本暂停命令 linux shell 暂停
- mysql千万级别数据如何存储,mysql几千万条数据