1.3 Types of input methods for object design clocks (heavy duty)
Package Com.lovo;import java.text.decimalformat;import java.util.calendar;/** * Clock class * @author Abe * *//** * attribute, hours, minutes, seconds * * @author Abe * */public class Clock {private int hour;private int min;private int sec;/** * Constructor 1 Direct input */public Clock (in T hour, int min, int sec) {This.hour = Hour;this.min = Min;this.sec = sec;} /** * Constructor 2 input string auto-split entry * * @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 entry system time */public Clock () {Calendar cal = Calendar.getinstance (); this.hour = Cal.get (Calendar.hour_of_day); This.min = Cal.get (calendar.minute); this.sec = Cal.get (Calendar.second);} /** * Action: Walk seconds */public Clock Go () {sec++;if (sec = =) {min++;sec = 0;} if (min = =) {hour++;min = 0;} if (hour = =) {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;} /** * Output memory address in exchange for string */public strings toString () {DecimalFormat df = new DecimalFormat ("XX"); return Df.format (hour) + ":" + D F.format (min) + ":" + df.format (sec);}}
Then it shows the countdown in the pop-up window.
Package com.lovo;/** * Clock runs in a pop-up window * @author Abe */import java.awt.font;import java.awt.event.actionevent;import java.awt . event. Actionlistener;import Java.util.scanner;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 Ma In (string[] args) {Scanner sc = new Scanner (system.in);//System.out.printf ("Please enter the current time:"); final clock C = new Clock ("1:0:4" ); JFrame j = new JFrame (); J.setsize (n, 480); j.setresizable (false); J.setlocationrelativeto (null); J.settitle ("Beware of bombs"); J.setdefaultcloseoperation (Jframe.exit_on_close); final JLabel lbl = new JLabel ("Time", Jlabel.center); New content, fill text "Time"//text position centered font font = new Font ("Microsoft Jas Black", Font.plain, 60); Format font, bold, etc., size lbl.setfont (font); text box call font format j.setlayout (null); After opening the layout manager bounds can use Lbl.setbounds (100, 100, 300, 100); Lbl.settext (C.tostring ()) is available at location after the text box position is opened; Text box content call Clbl.setlocation (150, 170); Position top off BounThe location of the DS is set J.add (LBL); j.setvisible (true); Window Visual timer = new Timer ($, new ActionListener () {//Here cannot be handwritten select the correct automatic out below @overridepublic void actionperformed (Actioneven T e) {//TODO auto-generated method Stubc.back ();//clock to do what, write here Lbl.settext (c.tostring ()), if (c.tostring (). Equals (" 00:00:00 ")) {Lbl.settext (" time to ");//clock stops displaying this timer.stop ();}}); Create timer object Timer.start ();//Start Timer sc.close ();}}
Java Programming (07.3)-----Face object Design Clock