Java Source - Tạo đồng hồ số bằng chữ phien bản 2 (How to created Text Clock in Java version 2.)


This is a simple text clock.
Hướng dẫn tạo ứng dụng đồng hồ bằng chữ trong Java.





Mã nguồn - Source Code

TextClock2.java (Main)
// File:   textclock/TextClock2.java
// Description: Application / Applet for a simple text clock.
// Author: Fred Swartz
// Date:   2005-02-15, 1999-05-01, 2001-11-02, 2002-11-07

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

///////////////////////////////////////////////////////////// TextClock2
public class TextClock2 extends JApplet {

    //====================================================== constructor
    public TextClock2() {
        add(new Clock());
    }

    //============================================================= main
    public static void main(String[] args) {
        JFrame window = new JFrame("Time of Day");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        window.setContentPane(new Clock());

        window.pack();
        window.setVisible(true);
        System.out.println(window.getContentPane().size());
    }
}

Clock.java (Class)
// File:   textclock/Clock.java
// Description: This implements a text clock by subclassing
//              JTextField, changing its appearance and
//              adding a timer to update itself.
//              Regarding subclassing JTextField
//              PROS: It's simple.
//                    All JTextField methods are available
//                        to the user, so they can easily
//                        customize it (fonts, colors, ...);
//              CONS: It can't become more complicated, eg,
//                        to add buttons, etc. because the
//                        users may already be using the
//                        JTextField methods.
// Author: Fred Swartz,
// Date:   15 Feb 2005
// Possible Enhancements:
//         Appearance: center, leading zeros, uneditable,
//         Function:   12/24 hour, alarm, timer, stop and start.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;

///////////////////////////////////////////////////////////// Clock
class Clock extends JTextField {
    javax.swing.Timer m_t;

    //================================================== constructor
    public Clock() {
        //... Set some attributes.
        setColumns(6);
        setFont(new Font("sansserif", Font.PLAIN, 48));

        //... Create a 1-second timer.
        m_t = new javax.swing.Timer(1000, new ClockTickAction());
        m_t.start();  // Start the timer
    }

    /////////////////////////////////////////// inner class listener
    private class ClockTickAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //... Get the current time.
            Calendar now = Calendar.getInstance();
            int h = now.get(Calendar.HOUR_OF_DAY);
            int m = now.get(Calendar.MINUTE);
            int s = now.get(Calendar.SECOND);
            setText("" + h + ":" + m + ":" + s);
        }
    }
}


Kết quả - Result





Source: leepoint





No comments:

Post a Comment