首页 > 后端开发 > 正文

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位

2024-03-25 21:26:30 | 我爱编程网

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位相关内容,小编在这里做了整理,希望能对大家有所帮助,关于java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位信息,一起来了解一下吧!

本文目录一览:

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位

简单代码如下:

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.Timer;

@SuppressWarnings("serial")

public class Timers extends JFrame {

final Label lab = new Label();

Date now = new Date();

@SuppressWarnings("deprecation")

public Timers() {

now.setHours(0);

now.setMinutes(0);

now.setSeconds(0);

setBounds(550, 270, 200, 150);

final Timer timer = new Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {

Date now2 = new Date(now.getTime() + 1000);

now = now2;

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

lab.setText(formatter.format(now));

}

});

Button b1 = new Button("开始");

Button b2 = new Button("停止");

b2.setBounds(40, 40, 40, 40);

b1.setBounds(30, 30, 30, 30);

b1.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Button b = (Button) e.getSource();

b.setLabel("开始");

timer.start();

}

});

b2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Button b = (Button) e.getSource();

b.setLabel("停止");

timer.stop();

}

});

this.setLayout(new FlowLayout());

this.add(b2);

this.add(b1);

this.add(lab);

this.setSize(300, 200);

this.setVisible(true);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String[] args) {

Timers t = new Timers();

t.lab.setText("00:00:00");

}

}

不知是否帮到你,如满意请采纳!

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位

求计时器的java程序,要完整的,能用eclipse运行

01 package com.lag.ch03227.time;

02

03 import java.awt.Color;

04 import java.awt.Font;

05

06 import javax.swing.JButton;

07 import javax.swing.JFrame;

08 import javax.swing.border.LineBorder;

09

10 public class MyTimerFrame extends JFrame{

11

12 private MyLabel lab;

13 private JButton but1,but2;

14

15 private TimeThread time;

16

17 public MyTimerFrame(){

18 super("ÅÜÃë");

19 this.setBounds(200, 200, 300, 200);

20 this.setLayout(null);

21

22 lab= new MyLabel("00:00:00");

23 this.lab.setBounds(100, 50, 100, 20);

24 this.lab.setBorder(new LineBorder(Color.BLACK));

25 this.lab.setFont(new Font("ºÚÌå",Font.BOLD,20));

26 this.lab.setHorizontalAlignment(0);

27 this.add(lab);

28

29

30

31 but1 = new JButton("¿ªÊ¼");

32 but1.setBounds(40, 110, 100, 20);

33

34 but2 = new JButton("ÖØÖÃ");

35 but2.setBounds(160, 110, 100, 20);

36

37

38 Handler h= new Handler(this);

39

40 but1.addActionListener(h);

41 but2.addActionListener(h);

42 this.add(but1);

43 this.add(but2);

44

45

46 this.setVisible(true);

47 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

48

49 time = new TimeThread(lab);

50 time.setDaemon(true);

51 time.start();

52 }我爱编程网

53

54 public JButton getBut1() {

55 return but1;

56 }

57

58 public void setBut1(JButton but1) {

59 this.but1 = but1;

60 }

61

62 public JButton getBut2() {

63 return but2;

64 }

65

66 public void setBut2(JButton but2) {

67 this.but2 = but2;

68 }

69

70 public MyLabel getLab() {

71 return lab;

72 }

73

74 public void setLab(MyLabel lab) {

75 this.lab = lab;

76 }

77

78

79 public static void main(String[] args) {

80 new MyTimerFrame();

81 }

82 }

3. [文件] MyLabel.java ~ 2KB 下载(39)

001 package com.lag.ch03227.time;

002

003 import java.text.DecimalFormat;

004

005 import javax.swing.JLabel;

006

007 public class MyLabel extends JLabel{

008 private int min,senconds,ps;

009 private boolean flag=false;

010 private DecimalFormat df = new DecimalFormat("00");

011

012 public MyLabel(String names) {

013 super(names);

014 }

015

016 public int getMin() {

017 return min;

018 }

019

020 public void setMin(int min) {

021 this.min = min;

022 }

023

024 public int getSenconds() {

025 return senconds;

026 }

027

028 public void setSenconds(int senconds) {

029 this.senconds = senconds;

030 }

031

032 public int getPs() {

033 return ps;

034 }

035

036 public void setPs(int ps) {

037 this.ps = ps;

038 }

039

040

041

042 public boolean isFlag() {

043 return flag;

044 }

045

046 public void setFlag(boolean flag) {

047 this.flag = flag;

048 }

049

050

051 public DecimalFormat getDf() {

052 return df;

053 }

054

055 public void setDf(DecimalFormat df) {

056 this.df = df;

057 }

058

059 /**

060 * ÖØÖÃ

061 */

062 public void init(){

063 this.flag=false;

064 this.min=0;

065 this.senconds=0;

066 this.ps=0;

067

068 this.setText("00:00:00");

069 }

070

071 /**

072 * ¼ÆʱÆ÷

073 */

074 public synchronized void timer(){

075 if(!flag){

076 try {

077 this.wait();

078 } catch (InterruptedException e) {

079 // TODO Auto-generated catch block

080 e.printStackTrace();

081 }

082 }

083 ps++;

084 if(ps>=100){

085 senconds++;

086 ps=0;

087 if(senconds>=60){

088 min++;

089 senconds=0;

090 }

091 }

092

093 this.setText(df.format(min)+":"+df.format(senconds)+":"+df.format(ps));

094 }

095 /**

096 * »½ÐÑÐÝÃß

097 */

098 public synchronized void notifyLabel() {

099 this.notifyAll();

100 }

101

102 }

4. [文件] Handler.java ~ 671B 下载(41)

01 package com.lag.ch03227.time;

02

03 import java.awt.event.ActionEvent;

04 import java.awt.event.ActionListener;

05

06 public class Handler implements ActionListener{

07 private MyTimerFrame win;

08

09 public Handler(MyTimerFrame win) {

10 super();

11 this.win = win;

12 }

13

14 public void actionPerformed(ActionEvent e) {

15

16 if(e.getActionCommand().equals("开始")){

17 win.getBut1().setText("暂停");

18 win.getLab().setFlag(true);

19 win.getLab().notifyLabel();

20 }else if(e.getActionCommand().equals("暂停")) {

21 win.getBut1().setText("开始");

22 win.getLab().setFlag(false);

23

24 }else{

25

26 win.getBut1().setText("开始");

27 win.getLab().init();

28 }

29 }

30

31 }

5. [文件] TimeThread.java ~ 375B 下载(40) 跳至 [2] [3] [4] [5] [全屏预览]

view source

print?

01 package com.lag.ch03227.time;

02

03 public class TimeThread extends Thread{

04 private MyLabel lab;

05

06 public TimeThread(MyLabel lab) {

07 super();

08 this.lab = lab;

09 }

10

11

12 @Override

13 public void run() {

14

15 while(true){

16 try {

17 Thread.sleep(10);

18 } catch (InterruptedException e) {

19

20 e.printStackTrace();

21 }

22 lab.timer();

23 }

24 }

25

26

27

28 }

java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位

求一个倒计时,秒表。需要JAVA. 需要有断电记忆功能

以下是一个简单的Java倒计时和秒表程序示例,其中使用了Timer和'计时器任务TimerTask类来实现计时功能,使用了'FileFile和'FileWriterFileWriter类来实现断电记忆功能。在程序中,倒计时可以通过设置'countDownSecondscountDownSeconds变量来设置,秒表可以通过点击"开始"和"停止"按钮来控制计时。每次停止计时后,程序将自动保存当前计时的时间戳,以实现断电记忆功能。

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.*;

public class TimerExample extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

private JLabel countDownLabel, stopWatchLabel;

private JButton countDownButton, stopWatchButton, stopButton;

private Timer countDownTimer, stopWatchTimer;

private int countDownSeconds, stopWatchSeconds;

private long lastStopWatchTime;

public TimerExample() {

super("倒计时和秒表");

setLayout(new GridLayout(3, 2, 10, 10));

// 倒计时标签和按钮

countDownLabel = new JLabel("倒计时: 00:00:00");

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

countDownButton.addActionListener(this);

add(countDownLabel);

add(countDownButton);

// 秒表标签和按钮

stopWatchLabel = new JLabel("秒表: 00:00:00");

stopWatchButton = new JButton("开始");

stopWatchButton.addActionListener(this);

stopButton = new JButton("停止");

stopButton.addActionListener(this);

stopButton.setEnabled(false);

add(stopWatchLabel);

add(stopWatchButton);

add(stopButton);

// 初始化计时器

countDownTimer = new Timer(1000, new CountDownTask());

stopWatchTimer = new Timer(1000, new StopWatchTask());

// 恢复上一次停止的时间

try {

File file = new File("last_stopwatch_time.txt");

if (file.exists()) {

BufferedReader reader = new BufferedReader(new FileReader(file));

lastStopWatchTime = Long.parseLong(reader.readLine());

reader.close();

stopWatchSeconds = (int) ((System.currentTimeMillis() - lastStopWatchTime) / 1000);

updateStopWatchLabel();

}

} catch (IOException e) {

e.printStackTrace();

}

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 200);

setVisible(true);

}

private void updateCountDownLabel() {

int hours = countDownSeconds / 3600;

int minutes = (countDownSeconds % 3600) / 60;

int seconds = countDownSeconds % 60;

countDownLabel.setText(String.format("倒计时: %02d:%02d:%02d", hours, minutes, seconds));

}

private void updateStopWatchLabel() {

int hours = stopWatchSeconds / 3600;

int minutes = (stopWatchSeconds % 3600) / 60;

int seconds = stopWatchSeconds % 60;

stopWatchLabel.setText(String.format("秒表: %02d:%02d:%02d", hours, minutes, seconds));

}

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == countDownButton) {

if (countDownTimer.isRunning())

以上就是java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位全部内容了,了解更多相关信息,关注我爱编程网。
与“java中如何实现自动计时功能,就是点击一个start按钮就开始计时,以秒为单位”相关推荐
如何做一个Java循环超时的程序,当程序运行30秒以上,循环跳出
如何做一个Java循环超时的程序,当程序运行30秒以上,循环跳出

java应用调用数据库超时,数据库自己sql很正常Java应用调用数据库超时是指在Java应用程序尝试从数据库中获取数据时,由于某种原因,Java应用程序无法获取数据,超过了约定的时间,从而导致程序出现超时问题。超时的原因可以是Java应用程序的查询语句所花费的时间过长,也可以是网络问题引起的网络延迟。要解决数据库超时问题,可以采取一些措施:1、优化SQL语句,将查询语句优化,减

2024-03-21 11:44:36
为什么点开new中python3后然后黑框就闪退了
为什么点开new中python3后然后黑框就闪退了

为什么点开new中python3后然后黑框就闪退了可能是系统不兼容导致的。如果打不开或者闪退,可以尝试选择打开方式,选择Python应用程序或者文本编译器看看是否能够打开文件。先尝试了双击,未打开,接着选择打开方式pthon,还是失败。然后选择了平时的文本编译器Geany,成功打开了命名为commentpy的python文件。python在重命名时黑框出现闪退重命名时

2024-02-18 11:47:26
计时器怎么做
计时器怎么做

python的两种编程方法Python的两种主要编程方法是面向过程编程和面向对象编程。面向过程编程是一种基础的编程方法,它主要关注的是程序的执行流程。在这种编程方法中,程序员需要明确程序每一步的操作,并按照顺序编写代码。这种方法的优点是简单直接,易于理解和调试。然而,当程序变得复杂时,面向过程的代码可能会变得难以管理和维护。举个例子,如果我们想要编写一个程序来计算两个数的和,面向过程

2023-12-18 17:59:25
Python中,如何实现函数的自定义?
Python中,如何实现函数的自定义?

Python中,如何实现函数的自定义?此题考察自定义函数,代码如下:intMinCommonMultiple(inta,intb){inti;if(a<=0||b<=0)return-1;       //保证输入的参数为正整数for(i=1;i<b;i++){if((i*a)%b==0)returni*a;}

2024-01-08 23:14:08
如何检测一个JAVA程序的运行时间
如何检测一个JAVA程序的运行时间

JAVA获取一段程序运行时间abstract class GetTime { public final void getTime() { long start = System.currentTimeMillis(); runcode(); long end = System.currentTimeMillis(); System.out.println("运行时间:" + (end

2024-03-16 10:43:18
如何在JAVA中每隔一段时间执行一段程序
如何在JAVA中每隔一段时间执行一段程序

简述Java程序从编写到运行的基本步骤,并说明Java的基本工作原理Java编译原理:Java虚拟机(JVM)是可运行Java代码的假想计算机。只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该系统上运行。一.Java源文件的编译、下载、解释和执行Java应用程序的开发周期包括编译、下载、解释和执行几个部分。Java编译

2024-03-24 16:51:53
如何用Python实现对字符串进行频率统计?
如何用Python实现对字符串进行频率统计?

如何用Python实现对字符串进行频率统计?下面是一个Python的实现,可以输入任意字符串,统计其中元音字母(不区分大小写)出现的次数和频率:例如,输入字符串"HelloWorld!",程序会输出:这里使用了一个字符串变量vowels来保存元音字母,使用一个计数器变量count来记录元音字母出现次数,然后遍历输入的字符串s,如果当前字符是元音字母(不区分大小写),则将计数器加

2024-01-14 08:21:43
怎么用Python实现时间加减运算?
怎么用Python实现时间加减运算?

1.+编写程序,简单的加减运算,单步调试。信息输出窗口观察程序、数据所占用?这task可以使用任何一种编程语言来实现,以下是一个Python的例子:#定义两个变量,用于加减运算a=5b=3#加法运算result_add=a+b#减法运算result_sub=a-b#输出结果print("加法运算的结果是:",result_add)print("减法运算

2024-01-06 20:18:27