Posted By

jugyo on 10/14/06


Tagged

timer swing


Versions (?)

SimpleTimer.java


 / Published in: Java
 

シンプルなタイマー

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5. import java.util.Timer;
  6. import javax.swing.*;
  7.  
  8. /**
  9.  * シンプルなタイマー
  10.  */
  11. public class SimpleTimer extends JWindow {
  12.  
  13. /**
  14. * メイン関数
  15. */
  16. public static void main(String[] args) {
  17.  
  18. SimpleTimer simpleTimer = new SimpleTimer();
  19. simpleTimer.pack();
  20. simpleTimer.setAlwaysOnTop(true);
  21. simpleTimer.setVisible(true);
  22. }
  23.  
  24. private Long startTimeMillis;
  25.  
  26. private JLabel label;
  27.  
  28. private SimpleDateFormat dateFormat;
  29.  
  30. private Timer timer;
  31.  
  32. /**
  33. * コンストラクタ
  34. */
  35. public SimpleTimer() {
  36.  
  37. initDateFormat();
  38. initConponents();
  39. resetTime();
  40. updateLabelText();
  41. startTimer();
  42. }
  43.  
  44. /**
  45. * DateFormatの初期化
  46. */
  47. private void initDateFormat() {
  48.  
  49. dateFormat = new SimpleDateFormat("HH:mm:ss");
  50. dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  51. }
  52.  
  53. /**
  54. * GUIコンポーネントの初期化
  55. */
  56. private void initConponents() {
  57.  
  58. label = new JLabel();
  59. label.setFont(new Font(label.getFont().getName(), Font.BOLD, 14));
  60. label.setOpaque(true);
  61. label.setBackground(Color.black);
  62. label.setForeground(Color.white);
  63. label.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
  64. getContentPane().add(label);
  65.  
  66. final JPopupMenu popup = new JPopupMenu();
  67. JMenuItem menuItem1 = new JMenuItem("Reset");
  68. menuItem1.addActionListener(new ActionListener() {
  69.  
  70. public void actionPerformed(ActionEvent e) {
  71.  
  72. resetTime();
  73. }
  74. });
  75. popup.add(menuItem1);
  76. JMenuItem menuItem2 = new JMenuItem("Exit");
  77. menuItem2.addActionListener(new ActionListener() {
  78.  
  79. public void actionPerformed(ActionEvent e) {
  80.  
  81. System.exit(0);
  82. }
  83. });
  84. popup.add(menuItem2);
  85. label.setComponentPopupMenu(popup);
  86.  
  87. label.addMouseListener(new MouseAdapter() {
  88.  
  89. @Override
  90. public void mouseClicked(MouseEvent e) {
  91.  
  92. if (e.getButton() == MouseEvent.BUTTON2
  93. && e.getClickCount() == 1) {
  94. popup.setVisible(true);
  95. }
  96. }
  97. });
  98. }
  99.  
  100. /**
  101. * リセット
  102. */
  103. private void resetTime() {
  104.  
  105. startTimeMillis = Calendar.getInstance().getTimeInMillis();
  106. }
  107.  
  108. /**
  109. * ラベル更新
  110. */
  111. private void updateLabelText() {
  112.  
  113. SwingUtilities.invokeLater(new Runnable() {
  114.  
  115. public void run() {
  116.  
  117. long elapsedTime = Calendar.getInstance().getTimeInMillis()
  118. - startTimeMillis;
  119.  
  120. Date date = new Date(elapsedTime);
  121.  
  122. label.setText(dateFormat.format(date));
  123. }
  124. });
  125. }
  126.  
  127. /**
  128. * タイマー開始
  129. */
  130. private void startTimer() {
  131.  
  132. timer = new Timer();
  133. timer.schedule(new TimerTask() {
  134.  
  135. @Override
  136. public void run() {
  137.  
  138. updateLabelText();
  139.  
  140. }
  141. }, 0, 1000);
  142. }
  143. }

Report this snippet  

You need to login to post a comment.