import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Toolkit; import java.io.IOException; /** A break scheduler to remind users to give their eyes a rest every so many minutes. On startup, this class pops up a dialog asking the user to schedule a break. On scheduling, the dialog disappears for a specified number of minutes. After the time, it reappears and advises the user to take a break. After taking a break, he can then schedule another break or quit the dialog. */ public class Breaktime { public static void main(String[] args) throws InterruptedException, IOException { final int MINUTES_BETWEEN_BREAKS = 20; final int MINUTES_OF_BREAK = 3; JFrame frame = new JFrame(); String msg = "Please take a break...\n" + "Do you want to schedule another break?"; Object[] array = { msg }; JOptionPane option_pane = new JOptionPane(array, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); Object[] options = { "Yes", "No", "Beep after " + MINUTES_OF_BREAK + " minutes" }; option_pane.setOptions(options); // Continue scheduling breaks indefinitely, until user stops scheduling // or closes the dialog. while (true) { JDialog dialog = option_pane.createDialog(frame, "Breaktime"); dialog.setAlwaysOnTop(true); dialog.setVisible(true); Object result = option_pane.getValue(); // Close or No if (result == null || result == options[1]) { System.exit(0); } // Signal end of break else if (result == options[2]) { Thread.sleep(MINUTES_OF_BREAK * 60 * 1000); Toolkit.getDefaultToolkit().beep(); } // Schedule another break else if (result == options[0]) { Thread.sleep(MINUTES_BETWEEN_BREAKS * 60 * 1000); } } } }