JavaFX 自定义字体

标签 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; }

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

最佳答案

这是一个简单示例,说明如何在 JavaFX 应用程序中使用自定义字体。此示例只是示例 FontLoad application 的编辑版本.

输出

enter image description here


项目结构

下图是项目结构。

enter image description here


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

样式表

@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';
}

更新 >= 8u60

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

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

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

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

关于JavaFX 自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336542/

相关文章:

java - 来自 API 调用的字符串翻译

java - 如何解决 "java.lang.OutOfMemoryError: Java heap space"问题?

java - 如何更改 JTable 中字符串的字体大小?

java - 关闭 javafx 窗口处理程序

FileChooser 保存对话框上的 JavaFX ExtensionFilter 在 macOS 上太宽

java - 安卓 : Unable to Access Function From Other Class (Service)

java - 用其后面的字符替换元音

php - 如何更改我的 php 的颜色和字体大小?如果我尝试,它不起作用

css - 创建透明的透视或纹理字体样式

java - 使用 Maven 编译具有所有依赖项的 JavaFX jar