運用 Thread 讓時間不斷刷新- [hide]package tw.kuas.edu.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.SwingConstants;
- import com.jgoodies.forms.factories.DefaultComponentFactory;
- public class Main extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- JLabel NowTime;
- public Main() {
- setTitle("\u5C0F\u6642\u9418");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 345, 83);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- NowTime = new JLabel("New label");
- NowTime.setHorizontalAlignment(SwingConstants.CENTER);
- NowTime.setBounds(27, 10, 279, 38);
- contentPane.add(NowTime);
- GetTime();
-
- }
-
- public void GetTime()
- {
- TimeThread thread = new TimeThread(NowTime);
- thread.start();
-
- }
- }
- [/hide]
複製代碼 |