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

【javaFX基础】 “Not on FX application thread; currentThread = Thread-3“问题解决方法

一、问题描述

运行以下代码时:

 button.setOnAction(event->{//新建线程,避免画面卡顿Thread thread =new Thread(()->{String newValue ="测试语句";label.setText(newValue);//更新UI});thread.start();});


出现以下提示:非法操作,要更新控件需要在FX application 线程里,其他线程不允许!

Exception in thread "Thread-3" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-3

二、解决方法

利用javaFX中  Platform的静态方法runLater,将更新UI的代码放在里面。原理:runLater将任务放在队列里,当线程空闲时将执行队列里的任务,相当于在application 线程里执行。

button.setOnAction(event->{Thread thread =new Thread(()->{String newValue ="测试代码";Platform.runLater(()->{label.setText(newValue);//更新UI});});thread.start();});

完整代码,运行程序,点击按钮,标签文字对应变化:

package org.example.testsc1;import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;import java.io.IOException;public class HelloApplication extends Application {@Overridepublic void start( Stage primaryStage) throws IOException {/*FXMLLoader fxmlLoader =new FXMLLoader();fxmlLoader.setLocation(getClass().getResource( "Sc.fxml"));Parent root=fxmlLoader.load();Scene scene =new Scene(root);HelloController controller =fxmlLoader.getController();controller.circleLocationBind(scene);//实现圆随页面大小变化而位置变化
*/Label label = new Label( "你在干什么?");label.setLayoutX(200);label.setLayoutY(50);Button button = new Button("获取");button.setLayoutX(200);button.setLayoutY(100);button.setOnAction(event->{Thread thread =new Thread(() ->{String newValue ="测试代码";Platform.runLater(()->{label.setText(newValue);//更新UI});});thread.start();});
/*primaryStage.setTitle("Hello World");primaryStage.setScene(scene);primaryStage.show();*/AnchorPane pane =new AnchorPane();pane.getChildren().addAll(label, button);Scene scene =new Scene(pane,500, 500);primaryStage.setScene(scene);primaryStage.setScene(scene);primaryStage.setResizable(false);primaryStage.setTitle("Hello");primaryStage.show();}public static void main(String[] args){ launch(args);}
}

 

http://www.dtcms.com/a/525050.html

相关文章:

  • 专业的网站开发服务window2008 网站建设
  • Linux 查找文件
  • 法术施放选择目标逻辑概述
  • 后台管理网站开发找客户在公司做网站
  • 做游戏陪玩网站江苏省建设厅的官方网站
  • 青岛科技大学《Nano Res.》:0.5秒合成双单原子催化剂!锌空电池稳定运行650小时,氯碱电解效率达97%
  • 力扣 11. 盛最多水的容器 - Java版
  • 狗和人做网站旅游网站框架
  • 网站搭建公司排行榜网站要求
  • n8n网页抓取自动化完整指南
  • Alpine Linux:轻量、安全与高效的Linux发行版
  • 第四章、路由配置
  • Node.js:JavaScript的服务器端革命
  • C++动态内存管理详解:new/delete与malloc/free深度对比
  • 危险网站提示门户网站是如何做引流的
  • 网站代码怎么打开清风网站建设
  • Spring Boot 1.x、2.x 3.x区别汇总
  • 房产网站建设接单公关策划书模板范文
  • three.js加载三维GLB文件,查看三维模型
  • 在Linux中以root的身份进入GNOME桌面
  • 国内wordpress主题网站广元建设网站
  • 做网站哪家便宜搭建网站需要什么技能
  • 网站打不开第二天不收录啦好用的建站系统
  • 前端实现大文件上传全流程详解
  • pom.xml文件中io.swagger的swagger-bootstrap-ui和springfox-bean-validators未找到
  • ClickHouse 数据库应用场景与示例
  • 海口网红图书馆在哪里灰色行业关键词优化
  • 网站建设免费的蔬莱网站建设
  • MATLAB 实现图像边缘检测与轮廓提取(Canny、Sobel、Prewitt 算子对比)
  • 个人网站建站的流程合肥网站建合肥网站建设找蓝领商务