当前位置: 首页 > news >正文

JavaFX 第三篇 HostServices和Platform

1、HostServices类

介绍这个类主要是使用里面的一个方法

返回类型方法说明
voidshowDocument(java.lang.String uri)使用默认浏览器打开一个url地址
/*** @description: 程序打开3秒后,打开百度* @author: HK* @since: 2025/4/24 16:40*/
public class Demo1 extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws Exception {// 设置窗体标题primaryStage.setTitle("测试HostServices");// 设置窗体大小primaryStage.setWidth(400);primaryStage.setHeight(400);// 窗体显示primaryStage.show();Thread.sleep(3000);// 获取HostServicesHostServices hostServices = getHostServices();// 打开网页hostServices.showDocument("www.baidu.com");}
}

2、Platform类

(1)runLater方法

        runLater(java.lang.Runnable runnable):在JavaFX Application线程空闲时运行,他不会单独开启一个线程,他和application是同一个线程,可以做一些简单的页面刷新等操作。

/*** @description: Platform类方法介绍* @author: HK* @since: 2025/4/24 16:40*/
public class Demo2 extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws Exception {Platform.runLater(() -> {System.out.println("Platform中run方法的线程名称:" + Thread.currentThread().getName());});System.out.println("Application中start方法的线程名称:" + Thread.currentThread().getName());}
}

(2)setImplicitExit方法

setImplicitExit(boolean implicitExit):设置Fx窗体关闭后,后台程序时候也进行关闭

true:表示窗体关闭,程序也会关闭

false:表示窗体关闭,程序不会关闭

/*** @description: Platform类方法介绍* @author: HK* @since: 2025/4/24 16:40*/
public class Demo3 extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws Exception {Platform.setImplicitExit(false);// 设置窗体标题primaryStage.setTitle("测试setImplicitExit方法");// 设置窗体大小primaryStage.setWidth(400);primaryStage.setHeight(400);// 窗体显示primaryStage.show();}
}

(3)exit方法

exit():退出程序
/*** @description: Platform类方法介绍* @author: HK* @since: 2025/4/24 16:40*/
public class Demo3 extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws Exception {// 设置窗体标题primaryStage.setTitle("测试setImplicitExit方法");// 设置窗体大小primaryStage.setWidth(400);primaryStage.setHeight(400);// 窗体显示primaryStage.show();// 3秒后程序自动关闭Thread.sleep(3000);Platform.exit();}
}

(4)isSupported方法

isSupported(ConditionalFeature   feature):查询平台是否支持指定的条件特性

ConditionalFeature.SCENE3D:3D效果

public class Demo3 extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws Exception {// 设置窗体标题primaryStage.setTitle("测试setImplicitExit方法");// 设置窗体大小primaryStage.setWidth(400);primaryStage.setHeight(400);// 窗体显示primaryStage.show();// 查看平台是否支持3DSystem.out.println(Platform.isSupported(ConditionalFeature.SCENE3D));}
}

常用的方法runLater和exit

http://www.xdnf.cn/news/129151.html

相关文章:

  • 交通运输行业综合智慧监管平台:商贸物流的安全与效率引擎
  • Windows下QT打包后程序运行后弹出CMD命令窗口的问题解决方法
  • vxe-table封装表头
  • MSHFlexGrid 控件网格赋值指南方案
  • Python爬虫实战:移动端逆向工具Fiddler经典案例
  • 24FIC 决赛 计算机部分
  • 【HFP】蓝牙语音通话控制深度解析:来电拒接与通话终止协议
  • 【前端】【业务场景】【面试】在前端开发中,如何优化 SVG(可缩放矢量图形)的性能,特别是在处理复杂图形和动画时
  • MCP实战-本地MCP Server+Cursor实践
  • AI日报 - 2025年04月25日
  • C语言-函数-1
  • 入门-C编程基础部分:19、输入 输出
  • 位带和位带别名区
  • python自动化学习六:断言
  • 【Linux系统篇】:什么是信号以及信号是如何产生的---从基础到应用的全面解析
  • redis相关问题整理
  • 2024年ASOC SCI1区TOP:改进灰狼算法IGWO+股票指数收益预测,深度解析+性能实测
  • 传感器测量(图片流程)
  • ECharts学习之 toolbox 工具栏
  • 大模型数据预处理方法总结
  • 旋转磁体产生的场-对导航姿态的影响
  • 数据库--回表
  • Spring Security基础入门
  • 电子处方模块开发避坑指南:从互联网医院系统源码实践出发
  • vuex持久化vuex-persistedstate,存储的数据刷新页面后导致数据丢失
  • 【解决】Android Gradle Sync 报错 Could not read workspace metadata
  • 【C/C++】深入理解指针(四)
  • MuJoCo 机械臂 PPO 强化学习逆向运动学(IK)
  • 工会考试精选题目及答案分享
  • 2025年3月电子学会青少年机器人技术(六级)等级考试试卷-实际操作