系统托盘的修改

package cn.wuwenfu.swing;

import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Font;
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.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import java.io.IOException;
import java.util.Map;




import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;
import javax.swing.JTextArea;


/*
 * 2014年2月27日 09:04:47
 * 系统托盘要一开始就存在
 * */
public class SdsSwing extends JFrame {
    private JPanel infoJP;
    private JPanel textJP;
    private JPanel buttJP;
    private TrayIcon trayIcon;

    public SdsSwing() {
        super();
        this.setSize(500, 400);

        this.setResizable(false);
        this.getContentPane().setLayout(null);
        this.add(getInfoJP());
        this.add(getTextJP());

        this.add(getButtJP());
        this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);

        this.setTitle("SDS");
        
        
        Image img = Toolkit.getDefaultToolkit().getImage("./config/1.jpg");
        this.setIconImage(img);

        this.setLocationRelativeTo(null);//窗口在显示器中间
        
        windowTray();

    }

    /* 瑞德医疗官网,工作日志 */
    private JPanel getInfoJP() {
        if (infoJP == null) {
            
            infoJP = new JPanel();
            infoJP.setBounds(0, 0, 500, 50);
            // infoJP.setSize(500, 200);
            infoJP.setLayout(new FlowLayout());
            JButton ryzurNet = new JButton("瑞德官网");
            infoJP.add(ryzurNet);
            ryzurNet.setMnemonic(KeyEvent.VK_I);

            ryzurNet.addActionListener(new RyzurInternet());

            JButton ryzurLog = new JButton("查看日志");
            infoJP.add(ryzurLog);

            ryzurLog.setMnemonic(KeyEvent.VK_I);

            ryzurLog.addActionListener(new ReadLog());
            
            JButton ryzurPro = new JButton("配置参数");
            infoJP.add(ryzurPro);

            ryzurPro.setMnemonic(KeyEvent.VK_I);

            ryzurPro.addActionListener(new ReadPro());


        }
        return infoJP;
    }

    /* 软件介绍 */
    private JPanel getTextJP() {
        if (textJP == null) {
            textJP = new JPanel();
            // textJP.setSize(500, 200);
            textJP.setBounds(0, 60, 500, 200);
            textJP.setLayout(new FlowLayout());

            JTextArea jtextArea = new JTextArea(45, 40);
            jtextArea.setLineWrap(true);

            // jtextArea.setTabSize(50);
            jtextArea.setFont(new Font("标楷体", Font.BOLD, 12));
            jtextArea.setLineWrap(true);// 激活自动换行功能
            jtextArea.setWrapStyleWord(true);// 激活断行不断字功能
            jtextArea
                    .append("SdsServer,全称SDS自动数据平台服务器端,是安徽瑞德医疗器械有限公司处理终端设备的绿色软件");
            jtextArea.append("
");
            jtextArea.append("程序的功能");
            jtextArea.append("
");
            jtextArea.append("1、接受来自客户端上xml文件,解析到mysql数据库中");
            jtextArea.append("
");
            jtextArea.append("2、支持多线程,分步解析,避免内存溢出");
            jtextArea.append("
");
            jtextArea.append("3、支持客户端的自动升级");
            jtextArea.append("
");
            jtextArea.append("注意事项:");
            jtextArea.append("
");
            jtextArea.append("1、配置文件中的参数必不可少,修改时注意大小写与默认的配置的要一致");
            jtextArea.append("
");
            jtextArea.append("2、默认通信端口为8011,启动时,请确保默认端口或配置的端口没有被占用");
            jtextArea.append("
");

            jtextArea.append("3、如果修改了配置文件,程序需要重新启动");

            textJP.add(jtextArea);
        }

        return textJP;
    }

    /* 开启,关闭 */
    private JPanel getButtJP() {
        if (buttJP == null) {
            buttJP = new JPanel();
            // buttJP.setSize(500, 200);
            buttJP.setBounds(0, 300, 500, 100);
            buttJP.setLayout(new FlowLayout());
            JButton startB = new JButton("开启");
            
            //这里要判断是否服务已经开启
            JButton closeB = new JButton("关闭");
            
            if(CheckServer.isStart()){
                startB.setEnabled(false);
                
            }else{
                closeB.setEnabled(false);
                
            }
            

            buttJP.add(startB);
            startB.setMnemonic(KeyEvent.VK_I);
            startB.addActionListener(new StartServer(startB, closeB));

            buttJP.add(closeB);
            closeB.setMnemonic(KeyEvent.VK_I);
            closeB.addActionListener(new CloseServer(startB, closeB));

        }
        return buttJP;
    }

    public static void main(String[] args) {
        
        //根据配置文件
        Map m =LoadPro.loadSdsFile("./config/sds.properties");
        if(Boolean.valueOf(m.get("isauto").toString())){
            //执行server
            // 执行bat命令,启动服务程序
            Runtime run = Runtime.getRuntime();
            try {
                run.exec("cmd /c  .\config\start.bat");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                JOptionPane.showMessageDialog(null, "启动服务时发生错误");
                
            }

            
        }
        
        SdsSwing w = new SdsSwing();
        w.setVisible(true);
    }

    protected void processWindowEvent(WindowEvent e) {

        // 窗口关闭
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            
            
            
            //关闭时提示
            int i =JOptionPane.showConfirmDialog(null, "确定要退出程序吗?", "确认关闭", JOptionPane.YES_OPTION);

            
            //如果不为0,表明点击的是关闭和取消操作,因此停止关闭的逻辑
            if(i!=0){
                return;
            }

            
            

            // 退出之前先关闭服务
            // 执行bat命令,启动服务程序
            Runtime run = Runtime.getRuntime();

            try {
                run.exec("taskkill /im SdsServer.exe /f /t");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            System.exit(0);
        }

        // 窗口最小化
        if (e.getID() == WindowEvent.WINDOW_ICONIFIED) {
            
            this.setVisible(false);
            

        }

        super.processWindowEvent(e); // 该语句会执行窗口事件的默认动作(如:隐藏)

    }

    //执行最小化时的逻辑
    private void windowTray() {

        // TODO Auto-generated method stub
        // 窗口最小化时执行
        this.setVisible(false);
        //TrayIcon trayIcon = null;
        SystemTray tray = null;
        if (SystemTray.isSupported()) // 判断系统是否支持系统托盘
        {
            tray = SystemTray.getSystemTray(); // 创建系统托盘
            
            
            Image image = Toolkit.getDefaultToolkit().getImage("./config/1.jpg");// 载入图片,这里要写你的图标路径哦

            // 创建弹出菜单
            PopupMenu popup = new PopupMenu();
            // 主界面选项
            MenuItem mainFrameItem = new MenuItem("主界面");
            // 退出程序选项
            MenuItem exitItem = new MenuItem("退出程序");

            trayIcon = new TrayIcon(image, "SdS", popup);// 创建trayIcon
            trayIcon.setImageAutoSize(true);//自动设置图片大小

            // 主界面执行的事件,回复窗口
            ActionListener listener = new ShowJF(this);

            mainFrameItem.addActionListener(listener);

            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (JOptionPane.showConfirmDialog(null, "确定退出系统") == 0) {
                        
                        Runtime run = Runtime.getRuntime();

                        try {
                            run.exec("taskkill /im SdsServer.exe /f /t");
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        System.exit(0);
                    }
                }
            });

            popup.add(mainFrameItem);
            popup.add(exitItem);

            trayIcon.addActionListener(listener);
            try {
                tray.add(trayIcon);
            } catch (AWTException e1) {
                e1.printStackTrace();
            }
        }

    }

}
修改后的代码

主要修改的地方:

windowTray(); 放在构造函数中。这样程序一开始就会初始化托盘

窗口最小化时,仅仅执行隐藏逻辑。

双击托盘时,不执行移除系统托盘逻辑

QQ:540045865 热爱生活,热爱编程。
原文地址:https://www.cnblogs.com/jsRunner/p/3570840.html