JavaFX 自定义字体

Vik*_*ria 5 java fonts javafx javafx-8

实际上到处搜索,但任何答案都无济于事,所以问题来了:我想为我的按钮添加自定义字体。我已经尝试编写一个 css java 类和许多其他解决方案,但没有任何帮助。

/代码删除/

如果有人能帮助我,我会很高兴!

更新:我试过这个:

package view;
import ...
public class SceneStyle {

ImageView header = new ImageView("/view/images/header.jpg");
ImageView chatImg = new ImageView("/view/images/chat_win.jpg");

//Layout Style
//public String font1 = "Impact";
//public String font2 = "Comic Sans MS";
public static String color1 = "#00adf0"; 
public static String color2 = "#0076a3"; 

public void setLabelStyle(Label label) {
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.GRAY);
}

public void setLabelStyle2(Label label){
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.GRAY);
}

public void setLabelStyle3(Label label){
    label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
    Scene scene = new Scene(label);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    label.setTextFill(Color.RED);
}

public void setButtonStyle(Button button){
    button.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 30; -fx-font-style: normal");
    Scene scene = new Scene(button);
    scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
    button.setTextFill(Color.web(color2));
    button.setPrefWidth(190);
}

public void setBorderPaneStyle(BorderPane pane, VBox btnBox, HBox scoreBox){

    pane.setTop(header);
    pane.setLeft(btnBox);
    pane.setRight(chatImg);
    pane.setBottom(scoreBox);
    pane.getLeft().setStyle("-fx-background-image: url(\"/view/images/border_left.jpg\");");
    pane.getBottom().setStyle("-fx-background-image: url(\"/view/images/bottom.jpg\");");
    //pane.getCenter().setStyle("-fx-background-image: url(\"/view/images/center.jpg\");");
    //TODO: why can't I implement this?
}

public static String getFontColor1(){ return color1; }
public static String getFontColor2(){ return color2; }
Run Code Online (Sandbox Code Playgroud)

感谢您提供详细的答案,但实际上我不想编写被覆盖的 start 方法。只是想在我的代码中实现字体。是的,现在我正在尝试使用 Google 字体。谢谢回答我的问题。

Ita*_*iha 7

以下是如何在 JavaFX 应用程序中使用自定义字体的简单示例。此示例只是示例FontLoad 应用程序的编辑版本。

输出

在此处输入图片说明


项目结构

下图是项目结构。

在此处输入图片说明


Java类

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class FontLoad extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("JavaFX Application");
        Button button = new Button("My Button");
        VBox box = new VBox(15, label, button);
        box.setAlignment(Pos.CENTER);
        Scene scene = new Scene(box, 500, 300);
        scene.getStylesheets().add(getClass().getResource("/fontstyle.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

样式表

@font-face {
    font-family: 'Fleftex';
    src: url('fonts/Fleftex_M.ttf');
}

.label {
    -fx-font-family: 'Fleftex';
    -fx-font-size: 20;
}

.button .text {
    -fx-font-family: 'Fleftex';
}
Run Code Online (Sandbox Code Playgroud)

更新 >= 8u60

从这个版本开始,属性“font-family”被忽略,你必须使用 TTF 的真实名称。

例如:要使用文件 Fleftex_M.ttf 中包含的字体“Fleftex”,您必须使用此 CSS 文件:

@font-face {
src: url(“fonts/Fleftex_M.ttf”);
}

.label {
-fx-font-family: 'Fleftex';
}
Run Code Online (Sandbox Code Playgroud)

  • 可能人们在这里可能没有看到的是,`@font-face` 在 css 文件中排在第一位并不是偶然的。这实际上是它需要工作的地方,否则它将被忽略。经过一个小时的艰苦搜索以找出为什么我的字体被忽略后了解到这一点 (6认同)

mip*_*ipa 3

这是一个小示例,展示如何加载和设置自定义字体。

package createfont;

import java.io.IOException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class CustomFontTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        String currentFontFile = "English Gothic, 17th c..TTF";
        InputStream fontStream = CustomFontTest.class.getResourceAsStream(currentFontFile);
        if (fontStream != null) {
            Font bgFont = Font.loadFont(fontStream, 36);
            fontStream.close();

            final Button button = new Button("Press me");
            button.setFont(bgFont);

            BorderPane root = new BorderPane();
            root.setCenter(button);

            Scene scene = new Scene(root, 500, 100);

            primaryStage.setTitle("CustomFontTest");
            primaryStage.setScene(scene);
            primaryStage.show();
        } else {
            throw new IOException("Could not create font: " + currentFontFile);
        }
    }

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

}
Run Code Online (Sandbox Code Playgroud)

  • 当然不是。我的假设是您可以将其替换为您自己的任何字体文件。 (4认同)