赞
踩
想要在一个普通类里弹出一个弹窗
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/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。