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

41 篇文章 0 订阅

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();
                }
            }
        });

    }
}

运行报错

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();
                }
            }
        });

    }
}

总结

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

参考

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

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您需要在一个JavaFX类中关闭其他类的窗口,您可以考虑将需要关闭的窗口作为参数传递给该类的方法。例如,您可以创建一个名为`WindowManager`的类,并在该类中定义一个名为`closeWindow`的静态方法,用于关闭指定的窗口。以下是一个示例代码: ```java public class WindowManager { public static void closeWindow(Stage window) { window.close(); } } ``` 在上面的代码中,我们定义了一个`WindowManager`类,并在该类中定义了一个名为`closeWindow`的静态方法。该方法接受一个`Stage`参数,用于指定需要关闭的窗口。在方法体中,我们调用`close()`方法关闭该窗口。 现在,如果您需要在其他JavaFX类中关闭窗口,您可以直接调用`WindowManager.closeWindow()`方法,并将需要关闭的窗口作为参数传递给该方法。例如,如果您需要在`MainController`类中关闭`SecondController`类的窗口,您可以在`MainController`中调用以下代码: ```java public class MainController { @FXML private void handleCloseButtonAction(ActionEvent event) { Stage secondStage = SecondController.getStage(); // 获取SecondController的窗口 WindowManager.closeWindow(secondStage); // 关闭SecondController的窗口 } } ``` 在上面的代码中,我们在`MainController`类中定义了一个名为`handleCloseButtonAction`的事件处理程序,该处理程序会在用户单击关闭按钮时被调用。在处理程序中,我们通过调用`SecondController.getStage()`方法获取`SecondController`类的窗口,并将其传递给`WindowManager.closeWindow()`方法关闭该窗口。 请注意,上述代码中的`SecondController.getStage()`方法是一个自定义方法,您需要在`SecondController`类中实现该方法,用于获取该类的窗口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值