Simple Tomato Working method countdown (source code)

Source: Internet
Author: User

Beginner, simple tomato work countdown, Java implementation, Eclipse.

An executable file that produces a program event txt, which can be manually edited, each time the software is opened will be read and each shutdown software will be saved. Paste the source code, because it is relatively simple, no layering.



Package Pers.yuanzi.tomoto;
Import Java.awt.gridlayout;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.ioexception;import Java.io.PrintWriter;import Java.util.Scanner;
Import Javax.swing.jframe;import Javax.swing.joptionpane;import Javax.swing.JPanel;
Main function class, call left and right two view public class UI extends JFrame {
Text Panel1 = new text ();Time Panel2 = new Time ();
File File = new file ("ToDoList.txt");Public UI () {Try{Every time the file is read, no is createdFile.createnewfile ();Scanner input = new Scanner (file);StringBuffer buffer = new StringBuffer ();while (Input.hasnextline ()) {Buffer.append (Input.nextline ());}Panel1.text.setText (Buffer.tostring ());Input.close ();}catch (Exception e){E.printstacktrace ();throw new RuntimeException (e);}
Addwindowlistener (New Windowadapter () {
@Overridepublic void windowclosing (WindowEvent e) {Save schedule when closedString str = new String (Panel1.text.getText ());try {PrintWriter output = new PrintWriter (file);Output.print (str);Output.flush ();Output.close ();} catch (FileNotFoundException E1) {E1.printstacktrace ();throw new RuntimeException (E1);}Alert when exitingif (Joptionpane.showconfirmdialog (null, "Exit", "hint", joptionpane.yes_no_option,joptionpane.question_message) = = joptionpane.yes_option) {System.exit (0);}}});
SetLayout (New GridLayout (1, 2, 20, 20));Settitle ("Tomato");SetSize (700, 400);Setdefaultcloseoperation (Do_nothing_on_close);SetVisible (TRUE);
Add (Panel1);Add (Panel2);
}public static void Main (string[] args) {UI UI =new UI ();}
}






Package Pers.yuanzi.tomoto;
Import Java.awt.borderlayout;import Java.awt.font;import java.awt.GridLayout;
Import Javax.swing.jlabel;import javax.swing.jpanel;import Javax.swing.jscrollpane;import javax.swing.JTextArea;// Left plan view public class Text extends jpanel{JTextArea Text = new JTextArea ();JLabel label = new JLabel ("To Do List:");JLabel empty = new JLabel ("");int i = 0;Public Text () {SetLayout (New BorderLayout (10,10));Label.setfont (New Font ("Microsoft Jas Black", Font.plain, 14));Empty.setfont (New Font ("Microsoft Jas Black", Font.plain, 14));Text.setcolumns (22);Text.setrows (22);Text.setlinewrap (TRUE);Text.setwrapstyleword (TRUE);Text.setfont (New Font ("Microsoft Jas Black", Font.plain, 14));JScrollPane Scrollpanel = new JScrollPane (text);Add (Label,borderlayout.north);Add (Scrollpanel,borderlayout.center);Add (Empty,borderlayout.south);}}






Package Pers.yuanzi.tomoto;
Import Java.awt.gridlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.timer;import Java.util.TimerTask;
Import Javax.sound.sampled.audioinputstream;import Javax.sound.sampled.audiosystem;import Javax.sound.sampled.clip;import Javax.swing.jbutton;import Javax.swing.jlabel;import Javax.swing.JOptionPane; Import Javax.swing.jpanel;import Javax.swing.JTextField;
public class Time extends JPanel {JLabel Label1 = new JLabel ("Time");JLabel Label2 = new JLabel ("min");JLabel label3 = new JLabel ("seconds");
JTextField Text1 = new JTextField ("0", 3);JTextField Text2 = new JTextField ("30", 3);JTextField Text3 = new JTextField ("0", 3);
JButton button2 = new JButton ("Pause/Resume");JButton Button3 = new JButton ("reset");
int hours = 0, minutes = 0, seconds = 0;int stop = 1;
Timer timer = new timer ();TimerTask timertask = null;
Public time () {Reset button Default 30 secondsButton3.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {if (timertask! = null) {stop = 1;Timertask.cancel ();}Text1.settext ("0");Text2.settext ("30");Text3.settext ("0");}});Start Pause buttonButton2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {
Checktime ();
Hours = Integer.parseint (Text1.gettext ());minutes = Integer.parseint (Text2.gettext ());seconds = Integer.parseint (Text3.gettext ());
if (1 = = stop) {stop = 0;} else {stop = 1;}Countdown Core, create a Countdown update viewTimer.schedule (timertask = new TimerTask () {@Overridepublic void Run () {if (1 = = stop) {
Text1.settext (Hours + "");Text2.settext (minutes + "");Text3.settext (seconds + "");
This.cancel ();} else {if (0! = seconds) {Seconds-= 1;Text3.settext (seconds + "");} else if (0! = minutes) {Minutes-= 1;Text2.settext (minutes + "");seconds = 59;Text3.settext (seconds + "");} else if (0! = hours) {Hours-= 1;Text1.settext (Hours + "");minutes = 59;Text2.settext (minutes + "");seconds = 59;Text3.settext (seconds + "");} else {This.cancel ();PlaySound ();Joptionpane.showmessagedialog (NULL, "Time is up!", "hint", joptionpane.information_message);stop = 1;}}}}, 0, 1000);
}});
JPanel JPanel1 = new JPanel ();Jpanel1.setlayout (New GridLayout (1, 6, 10, 10));Jpanel1.add (Text1);Jpanel1.add (Label1);Jpanel1.add (TEXT2);Jpanel1.add (Label2);Jpanel1.add (TEXT3);Jpanel1.add (LABEL3);
JPanel JPanel2 = new JPanel ();Jpanel2.setlayout (New GridLayout (1, 2, 20, 20));
Jpanel2.add (button2);Jpanel2.add (Button3);
SetLayout (New GridLayout (8, 1, 10, 20));
Add (JPANEL1);Add (JPANEL2);
}Check if the time is entered correctlyprivate void Checktime () {if (!isdigit (text1.gettext) | | |!isdigit (TEXT2.GETTEXT ()) | |!isdigit (TEXT3.GETTEXT ())|| Integer.parseint (Text2.gettext ()) < 0 | | Integer.parseint (Text2.gettext ()) > 59|| Integer.parseint (Text2.gettext ()) < 0 | | Integer.parseint (Text3.gettext ()) > 59|| Integer.parseint (Text3.gettext ()) < 0) {Joptionpane.showmessagedialog (NULL, "Please enter a valid range of time!", "hint", joptionpane.information_message);Button3.doclick ();}}
Private Boolean isdigit (String str) {for (int i = 0; i < str.length (); i++) {if (! Character.isdigit (Str.charat (i)))return false;}return true;}
How to sound when warningprivate void PlaySound () {/*audioclip Audioclip=getaudioclip (GetCodeBase (), "Directory of Audio");
Audioclip.play (); *//*audioclip au = null;try {Au = applet.newaudioclip (new File ("Kondo-Nintendo Mark.mid"). Tourl ());Au.play ();} catch (Malformedurlexception e) {E.printstacktrace ();throw new RuntimeException (e);}*/try {Clip clip = Audiosystem.getclip ();Audioinputstream InputStream = Audiosystem. Getaudioinputstream (Time.class.getResourceAsStream ("Kondo-Nintendo Mark.mid"));Clip.open (InputStream);Clip.start ();} catch (Exception e) {System.err.println (E.getmessage ());}}
}


From for notes (Wiz)

Simple Tomato Working method countdown (source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.