java 实现关机工具,托盘图标

以下是自己使用swing编写的windows关机工具,实现了托盘图标。


package com.gpengtao;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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 java.io.IOException;
import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CloseComputer extends JFrame implements ActionListener {

	/**
	 * @author gpengtao
	 */
	private static final long serialVersionUID = 1L;
	private JButton countDown;
	private JButton timeDown;
	private JButton cancel;
	private JLabel label;
	private JPanel panelNorth;
	private JPanel panelSouth;
	private TrayIcon trayIcon;
	private JFrame frame;

	/**
	 * 构造方法 设置各种组建
	 */
	public CloseComputer() {
		super("关机工具");
		frame = this;
		countDown = new JButton("倒计时关机");
		timeDown = new JButton("定时关机");
		cancel = new JButton("取消关机");
		label = new JLabel("请选择关机方式");

		panelNorth = new JPanel();
		panelNorth.add(label);

		panelSouth = new JPanel();
		panelSouth.add(countDown);
		panelSouth.add(timeDown);
		panelSouth.add(cancel);

		countDown.addActionListener(this);
		timeDown.addActionListener(this);
		cancel.addActionListener(this);

		// ImageIcon(CloseComputer.class.getResource("/tray.png"));
		ImageIcon titleIcon = new ImageIcon("tray.png");
		this.setIconImage(titleIcon.getImage());
		this.add(panelNorth, BorderLayout.NORTH);
		this.add(panelSouth, BorderLayout.SOUTH);
		this.setResizable(false);
		this.setVisible(true);
		this.setSize(380, 125);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * 设置可以最小化
		 */
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowIconified(WindowEvent e) {
				setVisible(false);
				setExtendedState(JFrame.ICONIFIED);
				dispose();
			}
		});

		/**
		 * 设置托盘图标
		 */
		if (SystemTray.isSupported()) {
			PopupMenu popupMenu = new PopupMenu();
			MenuItem item1 = new MenuItem("exit");
			MenuItem item2 = new MenuItem("open");
			popupMenu.add(item1);
			popupMenu.add(item2);
			item1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			item2.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					setVisible(true);
					setExtendedState(JFrame.NORMAL);
				}
			});

			// ImageIcon(CloseComputer.class.getResource("/tray.png"));
			ImageIcon trayImg = new ImageIcon("tray.png");
			trayIcon = new TrayIcon(trayImg.getImage(), "关机工具", popupMenu);
			SystemTray sysTray = SystemTray.getSystemTray();
			try {
				sysTray.add(trayIcon);
			} catch (AWTException e) {
				e.printStackTrace();
			}
			trayIcon.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					if (e.getClickCount() == 2) {
						setVisible(true);
						setExtendedState(JFrame.NORMAL);
					}
				}
			});
		}
	}//构造函数

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == countDown) {
			CountDown();
		} else if (e.getSource() == timeDown) {
			TimeDown();
		} else if (e.getSource() == cancel) {
			CancelDown();
		}
	}
	
	/**
	 * 倒计时关机实现
	 */
	void CountDown() {
		String time = JOptionPane.showInputDialog(this, "请输入倒计时时间 (秒):", null);
		try {
			Runtime.getRuntime().exec("shutdown -s -t " + time);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 定时关机实现
	 */
	void TimeDown() {
		Dialog dialog = new Dialog(frame);
		int hour = dialog.getHour();
		int minute = dialog.getMinute();
		int second = dialog.getSecond();
		Calendar time = Calendar.getInstance();
		int nowHour = time.get(Calendar.HOUR);
		int nowMinute = time.get(Calendar.MINUTE);
		int nowSecond = time.get(Calendar.SECOND);
		int distance = hour * 60 * 60 + minute * 60 + second - nowHour * 60 * 60 - nowMinute * 60 - nowSecond;
		if (distance < 0) {
			JOptionPane.showMessageDialog(this, "时间输入有误  ╯﹏╰ ", "ERROR", JOptionPane.WARNING_MESSAGE);
			return;
		}
		try {
			Runtime.getRuntime().exec("shutdown -s -t " + distance);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 取消关机实现
	 */
	void CancelDown() {
		JOptionPane.showMessageDialog(this, "您已经取消关机任务!");
		try {
			Runtime.getRuntime().exec("shutdown -a");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Dilog内部类 
	 * @author gpengtao
	 * 定时关机时弹出的对话框,并获得数据
	 */
	class Dialog extends JDialog {
		private static final long serialVersionUID = 1L;
		private JTextField textHour;
		private JTextField textMinute;
		private JTextField textSecond;

		public Dialog(JFrame frame) {
			super(frame, "输入关机时间", true);

			textHour = new JTextField(3);
			textMinute = new JTextField(3);
			textSecond = new JTextField(3);

			this.setLayout(new FlowLayout());
			this.add(textHour);
			this.add(new JLabel("时"));
			this.add(textMinute);
			this.add(new JLabel("分"));
			this.add(textSecond);
			this.add(new JLabel("秒"));

			this.setSize(300, 80);
			this.setLocationRelativeTo(frame);
			this.setVisible(true);

		}

		public int getHour() {
			return Integer.parseInt(textHour.getText());
		}

		public int getMinute() {
			return Integer.parseInt(textMinute.getText());
		}

		public int getSecond() {
			return Integer.parseInt(textSecond.getText());
		}
	}//Dilog内部类
	
	//自己的测试
	public static void main(String[] args) {
		new CloseComputer();
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值