定时提醒工具

来源:互联网 发布:网络爬虫 翻译 编辑:程序博客网 时间:2024/04/30 03:43
没事写了个定时提醒,可以隐藏界面,嘛~倒没什么技术含量,只是苦于Cairo-Dock的定时闹钟不能正常工作,遂有感而发。

当然Testing for Linux only,不过在OpenJDK和SunJDK下应该正常吧……编译后直接执行即可


package wafly.application.simAlarm;import java.awt.Button;import java.awt.Dialog;import java.awt.Dimension;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.Toolkit;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.regex.Pattern;public class Main{private static final Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();private static Frame inputWindow = new Frame();private static Label timeLabel =new Label("当前未设定提醒时间");private static Dialog confirmDlg = new Dialog(inputWindow, true);private static Label confirmMsg = new Label("退出时将同时取消定时通知");private static long settedNotifyTime = 0;private static Window notifyDlg = new Window(inputWindow);private static Label notifyMsg = new Label();private static boolean needNotify=false;public static void main(String[] args){createConfirmDlg();createNotifyDlg();inputWindow.setLayout(null);inputWindow.setSize(300, 100);inputWindow.setLocation(((ScreenSize.width-300)/2), (ScreenSize.height-100)/2);inputWindow.setIconImage(b.getToolkit().getImage("SimAlarm.png"));inputWindow.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){confirmDlg.setVisible(true);}});final TextField timeInput =new TextField("输入时间 HH:mm");timeInput.setSize(123, 20);timeInput.setLocation(25, 40);timeInput.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e){timeInput.selectAll();}});inputWindow.add(timeInput);final TextField labelInput =new TextField("输入提醒信息");labelInput.setSize(123, 20);labelInput.setLocation(152, 40);labelInput.addMouseListener(new MouseAdapter(){public void mouseReleased(MouseEvent e){labelInput.selectAll();}});inputWindow.add(labelInput);timeLabel.setSize(145, 20);timeLabel.setLocation(25, 70);inputWindow.add(timeLabel);new Thread(){public void run(){try{ while(true){Thread.sleep(1000);if(needNotify && Calendar.getInstance().getTimeInMillis()>settedNotifyTime){notifyDlg.setVisible(true);notifyDlg.requestFocus();}}}catch(Exception e){System.exit(0);}}}.start();Button setBtn = new Button("设定");setBtn.setSize(50, 20);setBtn.setLocation(170, 70);setBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){if(Pattern.compile("[0-2][0-9]:[0-6][0-9]").matcher(timeInput.getText()).matches()){try{Calendar settedNotifyDateTime = Calendar.getInstance();settedNotifyDateTime.setTime(new SimpleDateFormat("HH:mm").parse(timeInput.getText()));Calendar realNotifyDateTime = Calendar.getInstance();realNotifyDateTime.set(Calendar.HOUR_OF_DAY, settedNotifyDateTime.get(Calendar.HOUR_OF_DAY));realNotifyDateTime.set(Calendar.MINUTE, settedNotifyDateTime.get(Calendar.MINUTE));realNotifyDateTime.set(Calendar.SECOND, 0);if(realNotifyDateTime.getTimeInMillis()<Calendar.getInstance().getTimeInMillis()){realNotifyDateTime.add(Calendar.DAY_OF_YEAR, 1);}settedNotifyTime =realNotifyDateTime.getTimeInMillis();timeLabel.setText("下次提醒时间: "+timeInput.getText());notifyMsg.setText(labelInput.getText());if(notifyMsg.getText().trim().equals("") || notifyMsg.getText().trim().equals("输入提醒信息")){notifyMsg.setText("时间到了");}needNotify=true;}catch (Exception ex){timeInput.setText("输入时间 HH:mm");timeInput.selectAll();}}else{timeInput.setText("输入时间 HH:mm");timeInput.selectAll();}}});inputWindow.add(setBtn);Button hideBtn = new Button("隐藏");hideBtn.setSize(50, 20);hideBtn.setLocation(225, 70);hideBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){if(!needNotify){timeInput.setText("输入时间 HH:mm");timeInput.selectAll();return;}inputWindow.setVisible(false);inputWindow.setState(Frame.ICONIFIED);}});inputWindow.add(hideBtn);inputWindow.setVisible(true);}private static void createConfirmDlg(){confirmDlg.setLayout(null);confirmDlg.setSize(200, 100);confirmDlg.setLocation(((ScreenSize.width-200)/2), (ScreenSize.height-100)/2);confirmMsg.setSize(150, 20);confirmMsg.setLocation(25, 40);confirmMsg.setAlignment(Label.CENTER);confirmDlg.add(confirmMsg);Button yesBtn = new Button("是");yesBtn.setSize(50, 20);yesBtn.setLocation(25, 70);yesBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){System.exit(0);}});confirmDlg.add(yesBtn);Button noBtn = new Button("否");noBtn.setSize(50, 20);noBtn.setLocation(125, 70);noBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){confirmDlg.setVisible(false);}});confirmDlg.add(noBtn);}private static void createNotifyDlg(){notifyDlg.setLayout(null);notifyDlg.setAlwaysOnTop(true);notifyDlg.setSize(300, 100);notifyDlg.setLocation(((ScreenSize.width-300)/2), (ScreenSize.height-100)/2);notifyMsg.setSize(250, 20);notifyMsg.setLocation(25, 40);notifyMsg.setAlignment(Label.CENTER);notifyDlg.add(notifyMsg);Button yesBtn = new Button("知道了");yesBtn.setSize(100, 20);yesBtn.setLocation(25, 70);yesBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){needNotify=false;notifyDlg.setVisible(false);inputWindow.setVisible(true);timeLabel.setText("当前未设定提醒时间");}});notifyDlg.add(yesBtn);Button waitBtn = new Button("再稍候5分钟");waitBtn.setSize(100, 20);waitBtn.setLocation(175, 70);waitBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){settedNotifyTime=Calendar.getInstance().getTimeInMillis()+1000*60*5;notifyDlg.setVisible(false);}});notifyDlg.add(waitBtn);}}


原创粉丝点击