简体   繁体   English

JavaFX自定义字体

[英]JavaFX custom Fonts

Actually searched everywhere but any answer can't help me, sohere's the problem: I want to add a custom font to my buttons. 实际上到处搜索,但任何答案都无法帮助我,问题是:我想在我的按钮上添加自定义字体。 I already tried to write a css java class and a lot of other solutions, but nothing helped my. 我已经尝试编写一个css java类和许多其他解决方案,但没有任何帮助我。

/code deleted/ /代码已删除/

I'd be so glad if anyone could help me! 如果有人能帮助我,我会很高兴的!

Update: I tried this: 更新:我试过这个:

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

Thanks for the detailed answers, but actually I don't want to write a start method which is overwritten. 感谢您的详细解答,但实际上我不想写一个被覆盖的启动方法。 Just want to implemtent the font in my code. 只想在我的代码中实现字体。 And yes now I'm trying with the Google Fonts. 是的,现在我正在尝试谷歌字体。 Thanks for answering my question. 谢谢回答我的问题。

Here is a simple example of how to use custom fonts in your JavaFX application. 以下是如何在JavaFX应用程序中使用自定义字体的简单示例。 This example is just an edited version of the sample FontLoad application . 此示例只是示例FontLoad应用程序的已编辑版本。

Output 产量

在此输入图像描述


Project Structure 项目结构

The follow diagram is the project structure. 下图是项目结构。

在此输入图像描述


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

Stylesheet 样式表

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

Update >= 8u60 更新> = 8u60

Starting with this version the attribute “font-family” is ignored and you have to use the real name of the TTF. 从此版本开始,将忽略属性“font-family”,您必须使用TTF的真实名称。

By example: to use the font “Fleftex” contained on the file Fleftex_M.ttf You have to use this CSS file : 例如:使用Fleftex_M.ttf文件中包含的字体“Fleftex”你必须使用这个CSS文件:

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

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

Here is a little example showing how to load and set a custom font. 这是一个显示如何加载和设置自定义字体的小例子。

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

}

I tried this: 我试过这个:

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

in each method. 在每种方法中。 Because I don't want write a new methode which overrides the start method just like above. 因为我不想像上面那样编写一个覆盖start方法的新方法。 (Actually thanks for this detailed answer, but it isn't what I am searching for). (实际上感谢这个详细的答案,但它不是我正在寻找的)。 But also my solution doesn't work well... 但我的解决方案也不能很好地运作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM