java swing 最小化到托盘_如何最小化靠近系统托盘的javafx阶段?

本文介绍了如何在JavaFX应用中实现窗口最小化到系统托盘的功能。通过设置Platform.setImplicitExit(false)防止程序退出,并在关闭事件中添加逻辑。当SystemTray支持时,创建TrayIcon并添加菜单项,最后将TrayIcon添加到系统托盘。同时展示了如何从AWT事件中调用JavaFX的方法。
摘要由CSDN通过智能技术生成

坐落在启动方法下列财产

Platform.setImplicitExit(false);

然后设置上紧密事件

secondaryStage.setOnCloseRequest(event -> {

// Your code here

});

为了使ystem托盘尝试以下代码:

//Check the SystemTray is supported

if (!SystemTray.isSupported()) {

System.out.println("SystemTray is not supported");

return;

}

final PopupMenu popup = new PopupMenu();

URL url = System.class.getResource("/images/new.png");

Image image = Toolkit.getDefaultToolkit().getImage(url);

final TrayIcon trayIcon = new TrayIcon(image);

final SystemTray tray = SystemTray.getSystemTray();

// Create a pop-up menu components

MenuItem aboutItem = new MenuItem("About");

CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");

CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");

Menu displayMenu = new Menu("Display");

MenuItem errorItem = new MenuItem("Error");

MenuItem warningItem = new MenuItem("Warning");

MenuItem infoItem = new MenuItem("Info");

MenuItem noneItem = new MenuItem("None");

MenuItem exitItem = new MenuItem("Exit");

//Add components to pop-up menu

popup.add(aboutItem);

popup.addSeparator();

popup.add(cb1);

popup.add(cb2);

popup.addSeparator();

popup.add(displayMenu);

displayMenu.add(errorItem);

displayMenu.add(warningItem);

displayMenu.add(infoItem);

displayMenu.add(noneItem);

popup.add(exitItem);

trayIcon.setPopupMenu(popup);

try {

tray.add(trayIcon);

} catch (AWTException e) {

System.out.println("TrayIcon could not be added.");

}

实施例的系统托盘图像:

baca33251e690f43bfe143a9ab43f8ef.png

要从AWT事件处理程序调用的JavaFX的方法可能会follw以下方式:

yourAwtObject.addActionListener(e -> {

Platform.runLater(() -> primaryStage.show());

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值