java-javafx在普通类里如何弹出javafx的弹窗或者窗口
- 背景
- 代码
- 运行报错
- 解决方法
- 总结
- 参考
背景
想要在一个普通类里弹出一个弹窗
代码
package sample.main;import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.IOException;public class showAlertMain{public static void main(String[] args) {showWindowAlert("c");}public static void showWindowAlert(String message) {Platform.runLater(new Runnable() {@Overridepublic void run() {Stage primaryStage = new Stage();Parent root = null;FXMLLoader loader = new FXMLLoader();try {loader.setLocation(getClass().getResource("/dialog.fxml"));root = loader.load();Label cc = (Label) root.lookup("#mes");cc.setText(message);cc.setStyle("-fx-text-fill:red");Scene scene = null;scene = new Scene(root);scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {@Overridepublic void handle(KeyEvent t) {if (t.getCode() == KeyCode.ESCAPE) {Stage sb = (Stage) primaryStage.getScene().getWindow();//use any one objectsb.close();}}});primaryStage.initModality(Modality.WINDOW_MODAL);primaryStage.setScene(scene);primaryStage.setFullScreen(true);primaryStage.setFullScreenExitHint("");primaryStage.setAlwaysOnTop(true);primaryStage.initStyle(StageStyle.TRANSPARENT);primaryStage.setResizable(false);primaryStage.show();} catch (IOException e) {e.printStackTrace();}}});}
}
运行报错
Exception in thread “main” java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at javafx.application.Platform.runLater(Platform.java:86)
at sample.main.showAlertMain.showWindowAlert(showAlertMain.java:27)
at sample.main.showAlertMain.main(showAlertMain.java:22)
解决方法
在main方法或者初始化方法中调用 new JFXPanel();
package sample.main;import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.IOException;public class showAlertMain{public static void main(String[] args) {new JFXPanel();showWindowAlert("c");}public static void showWindowAlert(String message) {Platform.runLater(new Runnable() {@Overridepublic void run() {Stage primaryStage = new Stage();Parent root = null;FXMLLoader loader = new FXMLLoader();try {loader.setLocation(getClass().getResource("/dialog.fxml"));root = loader.load();Label cc = (Label) root.lookup("#mes");cc.setText(message);cc.setStyle("-fx-text-fill:red");Scene scene = null;scene = new Scene(root);scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {@Overridepublic void handle(KeyEvent t) {if (t.getCode() == KeyCode.ESCAPE) {Stage sb = (Stage) primaryStage.getScene().getWindow();//use any one objectsb.close();}}});primaryStage.initModality(Modality.WINDOW_MODAL);primaryStage.setScene(scene);primaryStage.setFullScreen(true);primaryStage.setFullScreenExitHint("");primaryStage.setAlwaysOnTop(true);primaryStage.initStyle(StageStyle.TRANSPARENT);primaryStage.setResizable(false);primaryStage.show();} catch (IOException e) {e.printStackTrace();}}});}
}
总结
方法一、执行前加new JFXPanel(),此时不需要继承Application
方法二、继承Application
参考
https://staticfinal.blog/2015/04/04/javafx-toolkit-not-initialized-solved/