自己刚学JavaFx,有问题欢迎指出讨论!希望帮到了大家!
这里添加一些美化、完善前面应用的功能。
一、添加CSS
当不指定CSS,会添加默认的CSS,而如果直接使用
Application.setUserAgentStylesheet(getClass().getResource("sample.css")
.toExternalForm());```
则会直接覆盖掉默认的CSS,默认的看起来还可以 ( ̄▽ ̄)"
所以用下面这种方法添加CSS,能够直接添加想要的属性比如隐藏滑动条。
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("sample.css").toExternalForm());
primaryStage.setScene(scene);
.list-view .scroll-bar:vertical{
-fx-pref-width: 0;
-fx-pref-height: 0;
}
.list-view .scroll-bar:horizontal{
-fx-pref-width: 0;
-fx-pref-height: 0;
}
二、添加托盘图标
参考文章:
https://blog.csdn.net/weixin_44512194/article/details/93800549
// 系统托盘
SystemTray tray = SystemTray.getSystemTray();
//此处不能选择ico格式的图片,要使用16*16的png格式的图片
BufferedImage image = ImageIO.read(new File("my.png"));
assert image != null;
TrayIcon trayIcon = new TrayIcon(image, "MyUtil");
tray.add(trayIcon);
trayIcon.addMouseListener(new MouseAdapter() {
// 鼠标事件
public void mouseClicked(MouseEvent e) {
// 鼠标右键关闭
if(e.getButton() == 3){
Platform.exit();
System.exit(0); // 必杀退出法
return;
}
// 判断是否双击了鼠标
if (e.getClickCount() == 2) {
Platform.setImplicitExit(false); //多次使用显示和隐藏设置false
if (primaryStage.isShowing()) {
Platform.runLater(primaryStage::hide);
} else {
Platform.runLater(primaryStage::show);
}
}
}
});
三、JIntellitype 方法全局监听
java运行在虚拟机上,JavaFx没有提供全局监听,使用JIntellitype可以完成!
今天发现了TIM打开后,JIntellitype会报错,个人认为是TIM的后台程序会检查全局监听,或者全局监听冲突,关闭TIM,并把后台的Q盾什么的杀掉就可以正确运行了。
// 全局按键监听
int GLOBAL_HOT_KEY_1 = 0;
int GLOBAL_HOT_KEY_2 = 1;
JIntellitype.getInstance().registerHotKey(GLOBAL_HOT_KEY_1, JIntellitype.MOD_ALT, (int) 'Q');
JIntellitype.getInstance().registerHotKey(GLOBAL_HOT_KEY_2, JIntellitype.MOD_ALT, (int) 'Z');
JIntellitype.getInstance().addHotKeyListener(markCode -> {
if (markCode == GLOBAL_HOT_KEY_1) {
Platform.setImplicitExit(false); //多次使用显示和隐藏设置false
if (primaryStage.isShowing()) {
Platform.runLater(primaryStage::hide);
} else {
Platform.runLater(primaryStage::show);
}
} else if (markCode == GLOBAL_HOT_KEY_2) {
Platform.exit();
System.exit(0); // 必杀退出法
}
});
有问题欢迎留言评论!