Java实现系统托盘以及菜单国际化,代码中标黄的地方,“是”“否”,这个国际化怎么整呢?...

import java.awt.AWTException;
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.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.apache.log4j.Logger;
import com.huawei.ams5090.nac.util.SystemGlobalCfg;

/**
 * 工具系统托盘展示
 * 
 * @version 1.0
 *
 */
public class SystemTrayFrame extends JFrame
{

    /**
     * 序列码
     *
     */
    private static final long serialVersionUID = -7887733949701809433L;

    /**
     * 托盘图标所在路径
     */
    private static final String TRAYPATH = SystemGlobalCfg.getSysRootPath()
                                           + File.separator
                                           + "lib"
                                           + File.separator
                                           + "nac.gif";

    /**
     * 中英文资源所在路径
     */
    private static final String LANGUAGEPATH = SystemGlobalCfg.getSysRootPath()
                                               + File.separator
                                               + "message.properties";

    private static final Logger logger = Logger.getLogger(SystemTrayFrame.class);

    /**
     * 默认编码格式
     */
    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * 托盘图标
     */
    private TrayIcon trayIcon = null;

    /**
     * 本操作系统托盘的实例
     */
    private SystemTray tray = null;

    /**
     * 系统托盘入口
     */
    public SystemTrayFrame()
    {
        try
        {
            // 将LookAndFeel设置成Windows样式
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Exception ex)
        {
            logger.warn(ex.toString());
        }
        // 如果操作系统支持托盘
        if (SystemTray.isSupported())
        {
            this.tray();
        }
    }

    /**
     * 启动系统托盘
     */
    private void tray()
    {
        // 获得本操作系统托盘的实例
        tray = SystemTray.getSystemTray();
        ImageIcon icon = new ImageIcon(TRAYPATH);

        Properties properties = this.getInformationFile(LANGUAGEPATH,
                                                        DEFAULT_ENCODING);
        // 托盘提示信息
        String systemTip = properties.getProperty("SYSTEM_TRAY_TIP");
       
        // 托盘菜单
        String systemTrayMenu = properties.getProperty("SYSTEM_TRAY_MENU");

        // 提示
        final String systemTrayPromf = properties.getProperty("SYSTEM_TRAY_PROMF");

        // 点击“退出”后,系统弹出的提示框信息
        final String systemTrayIdea = properties.getProperty("SYSTEM_TRAY_IDEA");
       
        // 构造一个右键弹出式菜单
        PopupMenu pop = new PopupMenu();
        MenuItem exit = new MenuItem(systemTrayMenu);

        // TrayIcon(Image image, String tooltip, PopupMenu popup)
        // 用“图标”,“ToolTip”,“弹出菜单”来构造一个托盘图标
        trayIcon = new TrayIcon(icon.getImage(), systemTip, pop);
        try
        {
            tray.add(trayIcon);
        }
        catch (AWTException e1)
        {
            logger.error("System Tray start errot:" + e1.toString(), e1);
        }

        // 点击“Exit”菜单后退出程序
        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    int selectID = JOptionPane.showConfirmDialog(null,
                                                                 systemTrayIdea,
                                                                 systemTrayPromf,
                                                                 JOptionPane.YES_NO_OPTION);
                    if (selectID == 0)
                    {
                        stopService();
                        tray.remove(trayIcon);
                        Runtime.getRuntime().exit(1);
                    }
                }
                catch (Exception e3)
                {
                    logger.error("Stop service error: " + e3.toString(), e3);
                }
            }
        });

        pop.add(exit);
    }

    /**
     * 读取配置文件信息
     *
     * @param path 路径
     * @param encoding 编码方式名
     * @return 配置对象
     */
    public Properties getInformationFile(String path, String encoding)
    {
        Properties property = new Properties();

        // 路径为空返回null
        if (null == path)
        {
            return property;
        }

        InputStreamReader isr = null;
        try
        {
            // 文件不存在返回null
            File file = new File(path);
            if (!file.exists())
            {
                return property;
            }

            // 读取文件
            isr = new InputStreamReader(new FileInputStream(path), encoding);
            property.load(isr);
            return property;
        }
        catch (Exception e)
        {
            logger.error("failed to load InputStreamReader", e);
            return property;
        }
        finally
        {
            closeStream(isr);
        }
    }

    /**
     * 关闭流
     *
     * @param closeable 可关闭流对象
     */
    public static void closeStream(Closeable closeable)
    {
        try
        {
            if (closeable != null)
            {
                closeable.close();
            }
        }
        catch (Exception e)
        {
            logger.error("Failed to close closeable", e);
        }
    }

    /**
     * 停止后台任务(mysql、tomcat)
     */
    private void stopService()
    {
        // 关闭后台数据库
        boolean mySqlStoped = MySQLAdmin.getInstance().stopService();
        String mysqlStopedMsg = mySqlStoped ? "Succeed to stop MySQL server." : "Failed to stop MySQL server.";

        // 关闭tomcat服务器
        boolean tomcatStoped = TomcatAdmin.getInstance().stopService();
        String tomcatStopedMsg = tomcatStoped ? "Succeed to stop Tomcat server." : "Failed to stop Tomcat server.";

        MessageBox.showMsgBox("INFO", mysqlStopedMsg);
        MessageBox.showMsgBox("INFO", tomcatStopedMsg);
    }

    /**
     * 移除托盘图标
     *
     */
    public void removeTray()
    {
        tray.remove(trayIcon);
    }

}

转载于:https://www.cnblogs.com/skay--001/archive/2013/01/30/2883026.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值