java systemtray 乱码_JavaFx2 实现系统托盘SystemTray

该博客介绍了如何在JavaFX应用程序中实现系统托盘功能,并解决了菜单项中文乱码的问题。通过设置-Dfile.encoding参数来解决IDEA和Eclipse中的乱码问题,使用 TrayIcon 类创建系统托盘图标,添加弹出菜单和监听事件,实现点击托盘图标显示和退出应用的功能。
摘要由CSDN通过智能技术生成

packagecom.ceadeal.javafxboot.ctrl;importjava.awt.Image;importjava.awt.MenuItem;importjava.awt.PopupMenu;importjava.awt.SystemTray;importjava.awt.Toolkit;importjava.awt.TrayIcon;importjava.awt.event.ActionListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.net.URL;importjavafx.application.Platform;importjavafx.stage.Stage;importlombok.extern.slf4j.Slf4j;/*** 自定义系统托盘(单例模式)

**/@Slf4jpublic classMySystemTray {private staticMySystemTray instance;private staticMenuItem showItem;private staticMenuItem exitItem;private staticTrayIcon trayIcon;private staticActionListener showListener;private staticActionListener exitListener;private staticMouseListener mouseListener;//右小角,最小化.//菜单项(打开)中文乱码的问题是编译器的锅,如果使用IDEA,需要在Run-Edit Configuration在LoginApplication中的VM Options中添加-Dfile.encoding=GBK//如果使用Eclipse,需要右键Run as-选择Run Configuration,在第二栏Arguments选项中的VM Options中添加-Dfile.encoding=GBK//打包成exe安装后打开不会乱码

static{//执行stage.close()方法,窗口不直接退出

Platform.setImplicitExit(false);//菜单项(打开)中文乱码的问题是编译器的锅,如果使用IDEA,需要在Run-Edit Configuration在LoginApplication中的VM Options中添加-Dfile.encoding=GBK//如果使用Eclipse,需要右键Run as-选择Run Configuration,在第二栏Arguments选项中的VM Options中添加-Dfile.encoding=GBK

showItem = new MenuItem("打开");//菜单项(退出)

exitItem = new MenuItem("退出");//此处不能选择ico格式的图片,要使用16*16的png格式的图片

URL url = MySystemTray.class.getResource("/icon/1.png");

Image image=Toolkit.getDefaultToolkit().getImage(url);//系统托盘图标

trayIcon = newTrayIcon(image);//初始化监听事件(空)

showListener = e -> Platform.runLater(() ->{});

exitListener= e ->{};

mouseListener= newMouseAdapter() {};

}public staticMySystemTray getInstance(Stage stage){if(instance == null){

instance= newMySystemTray(stage);

}returninstance;

}privateMySystemTray(Stage stage){try{//检查系统是否支持托盘

if (!SystemTray.isSupported()) {//系统托盘不支持

log.info(Thread.currentThread().getStackTrace()[ 1 ].getClassName() + ":系统托盘不支持");return;

}//设置图标尺寸自动适应

trayIcon.setImageAutoSize(true);//系统托盘

SystemTray tray =SystemTray.getSystemTray();//弹出式菜单组件

final PopupMenu popup = newPopupMenu();

popup.add(showItem);

popup.add(exitItem);

trayIcon.setPopupMenu(popup);//鼠标移到系统托盘,会显示提示文本

trayIcon.setToolTip("CMS");

listen(stage);

tray.add(trayIcon);

}catch(Exception e) {//系统托盘添加失败

log.error(Thread.currentThread().getStackTrace()[ 1 ].getClassName() + ":系统添加失败", e);

}

}/*** 更改系统托盘所监听的Stage*/

public voidlisten(Stage stage){//防止报空指针异常

if(showListener == null || exitListener == null || mouseListener == null || showItem == null || exitItem == null || trayIcon == null){return;

}//移除原来的事件

showItem.removeActionListener(showListener);

exitItem.removeActionListener(exitListener);

trayIcon.removeMouseListener(mouseListener);//行为事件: 点击"打开"按钮,显示窗口

showListener = e -> Platform.runLater(() ->showStage(stage));//行为事件: 点击"退出"按钮, 就退出系统

exitListener = e ->{

System.exit(0);

};//鼠标行为事件: 单机显示stage

mouseListener = newMouseAdapter() {

@Overridepublic voidmouseClicked(MouseEvent e) {//鼠标左键

if (e.getButton() ==MouseEvent.BUTTON1) {

showStage(stage);

}

}

};//给菜单项添加事件

showItem.addActionListener(showListener);

exitItem.addActionListener(exitListener);//给系统托盘添加鼠标响应事件

trayIcon.addMouseListener(mouseListener);

}/*** 关闭窗口*/

public voidhide(Stage stage){

Platform.runLater(()->{//如果支持系统托盘,就隐藏到托盘,不支持就直接退出

if(SystemTray.isSupported()) {//stage.hide()与stage.close()等价

stage.hide();

}else{

System.exit(0);

}

});

}/*** 点击系统托盘,显示界面(并且显示在最前面,将最小化的状态设为false)*/

private voidshowStage(Stage stage){//点击系统托盘,

Platform.runLater(() ->{if(stage.isIconified()){ stage.setIconified(false);}if(!stage.isShowing()){ stage.show(); }

stage.toFront();

});

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值