java系统托盘

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SystemTrayTest extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Image image;// 托盘图标
	private TrayIcon trayIcon; 
	private SystemTray systemTray;// 系统托盘
	//Image→TrayIcon→ SystemTray
	int alertInterval = 0;
	final String[] in = { "周", "日", "时", "分", "秒" };
	final int ONEDAY_MILLISECONDS = 24 * 3600 * 1000;
	final int[] milliSeconds = { 7 * this.ONEDAY_MILLISECONDS,
			this.ONEDAY_MILLISECONDS, 3600 * 1000, 60 * 1000, 1000 };

	Timer timer;
	JLabel time;
	JLabel interval;
	JTextField msg;
	JTextField tf;
	JComboBox cb;
	JButton button;
	//构造函数
	SystemTrayTest() {
		systemTray = SystemTray.getSystemTray();// 获得系统托盘的实例
		try {
			image = Toolkit.getDefaultToolkit().getImage(("img\\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);// 状态
				
				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 SystemTrayTest();
	}

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值