添加系统托盘 2023年3月11日10:52:46

master
zhangmeng 2023-03-11 10:52:58 +08:00
parent 5767ae2a34
commit 387252e3d0
12 changed files with 178 additions and 2 deletions

View File

@ -149,6 +149,12 @@
| boot-security |https://gitee.com/zhang.w/boot-security|
| 阿里图标库 |https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.3|
## 3. 添加系统托盘
> 启动图片来自网络收集!(特此说明)
![](./src/main/resources/static/redame/img_30.png)
**声明**
> 本项目纯属个人兴趣开发,仅供学习使用,切勿商用!如发生商用的一切后果自行承担! 十分感谢这些开源项目与组件!

View File

@ -26,6 +26,7 @@
package com.zhangmeng.tools;
import com.zhangmeng.tools.views.HomeView;
import com.zhangmeng.tools.views.LoadView;
import javafx.application.Application;
/**
@ -36,7 +37,7 @@ import javafx.application.Application;
public class JavaFxToolsApplication {
public static void main(String[] args) {
Application.launch(HomeView.class);
Application.launch(LoadView.class);
}
}

View File

@ -0,0 +1,24 @@
package com.zhangmeng.tools.controller;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import lombok.extern.slf4j.Slf4j;
/**
* @author :
* @version : 1.0
* @date : 2023-03-11 09:23
*/
@Slf4j
public class LoadStartController {
@FXML
private Label load_info_show;
@FXML
public void initialize() {
}
}

View File

@ -132,7 +132,7 @@ public class ImagePath {
/**
*
*/
public static String TRAY_ICON = "icon_tray/trayicon.png";
public static String TRAY_ICON = "icon_tray/tools-fx-3.png";
/**

View File

@ -28,11 +28,18 @@ package com.zhangmeng.tools.views;
import com.zhangmeng.tools.utils.ImagePath;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author :
@ -50,5 +57,42 @@ public class HomeView extends Application {
primaryStage.setScene(scene);
primaryStage.setTitle("java-fx 工具");
primaryStage.show();
primaryStage.setOnCloseRequest(event -> {
Platform.setImplicitExit(true);
try {
SystemTray systemTray = SystemTray.getSystemTray();
java.awt.Image image1 = Toolkit.getDefaultToolkit().getImage(this.getClass().getClassLoader().getResource(ImagePath.path(ImagePath.ImagePathType.TRAY_ICON)));
String title = "java-fx 系统托盘";
PopupMenu menu = new PopupMenu();
MenuItem item1 = new MenuItem("显示");
MenuItem item2 = new MenuItem("退出");
menu.add(item1);
menu.add(item2);
TrayIcon trayIcon = new TrayIcon(image1, title, menu);
systemTray.add(trayIcon);
item1.addActionListener(e -> {
Platform.runLater(primaryStage::show);
systemTray.remove(trayIcon);
});
item2.addActionListener(e -> {
Platform.setImplicitExit(true);
Platform.runLater(primaryStage::close);
systemTray.remove(trayIcon);
Platform.exit();
});
menu.addActionListener(e -> {
Platform.runLater(primaryStage::show);
systemTray.remove(trayIcon);
});
Platform.setImplicitExit(false);
primaryStage.hide();
} catch (AWTException e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,79 @@
package com.zhangmeng.tools.views;
import com.zhangmeng.tools.utils.ImagePath;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import lombok.extern.slf4j.Slf4j;
/**
* @author :
* @version : 1.0
* @date : 2023-03-11 09:33
*/
@Slf4j
public class LoadView extends Application {
public Label load_info_show;
@Override
public void start(Stage primaryStage) throws Exception {
AnchorPane fx = FXMLLoader.load(ResourcesUtils.getResource("load-start"));
ImageView bg_iv = (ImageView) fx.lookup("#bg_iv");
load_info_show = (Label) fx.lookup("#load_info_show");
Scene scene = new Scene(fx);
Image image = new Image(ImagePath.path(ImagePath.ImagePathType.Tools_ICON));
primaryStage.getIcons().add(image);
primaryStage.setScene(scene);
primaryStage.setWidth(bg_iv.getFitWidth());
primaryStage.setHeight(bg_iv.getFitHeight());
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setFullScreenExitHint("");
primaryStage.show();
// 核心代码
new Thread(() -> {
initSystem();// 1
Platform.runLater(() -> {// 2
try {
log.info("加载页面....");
HomeView home = new HomeView();
home.start(new Stage());
primaryStage.close();
} catch (Throwable e) {
e.printStackTrace();// 3
}
});
}).start();
}
// 初始化系统
private void initSystem() {
try {
showInfo("初始化目录...");
Thread.sleep(1500);
showInfo("初始化系统配置...");
Thread.sleep(1500);
showInfo("版本检测...");
Thread.sleep(1500);
} catch (Throwable e) {
e.printStackTrace();
}
}
// 显示信息
public void showInfo(String info) {
Platform.runLater(() -> load_info_show.setText(info));
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="649.0" prefWidth="1156.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.LoadStartController">
<children>
<ImageView fx:id="bg_iv" id="bg_iv" fitHeight="649.0" fitWidth="1156.0" layoutX="1.0" pickOnBounds="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<image>
<Image url="@../static/bgimg/2041075.jpg" />
</image>
</ImageView>
<Label fx:id="load_info_show" layoutX="1082.0" layoutY="618.0" text="加载中......" textFill="#26852f" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="43.0">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 468 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 609 KiB