Author: Not specified Language: java5
Description: Not specified Timestamp: 2013-05-11 11:35:21 +0000
View raw paste Reply
  1. package com.tieba.demo;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16.  
  17. public class MainDemo extends 1.5.0/docs/api/javax/swing/JFrame.html">JFrame {
  18.     private 1.5.0/docs/api/javax/swing/JButton.html">JButton btnTrigger = null;
  19.  
  20.         public void actionPerformed(1.5.0/docs/api/java/awt/event/ActionEvent.html">ActionEvent e) {
  21.             if (e.getActionCommand().equals("stop")) {
  22.                 btnTrigger.setText("start");
  23.                 btnTrigger.setForeground(1.5.0/docs/api/java/awt/Color.html">Color.BLACK);
  24.                 task.cancel();
  25.             } else {
  26.                 task = new 1.5.0/docs/api/java/util/TimerTask.html">TimerTask() {
  27.                     public void run() {
  28.                         lblTime.setText(new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(
  29.                                 "yyyy-MM-dd HH:mm:ss").format(new 5+0%2Fdocs%2Fapi+Date">Date()));
  30.                     }
  31.                 };
  32.                 timer.schedule(task, 0, 1000);
  33.                 btnTrigger.setText("stop");
  34.                 btnTrigger.setForeground(1.5.0/docs/api/java/awt/Color.html">Color.RED);
  35.             }
  36.         }
  37.     };
  38.  
  39.     private void initUI() {
  40.         lblTime = new 1.5.0/docs/api/javax/swing/JLabel.html">JLabel("=show time=");
  41.         btnTrigger = new 1.5.0/docs/api/javax/swing/JButton.html">JButton("start");
  42.  
  43.         panel.add(lblTime, 1.5.0/docs/api/java/awt/BorderLayout.html">BorderLayout.CENTER);
  44.         panel.add(btnTrigger, 1.5.0/docs/api/java/awt/BorderLayout.html">BorderLayout.SOUTH);
  45.  
  46.         add(panel);
  47.     }
  48.  
  49.     private void addListners() {
  50.         btnTrigger.addActionListener(clickListner);
  51.     }
  52.  
  53.     public MainDemo() {
  54.         setTitle("timer");
  55.         setBounds(0, 0, 200, 200);
  56.         setDefaultCloseOperation(1.5.0/docs/api/javax/swing/JFrame.html">JFrame.EXIT_ON_CLOSE);
  57.  
  58.         initUI();
  59.         addListners();
  60.     }
  61.  
  62.     public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) {
  63.         new MainDemo().setVisible(true);
  64.     }
  65. }
  66.  
View raw paste Reply