当前位置:   article > 正文

java-javafx在普通类里如何弹出javafx的弹窗或者窗口

java-javafx在普通类里如何弹出javafx的弹窗或者窗口

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() {
            @Override
            public 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>() {
                        @Override
                        public void handle(KeyEvent t) {
                            if (t.getCode() == KeyCode.ESCAPE) {
                                Stage sb = (Stage) primaryStage.getScene().getWindow();//use any one object
                                sb.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();
                }
            }
        });

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

运行报错

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() {
            @Override
            public 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>() {
                        @Override
                        public void handle(KeyEvent t) {
                            if (t.getCode() == KeyCode.ESCAPE) {
                                Stage sb = (Stage) primaryStage.getScene().getWindow();//use any one object
                                sb.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();
                }
            }
        });

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

总结

方法一、执行前加new JFXPanel(),此时不需要继承Application
方法二、继承Application

参考

https://staticfinal.blog/2015/04/04/javafx-toolkit-not-initialized-solved/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/451505
推荐阅读
相关标签
  

闽ICP备14008679号