以下是自己使用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(); } }