Revision: 1515
Updated Code
at October 14, 2006 18:11 by jugyo
Updated Code
import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.Timer; import javax.swing.*; /** * シンプルãªã‚¿ã‚¤ãƒžãƒ¼ */ public class SimpleTimer extends JWindow { /** * メイン関数 */ public static void main(String[] args) { SimpleTimer simpleTimer = new SimpleTimer(); simpleTimer.pack(); simpleTimer.setAlwaysOnTop(true); simpleTimer.setVisible(true); } private Long startTimeMillis; private JLabel label; private SimpleDateFormat dateFormat; private Timer timer; /** * コンストラクタ */ public SimpleTimer() { initDateFormat(); initConponents(); resetTime(); updateLabelText(); startTimer(); } /** * DateFormatã®åˆæœŸåŒ– */ private void initDateFormat() { dateFormat = new SimpleDateFormat("HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); } /** * GUIコンãƒãƒ¼ãƒãƒ³ãƒˆã®åˆæœŸåŒ– */ private void initConponents() { label = new JLabel(); label.setFont(new Font(label.getFont().getName(), Font.BOLD, 14)); label.setOpaque(true); label.setBackground(Color.black); label.setForeground(Color.white); label.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); getContentPane().add(label); final JPopupMenu popup = new JPopupMenu(); JMenuItem menuItem1 = new JMenuItem("Reset"); menuItem1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resetTime(); } }); popup.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Exit"); menuItem2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(menuItem2); label.setComponentPopupMenu(popup); label.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON2 && e.getClickCount() == 1) { popup.setVisible(true); } } }); } /** * リセット */ private void resetTime() { startTimeMillis = Calendar.getInstance().getTimeInMillis(); } /** * ラベル更新 */ private void updateLabelText() { SwingUtilities.invokeLater(new Runnable() { public void run() { long elapsedTime = Calendar.getInstance().getTimeInMillis() - startTimeMillis; Date date = new Date(elapsedTime); label.setText(dateFormat.format(date)); } }); } /** * タイマー開始 */ private void startTimer() { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { updateLabelText(); } }, 0, 1000); } }
Revision: 1514
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at October 14, 2006 18:09 by jugyo
Initial Code
import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.Timer; import javax.swing.*; /** * シンプルãªã‚¿ã‚¤ãƒžãƒ¼ */ public class SimpleTimer extends JWindow { /** * メイン関数 */ public static void main(String[] args) { SimpleTimer simpleTimer = new SimpleTimer(); simpleTimer.pack(); simpleTimer.setAlwaysOnTop(true); simpleTimer.setVisible(true); } private Long startTimeMillis; private JLabel label; private SimpleDateFormat dateFormat; private Timer timer; /** * コンストラクタ */ public SimpleTimer() { initDateFormat(); initConponents(); resetTime(); updateLabelText(); startTimer(); } /** * DateFormatã®åˆæœŸåŒ– */ private void initDateFormat() { dateFormat = new SimpleDateFormat("HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); } /** * GUIコンãƒãƒ¼ãƒãƒ³ãƒˆã®åˆæœŸåŒ– */ private void initConponents() { label = new JLabel(); label.setFont(new Font(label.getFont().getName(), Font.BOLD, 14)); label.setOpaque(true); label.setBackground(Color.black); label.setForeground(Color.white); label.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2)); getContentPane().add(label); final JPopupMenu popup = new JPopupMenu(); JMenuItem menuItem1 = new JMenuItem("Reset"); menuItem1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resetTime(); } }); popup.add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Exit"); menuItem2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(menuItem2); label.setComponentPopupMenu(popup); label.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON2 && e.getClickCount() == 1) { popup.setVisible(true); } } }); } /** * リセット */ private void resetTime() { startTimeMillis = Calendar.getInstance().getTimeInMillis(); } /** * ラベル更新 */ private void updateLabelText() { SwingUtilities.invokeLater(new Runnable() { public void run() { long elapsedTime = Calendar.getInstance().getTimeInMillis() - startTimeMillis; Date date = new Date(elapsedTime); label.setText(dateFormat.format(date)); } }); } /** * タイマー開始 */ private void startTimer() { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { updateLabelText(); } }, 0, 500); } }
Initial URL
Initial Description
シンプルãªã‚¿ã‚¤ãƒžãƒ¼
Initial Title
SimpleTimer.java
Initial Tags
Initial Language
Java