本帖最後由 黃茂勛 於 2018-6-2 11:43 編輯
- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import java.awt.Color;
- import java.awt.SystemColor;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.SwingConstants;
- import java.awt.Font;
- import javax.swing.JLabel;
- public class haung201862 extends JFrame {
- private JPanel contentPane;
- private JLabel Date;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- haung201862 frame = new haung201862();
- frame.setVisible(true);
- frame.setTitle("小時鐘");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public haung201862() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 345, 117);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- Date = new JLabel("");
- Date.setHorizontalAlignment(SwingConstants.CENTER);
- Date.setFont(new Font("新細明體", Font.PLAIN, 18));
- Date.setBounds(25, 10, 280, 65);
- contentPane.add(Date);
-
- GetTime();
- }
- public void GetTime()
- {
- TimeThread thread = new TimeThread(Date);
- thread.start();
- }
- }
複製代碼- package bbs.istak.org.tw;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import javax.swing.JLabel;
- public class TimeThread extends Thread{
- private JLabel lb = null;
- TimeThread(JLabel lb)
- {
- this.lb = lb;
- }
- public void run()
- {
-
- try {
- while(true)
- {
- Date d = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd E a hh:mm:ss ");
- String now = sdf.format(d);
- Thread.sleep(1000);
- lb.setText(now);
- }
- } catch (InterruptedException e) {
- // TODO 自動產生的 catch 區塊
- e.printStackTrace();
- }
- }
- }
複製代碼 |