返回列表 發帖
本帖最後由 黃茂勛 於 2018-6-2 11:43 編輯
  1. package bbs.istak.org.tw;

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;

  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JTextField;

  8. import java.awt.Color;
  9. import java.awt.SystemColor;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;

  12. import javax.swing.SwingConstants;
  13. import java.awt.Font;
  14. import javax.swing.JLabel;

  15. public class haung201862 extends JFrame {

  16.         private JPanel contentPane;
  17.         private JLabel Date;
  18.         /**
  19.          * Launch the application.
  20.          */
  21.         public static void main(String[] args) {
  22.                 EventQueue.invokeLater(new Runnable() {
  23.                         public void run() {
  24.                                 try {
  25.                                         haung201862 frame = new haung201862();
  26.                                         frame.setVisible(true);
  27.                                         frame.setTitle("小時鐘");
  28.                                 } catch (Exception e) {
  29.                                         e.printStackTrace();
  30.                                 }
  31.                         }
  32.                 });
  33.         }

  34.         /**
  35.          * Create the frame.
  36.          */
  37.         public haung201862() {
  38.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.                 setBounds(100, 100, 345, 117);
  40.                 contentPane = new JPanel();
  41.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  42.                 setContentPane(contentPane);
  43.                 contentPane.setLayout(null);
  44.                
  45.                 Date = new JLabel("");
  46.                 Date.setHorizontalAlignment(SwingConstants.CENTER);
  47.                 Date.setFont(new Font("新細明體", Font.PLAIN, 18));
  48.                 Date.setBounds(25, 10, 280, 65);
  49.                 contentPane.add(Date);
  50.                
  51.                 GetTime();
  52.         }
  53.         public void GetTime()
  54.         {
  55.                 TimeThread thread = new TimeThread(Date);
  56.                 thread.start();
  57.         }
  58. }
複製代碼
  1. package bbs.istak.org.tw;

  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;

  4. import javax.swing.JLabel;

  5. public class TimeThread extends Thread{
  6.      private JLabel lb = null;
  7.          TimeThread(JLabel lb)
  8.          {
  9.                  this.lb = lb;
  10.          }
  11.          public void run()
  12.      {                     
  13.                      
  14.                      try {
  15.                              while(true)
  16.                          {
  17.                                  Date d = new Date();
  18.                                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd  E a hh:mm:ss ");
  19.                                  String now = sdf.format(d);
  20.                                  Thread.sleep(1000);
  21.                                  lb.setText(now);
  22.                          }                                                
  23.                         } catch (InterruptedException e) {
  24.                                 // TODO 自動產生的 catch 區塊
  25.                                 e.printStackTrace();
  26.                         }                        
  27.      }
  28. }
複製代碼

TOP

返回列表