import java.awt.AWTException; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Image; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; 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 javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.Timer; public class Alerter extends JFrame implements ActionListener { private Image image;// 托盘图标 private TrayIcon trayIcon; private SystemTray systemTray;// 系统托盘 int alertInterval = 0; final String[] in = { "周", "日", "时", "分", "秒" }; final int ONEDAYMILLISECONDS = 24 * 3600 * 1000; final int[] milliSeconds = { 7 * this.ONEDAYMILLISECONDS, this.ONEDAYMILLISECONDS, 3600 * 1000, 60 * 1000, 1000 }; Timer timer; JLabel time; JLabel interval; JTextField msg; JTextField tf; JComboBox cb; JButton button; Alerter() { systemTray = SystemTray.getSystemTray();// 获得系统托盘的实例 try { image = Toolkit.getDefaultToolkit().getImage( getClass().getResource("temp.jpg"));// 定义托盘图标的图片 trayIcon = new TrayIcon(image, "系统托盘"); systemTray.add(trayIcon); } catch (AWTException e2) { e2.printStackTrace(); } trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { //if (e.getClickCount() == 2)// 双击托盘窗口再现 //setExtendedState(Frame.NORMAL);// Frame窗口的状态 //Frame.ICONIFIED 窗口图标化状态 //Frame.MAXIMIZED_BOTH 最大化 //Frame.NORMAL //setVisible(true); setExtendedState(Frame.NORMAL); setVisible(true); } }); time = new JLabel("请输入提示信息:"); interval = new JLabel("提醒周期:"); msg = new JTextField(12); tf = new JTextField(10); cb = new JComboBox(in); cb.setSelectedIndex(2); button = new JButton("确定"); button.addActionListener(this); this.setTitle("定时提醒器"); this.setBounds(200, 150, 300, 250); this.setVisible(true); this.setLayout(new FlowLayout()); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter() { public void windowIconified(WindowEvent e) { dispose();// 窗口最小化时dispose该窗口 } }); this.add(time); this.add(msg); this.add(interval); this.add(tf); this.add(cb); this.add(button); tf.requestFocus(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { if (!tf.getText().equals("")) { int idx = cb.getSelectedIndex(); this.alertInterval = Integer.parseInt(tf.getText()) * this.milliSeconds[idx]; if (timer == null) { timer = new Timer(this.alertInterval, this); } else { timer.setDelay(this.alertInterval); } timer.start(); } } else if (e.getSource() == timer) { JOptionPane.showMessageDialog(this, msg.getText(), "叮呤呤!", JOptionPane.DEFAULT_OPTION); } } public static void main(String[] args) { new Alerter(); } } import java.awt.AWTException; import java.awt.Desktop; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; public class DesktopTray { private static Desktop desktop; private static SystemTray st; private static PopupMenu pm; public static void main(String[] args) { if (Desktop.isDesktopSupported()) { // 判断当前平台是否支持Desktop类 desktop = Desktop.getDesktop(); } if (SystemTray.isSupported()) {// 判断当前平台是否支持系统托盘 st = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("temp.jpg"); // 定义托盘图标的图片 createPopupMenu(); TrayIcon ti = new TrayIcon(image, "Desktop Demo Tray", pm); try { st.add(ti); } catch (AWTException ex) { ex.printStackTrace(); } } } public static void sendMail(String mail) { if (desktop != null && desktop.isSupported(Desktop.Action.MAIL)) { try { desktop.mail(new URI(mail)); } catch (IOException ex) { ex.printStackTrace(); } catch (URISyntaxException ex) { ex.printStackTrace(); } } } public static void openBrowser(String url) { if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { try { desktop.browse(new URI(url)); } catch (IOException ex) { ex.printStackTrace(); } catch (URISyntaxException ex) { ex.printStackTrace(); } } } public static void edit() { if (desktop != null && desktop.isSupported(Desktop.Action.EDIT)) { try { File txtFile = new File("test.txt"); if (!txtFile.exists()) { txtFile.createNewFile(); } desktop.edit(txtFile); } catch (IOException ex) { ex.printStackTrace(); } } } public static void createPopupMenu() { pm = new PopupMenu(); MenuItem openBrowser = new MenuItem("Open My Blog"); openBrowser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openBrowser("http://blog.csdn.net/chinajash"); } }); MenuItem sendMail = new MenuItem("Send Mail to me"); sendMail.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendMail("mailto:chinajash@yahoo.com.cn"); } }); MenuItem edit = new MenuItem("Edit Text File"); sendMail.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { edit(); } }); MenuItem exitMenu = new MenuItem("&Exit"); exitMenu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); pm.add(openBrowser); pm.add(sendMail); pm.add(edit); pm.addSeparator(); pm.add(exitMenu); } }