查了很多,这个最详细,且解决了我的问题:
借鉴这里:Intellij IDLE 中javafx使用与配置
(其中,最关键一步:点击Add VM options→在输入框输入–module-path “自己的javafx的lib文件夹路径” --add-modules javafx.controls,javafx.fxml)
1.JavaFX的controller交付于Spring管理
对于Controller用@Component注释(Spring中@component的使用
@component是spring中的一个注解,它的作用就是实现bean的注入)
package tsd.usl;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
//import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Demo1Application extends Application {
//这个封装的方法的作用是加载FXML界面布局文件,并且让Spring来管理JavaFX的Controller对象。
//在这之后无需配置,Controller类自然就成了Spring容器的一部分。
private static ApplicationContext applicationContext;
private static FXMLLoader loadFxml(String fxmlPath){
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(Demo1Application.class.getResource(fxmlPath));
//下面这行就是结合JavaFX和Spring的关键行
fxmlLoader.setControllerFactory(applicationContext::getBean);
return fxmlLoader;
}
// private static ConfigurableApplicationContext applicationContext;
public static void main(String[] args) {
//做准备:先封装一个方法。Refactor——Extract/Introduce——Field
applicationContext = SpringApplication.run(Demo1Application.class, args);
Application.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
// stage.setTitle("HELLO WORLD!");
stage.setScene(new Scene(loadFxml("/pages/connect.fxml").load()));
stage.setTitle("图像检测平台");
stage.show();
}
}
2.java.lang.IllegalStateException: Not on FX application thread
运行javaFX程序,系统会自动创建一个FX application thread线程,用于更新界面的组件信息,例如ListView的items、Label的text。
但是我这里使用Socket进行长连接时,需要建立多线程,而自己创建的线程又直接导致了界面组件信息的更新时,控制台就会抛出java.lang.IllegalStateException异常
用Platform类的runLater方法可以解决该问题:
Platform.runLater(()->{
daccuracy.setText("准确率为:" + accuracy);//该段代码如果无Platform的话,会Exception
});
3.新的界面加载
只需要在别的Controler内调用这里的showDdetails()即可
package tsd.usl;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class Ddetails extends Application {
Stage stage = new Stage();
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/pages/ddetails.fxml"));
stage.setTitle("Ddetails");
stage.setScene(new Scene(root, 600, 400));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public void showDdetails() throws Exception {
start(stage);
}
}
4.不同Controller间信息传输
这里用的是定义static成员,如此以来即可全局访问该类内成员变量
package tsd.usl.controller;
import org.w3c.dom.ls.LSException;
import java.util.ArrayList;
import java.util.List;
public class GlobalData {
public static List staticlistScore = new ArrayList<>();
public static List staticlistdAddress = new ArrayList<>();
public static List staticlistDtype = new ArrayList<>();
public static List staticlistDtyperight = new ArrayList<>();
}



