JavaFX自定义字体 [英] JavaFX custom Fonts

查看:239
本文介绍了JavaFX自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,到处都是搜索,但任何答案都不能帮助我,这是问题:
我想添加一个自定义字体到我的按钮。我已经尝试写一个CSS的Java类和许多其他解决方案,但没有什么帮助我的。



/ code deleted /



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



更新:
我试过这个:

  package view; 
import ...
public class SceneStyle {

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

//布局样式
// public String font1 =影响;
// public String font2 =Comic Sans MS;
public static String color1 =#00adf0;
public static String color2 =#0076a3;
$ b $ public void setLabelStyle(Label label){
label.setStyle( - fx-font-family:Inconsolata:700; -fx-font-size:25);
场景场景=新场景(标签);
scene.getStylesheets()。add(https://fonts.googleapis.com/css?family=Inconsolata:700);
label.setTextFill(Color.GRAY);

$ b $ public void setLabelStyle2(Label label){
label.setStyle( - fx-font-family:Inconsolata:700; -fx-font-size:25 );
场景场景=新场景(标签);
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.getStylesheets()。add(https://fonts.googleapis.com/css?family=Inconsolata:700);
label.setTextFill(Color.RED);

$ b $ public void setButtonStyle(Button button){
button.setStyle( - fx-font-family:Inconsolata:700; -fx-font-size:30; -fx-font-style:normal);
场景场景=新场景(按钮);
scene.getStylesheets()。add(https://fonts.googleapis.com/css?family=Inconsolata:700);
button.setTextFill(Color.web(color2));
button.setPrefWidth(190);


public void setBorderPaneStyle(BorderPane窗格,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:为什么我不能执行这个?
}

public static String getFontColor1(){return color1; }
public static String getFontColor2(){return color2; }

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

解决方案

这里是一个简单的例子,介绍如何在JavaFX应用程序中使用自定义字体。这个例子只是样本



b

项目结构 下图是项目结构。 / p>






< Java Class


  import javafx.application.Application; 
import javafx.geometry.Pos;
导入javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
$ b $ public class FontLoad extends Application {
@Override
public void start(Stage primaryStage)throws Exception {
Label label = new Label(JavaFX Application);
按钮按钮=新建按钮(我的按钮);
VBox box = new VBox(15,label,button);
box.setAlignment(Pos.CENTER);
场景场景=新场景(方块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';
}


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.

/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. This example is just an edited version of the sample FontLoad application.

Output


Project Structure

The follow diagram is the project structure.


Java Class

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

这篇关于JavaFX自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆