JavaFX could not load @ font face font because com sun. javafx. css. StyleManager loadStylesheetUnPrivileged

I have asked a similar question here, but it seems unclear, because I have a lot of code in the project and can't be published here, so please don't mark it as duplicate

Therefore, I decided to create a new project with just one label, make the code small and clean, and eliminate other potential suspects I got wrong

So here is my java source code

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();

        Label label = new Label("Sample Label");
        label.setId("sampleLabel");
        root.getChildren().add(label);

        Scene scene = new Scene(root,300,275);
        scene.getStylesheets().add(getClass().getResource("applicationStyles.css").toExternalForm());
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

This is my CSS file

/**/
@font-face {
    font-family:'Roboto';
    src:url("Roboto-Thin.ttf");
}
#sampleLabel{
    -fx-font-family: Roboto ;
}

This is the error I encountered in IntelliJ idea

Dec 02,2015 9:16:34 AM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not load @font-face font [file:/C:/Users/UserName/Desktop/Java8%20projects/TeamViewer/out/production/TeamViewer/sample/Roboto-Thin.ttf]

Thank you. Thank you very much for any help

Solution

I found possible causes and solutions:

It seems that this function cannot use its path / url string Therefore, you need to resolve the path and replace it with spaces This means that you must use code - loaded fonts instead of CSS (now)

In clojure, my work is as follows:

(require '[clojure.java.io :as cio])
(require '[clojure.string :as s])
(-> "fonts/SourceCodePro-Regular.ttf" 
  cio/resource 
  str 
  (s/replace "%20" " ") 
  (javafx.scene.text.Font/loadFont  10.))

The above is the JavaFX collected by programming house for you. You can't load @ font face fonts because com sun. javafx. css. Stylemanager loadstylesheetunprivileged all the contents. I hope this article can help you solve the problem that JavaFX cannot load @ font face fonts because com sun. javafx. css. Program development problems encountered by stylemanager loadstylesheetunprivileged.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>