【问题标题】:Using timer to count as a stopwatch in Java Applet在 Java Applet 中使用计时器计为秒表
【发布时间】:2012-06-15 05:21:02
【问题描述】:

可以这么说,我想创建一个秒表来为我的比赛打分。假设我有一个变量:int sec = 0。当游戏开始时,我想要一个 g.drawString 来绘制小程序的时间。因此,例如每秒,sec 将增加 1。

如何让它 g.drawString(Integer.toString(sec), 40, 400) 递增 1 并每秒绘制一次?

谢谢。

编辑:

我已经弄清楚如何使用 ActionListener 并将 g.drawString 放入其中来增加它并将其打印到屏幕上,但它会相互打印。如果我将 g.drawString 放入 paint 方法中并且仅在 ActionListener 中将 sec 增加 1,则会出现闪烁。我应该使用双缓冲吗?如果是这样,我该怎么做?

【问题讨论】:

  • 不用绘图,在这种情况下,在JLabel的帮助下很容易做到这一点
  • 请看这个相关的example
  • @user1457836:接受答案

标签: java swing timer applet japplet


【解决方案1】:
import java.awt.event.*;
import javax.swing.*;

public class StopWatch extends JLabel
            implements MouseListener, ActionListener {

   private long startTime;   // Start time of stopwatch.
                             //   (Time is measured in milliseconds.)

   private boolean running;  // True when the stopwatch is running.

   private Timer timer;  // A timer that will generate events
                         // while the stopwatch is running

   public StopWatch() {
         // Constructor.
      super("  Click to start timer.  ", JLabel.CENTER);
      addMouseListener(this);
   }

   public void actionPerformed(ActionEvent evt) {
          // This will be called when an event from the
          // timer is received.  It just sets the stopwatch
          // to show the amount of time that it has been running.
          // Time is rounded down to the nearest second.
       long time = (System.currentTimeMillis() - startTime) / 1000;
       setText("Running:  " + time + " seconds");
   }

   public void mousePressed(MouseEvent evt) {
          // React when user presses the mouse by
          // starting or stopping the stopwatch.  Also start
          // or stop the timer.
      if (running == false) {
            // Record the time and start the stopwatch.
         running = true;
         startTime = evt.getWhen();  // Time when mouse was clicked.
         setText("Running:  0 seconds");
         if (timer == null) {
            timer = new Timer(100,this);
            timer.start();
         }
         else
            timer.restart();
      }
      else {
            // Stop the stopwatch.  Compute the elapsed time since the
            // stopwatch was started and display it.
         timer.stop();
         running = false;
         long endTime = evt.getWhen();
         double seconds = (endTime - startTime) / 1000.0;
         setText("Time: " + seconds + " sec.");
      }
   }

   public void mouseReleased(MouseEvent evt) { }
   public void mouseClicked(MouseEvent evt) { }
   public void mouseEntered(MouseEvent evt) { }
   public void mouseExited(MouseEvent evt) { }

}  // end StopWatchRunner

一个测试组件的小程序:

/*
   A trivial applet that tests the StopWatchRunner component.
   The applet just creates and shows a StopWatchRunner.
*/


import java.awt.*;
import javax.swing.*;

public class Test1 extends JApplet {

   public void init() {

      StopWatch watch = new StopWatch();
      watch.setFont( new Font("SansSerif", Font.BOLD, 24) );
      watch.setBackground(Color.white);
      watch.setForeground( new Color(180,0,0) );
      watch.setOpaque(true);
      getContentPane().add(watch, BorderLayout.CENTER);

   }

}

【讨论】:

  • 您实际上不必定义任何boolean variable 来了解Timer 的状态,因为您可以通过使用它的方法timerObject.isRunning() 简单地得到它。虽然 +1 为其余 :-)
  • 非常感谢,这对我有很大帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2015-06-12
  • 2015-10-20
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多