| | |
| | | |
| | | |
| | | import com.github.hunter0x7c7.sync.model.interfaces.Callback; |
| | | import com.github.hunter0x7c7.sync.model.storage.Session; |
| | | import javafx.application.Platform; |
| | | import javafx.stage.Stage; |
| | | |
| | | import java.util.List; |
| | | |
| | | import javax.swing.*; |
| | | import java.awt.*; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | |
| | | import static com.github.hunter0x7c7.sync.model.global.Parameters.APPID; |
| | | import static com.github.hunter0x7c7.sync.model.global.Parameters.AppNameString; |
| | | |
| | | public class TrayUtil { |
| | | |
| | |
| | | Image img = imageIcon.getImage(); |
| | | |
| | | |
| | | TrayIcon trayIcon = new TrayIcon(img, tooltip, popup); |
| | | //设置图标尺寸自动适应 |
| | | trayIcon.setImageAutoSize(true); |
| | | final TrayIcon trayIcon = new TrayIcon(img, tooltip, popup); |
| | | //trayIcon.setImageAutoSize(true);//设置图标尺寸自动适应 |
| | | trayIcon.setActionCommand(APPID); |
| | | trayIcon.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(java.awt.event.ActionEvent e) { |
| | | public void actionPerformed(ActionEvent e) { |
| | | //鼠标双击系统托盘图标 |
| | | Platform.runLater(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | if (stage != null) { |
| | | stage.show(); |
| | | if (stage.isIconified()) {//最小化 |
| | | stage.setIconified(false); |
| | | } |
| | | if (!stage.isShowing()) { |
| | | stage.show(); |
| | | } |
| | | stage.toFront(); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | }); |
| | | //系统托盘 |
| | | SystemTray tray = SystemTray.getSystemTray(); |
| | | final SystemTray tray = SystemTray.getSystemTray(); |
| | | Platform.runLater(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | if (tray != null) { |
| | | tray.remove(trayIcon); |
| | | } |
| | | //退出 |
| | | Platform.exit(); |
| | | exitApp(); |
| | | }); |
| | | //点击关闭按钮时隐藏场景 |
| | | stage.setOnCloseRequest(event -> { |
| | |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | |
| | | //退出 |
| | | public void exitApp() { |
| | | //UI线程 |
| | | Platform.runLater(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | //退出前先关闭窗口 |
| | | Stage stage = Session.getInstance().getPrimaryStage(); |
| | | if (stage != null) { |
| | | stage.close(); |
| | | } |
| | | //退出 |
| | | Platform.exit(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |