JAVA programming (07.3) ----- clock facing object design
1. Three input methods (heavy load) for designing clocks for objects)
Package com. lovo; import java. text. decimalFormat; import java. util. calendar;/*** clock class ** @ author Abe ** // *** attribute, hour, minute, seconds ** @ author Abe **/public class Clock {private int hour; private int min; private int sec; /*** directly input the constructor 1 */public Clock (int hour, int min, int sec) {this. hour = hour; this. min = min; this. sec = sec;}/*** constructor 2 input String segmentation input ** @ param str */public Clock (String str) {String s1 = str. split (":") [0]; String s2 = str. split (":") [1]; String s3 = str. split (":") [2]; this. hour = Integer. parseInt (s1); this. min = Integer. parseInt (s2); this. sec = Integer. parseInt (s3);}/*** constructor 3 default constructor input system time */public Clock () {Calendar ar cal = Calendar ar. getInstance (); this. hour = cal. get (Calendar. HOUR_OF_DAY); this. min = cal. get (Calendar. MINUTE); this. sec = cal. get (Calendar. SECOND);}/*** action: SECOND */public Clock go () {sec ++; if (sec = 60) {min ++; sec = 0 ;}if (min = 60) {hour ++; min = 0 ;}if (hour = 24) {hour = 0 ;}return this ;} /*** action: countdown */public Clock back () {if (sec> 0) {sec --;} else if (min> 0) {min --; sec = 59;} else if (hour> 0) {hour --; min = 59; sec = 59;} return this ;} /*** swap the output memory address to a String */public String toString () {DecimalFormat df = new DecimalFormat ("00"); return df. format (hour) + ":" + df. format (min) + ":" + df. format (sec );}}
Then, display the countdown in the pop-up window.
Package com. lovo;/*** run the clock in the pop-up window * @ author Abe */import java. awt. font; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. util. imports; import java. util. concurrent. delayed; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. timer; public class Clockshow {public static Timer timer = null; public static void main (String [] args) {kernel SC = new kernel (System. in); // System. out. printf ("Enter the current time:"); final Clock c = new Clock (""); JFrame j = new JFrame (); j. setSize (600,480); j. setResizable (false); j. setLocationRelativeTo (null); j. setTitle ("careful bomb"); j. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); final JLabel lbl = new JLabel ("time", JLabel. CENTER); // new content, fill in the text "time" // text position CENTER Font font = new Font ("", Font. PLAIN, 60); // set the format font, bold, and lbl. setFont (font); // the text box calls the font format j. setLayout (null); // After the layout manager is enabled, Bounds can use lbl. setBounds (100,100,300,100); // lbl can be used only when the Location of the text box is enabled. setText (c. toString (); // call clbl in the text box. setLocation (150,170); // set the position where the Bounds is located. add (lbl); j. setVisible (true); // window visible timer = new Timer (500, new ActionListener () {// here you cannot select the correct automatic output @ Overridepublic void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stubc. back (); // What is the clock going to do? Write here lbl. setText (c. toString (); if (c. toString (). equals ("00:00:00") {lbl. setText ("time to"); // The timer is displayed when the clock stops. stop () ;}}); // create a timer object timer. start (); // start the timer SC. close ();}}