I made a simple reminder and automatic computer shut downer in Java about 2 months ago.
Because I always miss my meals and decide to code an app to prevent that. (But I am not using it much so there is no difference even tough i made it )
I didn't post about this in here because the main code that does the core task of the app, I got it from a website because I couldn't figure it out. I tried with Java API but couldn't. That's why I didn't post my app here. Since I don't like to post a thing that is only half made by me. Is it OK to post half work of mine or isn't that a good thing ?
If you can fill me up with that then I can clear my mind about that.
Anyway back to topic. I used Netbens to build this but only used it as the code editor. This has a GUI but I code them all and the other codes all by myself, Only used Netbeans as the code efitor and compiler and runner because it's easy to use and has so many features and even point if I get any errors. Anyway, I didn't even use its code completion because If i do so it's not good because then I'll forget some codes.
Here is what this app does.
Remind the free defined times with messages. In my case.
Breakfast time, Lunch time and Dinner time.
Then shut down the computer.
The code I used that I couldn't find out (I mean the one I got from the internet) is,
The code that gets current system time and then prints it to a JLabel. That's the code I got from the net. And some other too it's the code that uses to shut down the computer.
Anyway, Here is the code. And I am attaching the .jar file too. (If you put this to your start up folder of your Windows computer then this app automatically get started every time you start up your computer.)
Sorry I couldn't add comments to the code because I didn't want to post because as I said half of the code is not mine
Code: Select all
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import javax.swing.*;
public class R_n_S extends JFrame
{
public R_n_S()
{
super("R_and_S");
final JLabel timeLabel = new JLabel();
final JLabel message = new JLabel("Program is running, Current time:");
add(message, BorderLayout.CENTER);
add(timeLabel, BorderLayout.LINE_END);
final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
ActionListener timerListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Date date = new Date();
String time = timeFormat.format(date);
timeLabel.setText(time);
if(time.equals("8:30:00")){
JOptionPane.showMessageDialog(null,"Breakfast Time "+time);
dispose();
try{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 30");
System.exit(0);
}
catch(Exception error){}
}
else if(time.equals("12:00:00")){
JOptionPane.showMessageDialog(null,"Lunch Time "+time);
dispose();
try{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 30");
System.exit(0);
}
catch(Exception error){}
}
else if(time.equals("20:30:00")){
JOptionPane.showMessageDialog(null,"Dinner Time "+time);
dispose();
try{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("shutdown -s -t 30");
System.exit(0);
}
catch(Exception error){}
}
}
};
Timer timer = new Timer(1000, timerListener);
// to make sure it doesn't wait one second at the start
timer.setInitialDelay(0);
timer.start();
}
public static void main(String[] args) throws Exception
{
JFrame frame = new R_n_S();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(270,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Thanks