Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fonts 如何在JavaFX2.2中嵌入.ttf字体?_Fonts_Embed_Javafx_Fxml - Fatal编程技术网

Fonts 如何在JavaFX2.2中嵌入.ttf字体?

Fonts 如何在JavaFX2.2中嵌入.ttf字体?,fonts,embed,javafx,fxml,Fonts,Embed,Javafx,Fxml,首先,我在编码方面是个新手。我需要在我基于javaFXML的应用程序中嵌入字体,但我不知道怎么做。我已将字体fontName.ttf粘贴到项目源目录下的“资源”文件夹中,即App/src/App/resources。我已经将组件(文本)的CSS设置为 我还尝试在url中添加倒逗号,即url(“resources/fontName.ttf”),但它不起作用。我还为组件设置了css id,所以这不会是问题所在。有没有其他的工作方法可以做到这一点?我看到了,但自从我有了JDK1.7U21之后,它就不起

首先,我在编码方面是个新手。我需要在我基于javaFXML的应用程序中嵌入字体,但我不知道怎么做。我已将字体
fontName.ttf
粘贴到项目源目录下的“资源”文件夹中,即
App/src/App/resources
。我已经将组件(文本)的CSS设置为


我还尝试在url中添加倒逗号,即
url(“resources/fontName.ttf”),但它不起作用。我还为组件设置了css id,所以这不会是问题所在。有没有其他的工作方法可以做到这一点?我看到了,但自从我有了JDK1.7U21之后,它就不起作用了。有没有关于嵌入字体的正确方法的想法?

解决方案方法

我更新了中的示例,以演示如何在使用CSS样式的JavaFX控件中使用自定义true type字体

重点是:

  • 将字体放置在与应用程序类相同的位置,并确保构建系统将其放置在二进制构建包(例如,应用程序jar文件)中
  • 在应用使用代码字体的样式之前,请在JavaFX代码中加载代码字体。
    Font.loadFont(CustomFontApp.class.getResource(“TRON.TTF”).toExternalForm(),10)
  • 要在样式类中使用自定义字体,请使用
    -fx font-family
    css属性,并仅引用字体名称(例如,在本例中为
    “TRON”
  • 创建并加载定义样式类的样式表
  • 将样式类应用于控件
  • 其他信息

    如果您使用的是Java8,您可能会对它感兴趣

    字体集合

    如果字体文件为
    .ttc
    格式,在单个文件中包含多个字体,则使用API(而不是
    font.loadFont
    )。请注意,
    Font.loadFonts
    仅在JDK 9之后可用,在早期版本中不可用

    使用自定义字体的示例输出

    示例代码

    该示例依赖于您可以从下载的TRON.TTF字体

    CustomFontApp.java

    自定义字体样式.css

    关于FXML的使用


    是一种采用两个参数的静态方法。我认为您不能从FXML调用font.loadFont,如果可以,也不会建议您这样做。相反,在加载需要字体的FXML或样式表之前,请先在Java代码中加载字体(正如我在回答中所做的那样)。

    我知道,在Java fx应用程序中使用自定义TTF字体时,您并没有要求使用纯编程方式,但我认为这可能有助于人们查看编程版本:

    public class Test2 extends Application {
    
      public static void main(String[] args) {
        launch(args);
      }
    
      public void start(final Stage primaryStage) {
        Group rootGroup = new Group();
    
        // create a label to show some text
        Label label = new Label("Demo Text");
        try { 
          // load a custom font from a specific location (change path!)
          // 12 is the size to use
          final Font f = Font.loadFont(new FileInputStream(new File("./myFonts/TRON.TTF")), 12);
          label.setFont(f); // use this font with our label
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
    
        rootGroup.getChildren().add(label); 
    
        // create scene, add root group and show stage
        Scene scene = new Scene(rootGroup, 640, 480, Color.WHITE);
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    }
    
    这对我来说很管用。你可以把字体放在任何你想要的地方,只要确保你调整了路径

    你可以找到更多关于


    HTH

    出现错误,提示在类
    Font
    中找不到符号:
    loadFont
    。预期有三个标识符。(我以导入javafx.scene.text.font;
    的形式导入了fxml中的字体)在答案中添加了关于fxml用法的部分。出现错误。看这里:我不能重现你的错误——我的答案中的解决方案对我有效。如果希望获得进一步的帮助,您需要发布一个复制您的错误的日志。猜测错误与其他代码逻辑有关。如果我浪费了你的时间,很抱歉,但是谢谢你告诉我答案
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.image.*;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.*;
    import javafx.stage.Stage;
    
    // demonstrates the use of a custom font.
    public class CustomFontApp extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) {
        stage.setTitle("TRON Synopsis");
    
        // load the tron font.
        Font.loadFont(
          CustomFontApp.class.getResource("TRON.TTF").toExternalForm(), 
          10
        );
    
        Label title = new Label("TRON");
        title.getStyleClass().add("title");
    
        Label caption = new Label("A sci-fi flick set in an alternate reality.");
        caption.getStyleClass().add("caption");
        caption.setMaxWidth(220);
        caption.setWrapText(true);
        caption.setTextAlignment(TextAlignment.CENTER);
    
        VBox layout = new VBox(10);
        layout.setStyle("-fx-padding: 20px; -fx-background-color: silver");
        layout.setAlignment(Pos.CENTER);
        layout.getChildren().setAll(
          title,
          new ImageView(
            new Image(
              "http://ia.media-imdb.com/images/M/MV5BMTY5NjM2MjAwOV5BMl5BanBnXkFtZTYwMTgyMzA5.V1.SY317.jpg"
            )
          ),
          caption
        );
    
        // layout the scene.
        final Scene scene = new Scene(layout);
        scene.getStylesheets().add(getClass().getResource("custom-font-styles.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
      }
    }
    
    /** file: custom-font-styles.css
     * Place in same directory as CustomFontApp.java 
     */
    
    .title {
        -fx-font-family: "TRON";
        -fx-font-size: 20;
    }
    
    .caption {
        -fx-font-family: "TRON";
        -fx-font-size: 10;
    }
    
    public class Test2 extends Application {
    
      public static void main(String[] args) {
        launch(args);
      }
    
      public void start(final Stage primaryStage) {
        Group rootGroup = new Group();
    
        // create a label to show some text
        Label label = new Label("Demo Text");
        try { 
          // load a custom font from a specific location (change path!)
          // 12 is the size to use
          final Font f = Font.loadFont(new FileInputStream(new File("./myFonts/TRON.TTF")), 12);
          label.setFont(f); // use this font with our label
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
    
        rootGroup.getChildren().add(label); 
    
        // create scene, add root group and show stage
        Scene scene = new Scene(rootGroup, 640, 480, Color.WHITE);
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    }