JavaFX秒表计时器

前端之家收集整理的这篇文章主要介绍了JavaFX秒表计时器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个用于 JavaFX的简单秒表的类,根据需要设置Label对象的样式
  1. package aaa;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import javafx.beans.property.SimpleStringProperty;
  6.  
  7. /**
  8. *
  9. * @author D07114915
  10. */
  11. public class KTimer extends Thread {
  12.  
  13. private Thread thread = null;
  14. private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:S");
  15. private String[] split;
  16. private SimpleStringProperty min,sec,millis,sspTime;
  17. private long time;
  18.  
  19. public static void main(String[] args) {
  20. KTimer t = new KTimer();
  21. t.startTimer(00);
  22. }
  23.  
  24. public KTimer() {
  25. min = new SimpleStringProperty("00");
  26. sec = new SimpleStringProperty("00");
  27. millis = new SimpleStringProperty("00");
  28. sspTime = new SimpleStringProperty("00:00:00");
  29. }
  30.  
  31. public void startTimer(long time) {
  32. this.time = time;
  33. thread = new Thread(this);
  34. thread.setPriority(Thread.MIN_PRIORITY);
  35. thread.start();
  36. }
  37.  
  38. public void stopTimer(long time) {
  39. if (thread != null) {
  40. thread.interrupt();
  41. }
  42. this.time = time;
  43. setTime(time);
  44. }
  45.  
  46. public void setTime(long time) {
  47. this.time = time;
  48. split = sdf.format(new Date(time)).split(":");
  49. min.set(split[0]);
  50. sec.set(split[1]);
  51.  
  52. if (split[2].length() == 1) {
  53. split[2] = "0" + split[2];
  54. }
  55. millis.set(split[2].substring(0,2));
  56.  
  57. sspTime.set(min.get() + ":" + sec.get() + ":" + millis.get());
  58. }
  59.  
  60. public long getTime() {
  61. return time;
  62. }
  63.  
  64. public SimpleStringProperty getSspTime() {
  65. return sspTime;
  66. }
  67.  
  68. @Override
  69. public void run() {
  70. try {
  71. while (!thread.isInterrupted()) {
  72. setTime(time);
  73. sleep(10);
  74. time = time + 10;
  75. }
  76. } catch (Exception e) {
  77. }
  78.  
  79. }
  80. }//end of class

现在只需在属性获取GUI的监听器

添加变量

  1. KTimer ktimer;
  2. Label timeLabel;

在你的班级初始化变量

  1. //Clock
  2. ktimer = new KTimer();
  3. timeLabel = new Label(ktimer.getSspTime().get());
  4. ktimer.getSspTime().addListener(new InvalidationListener() {
  5.  
  6. @Override
  7. public void invalidated(Observable observable) {
  8. timeLabel.setText(ktimer.getSspTime().get());
  9. }
  10. });

然后调用方法在任何需要的地方启动和停止

停止并重置是

  1. ktimer.stopTimer(0);

启动和暂停计时器是

  1. ktimer.startTimer(ktimer.getTime());

任何改进,因为类有点cpu饥饿…,但您可以调整运行线程和setTime(时间)函数以适应应用程序

解决方法

这是一个略有不同的版本(可能更好),我不确定同步方法是否真的有必要
  1. package aaa;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. import javafx.beans.property.SimpleStringProperty;
  8.  
  9. /**
  10. *
  11. * @author D07114915
  12. */
  13. public class KTimer {
  14.  
  15. private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss:S");
  16. private String[] split;
  17. private SimpleStringProperty sspTime;
  18. private long time;
  19. private Timer t = new Timer("Metronome",true);
  20. private TimerTask tt;
  21. boolean timing = false;
  22.  
  23. public KTimer() {
  24. sspTime = new SimpleStringProperty("00:00:00");
  25. }
  26.  
  27. public void startTimer(final long time) {
  28. this.time = time;
  29. timing = true;
  30. tt = new TimerTask() {
  31.  
  32. @Override
  33. public void run() {
  34. if (!timing) {
  35. try {
  36. tt.cancel();
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. } else {
  41. updateTime();
  42. }
  43. }
  44. };
  45. t.scheduleAtFixedRate(tt,10,10);
  46. }
  47.  
  48. public synchronized void stopTimer() {
  49. timing = false;
  50. }
  51.  
  52. public synchronized void updateTime() {
  53. this.time = this.time + 10;
  54. split = sdf.format(new Date(this.time)).split(":");
  55. sspTime.set(split[0] + ":" + split[1] + ":" + (split[2].length() == 1 ? "0" + split[2] : split[2].substring(0,2)));
  56. }
  57.  
  58. public synchronized void moveToTime(long time) {
  59. stopTimer();
  60. this.time = time;
  61. split = sdf.format(new Date(time)).split(":");
  62. sspTime.set(split[0] + ":" + split[1] + ":" + (split[2].length() == 1 ? "0" + split[2] : split[2].substring(0,2)));
  63. }
  64.  
  65. public synchronized long getTime() {
  66. return time;
  67. }
  68.  
  69. public synchronized SimpleStringProperty getSspTime() {
  70. return sspTime;
  71. }
  72. }

猜你在找的Java相关文章