如何在 fxml 项目中使用很棒的字体 (javafx)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24430121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:08:53  来源:igfitidea点击:

How to use font awesome in a fxml project (javafx)

javajavafxjavafx-2javafx-8fxml

提问by user3749316

I want to use font font awesome in my project but I have no idea how to use font awesome in my project.

我想在我的项目中使用 font awesome 但我不知道如何在我的项目中使用 font awesome。

I had found some example but they can't be use in fxml.

我找到了一些例子,但它们不能在 fxml 中使用。

font awesome javafx

字体真棒 javafx

I need help how to use it in my project using fxml

我需要帮助如何使用 fxml 在我的项目中使用它

Thank you.

谢谢你。

采纳答案by agonist_

I think this is what you need ControlFXthat include font awesome support. see the javadocfor more info (But I tested it one day and it works fine)

我认为这就是你需要的ControlFX,包括字体很棒的支持。有关更多信息,请参阅javadoc(但我有一天对其进行了测试,效果很好)

回答by Nuno Rafael Figueiredo

I achieved using FA Icons by adapting Jens Deters's approach.

我通过调整Jens Deters 的方法来使用 FA Icons 。

His routines target dynamic gui composition opposing fxml's declarative way. Nevertheless, his AwesomeIcon enumeration(which maps FA comprehensible names with unicode characters) suited perfectly to my intents.

他的例程针对与 fxml 声明方式相反的动态 gui 组合。尽管如此,他的 AwesomeIcon 枚举(用 Unicode 字符映射 FA 可理解的名称)非常适合我的意图。

It should start by statically load the font in main/app class:

它应该首先在 main/app 类中静态加载字体:

public class App extends Application {
    static {
        Font.loadFont(App.class.getResource("/font/fontawesome-webfont.ttf").toExternalForm(), 10);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        URL resource = getClass().getResource("/fxml/app.fxml");
        primaryStage.setScene(new Scene((Parent) FXMLLoader.load(resource), 500, 500));
        primaryStage.setTitle("FontAwesomeFX demo");
        primaryStage.show();
    }

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

One can not use unicode characters in fxml (as needed to specify FA Icons), but can dynamic set attributes with such values. Hence having the above mentioned enumeration (AwesomeIcon), the job was done:

不能在 fxml 中使用 unicode 字符(根据需要指定 FA 图标),但可以使用此类值动态设置属性。因此有了上面提到的枚举(AwesomeIcon),工作就完成了:

  1. The import:

    <?import de.jensd.fx.fontawesome.AwesomeIcon?>
    
  2. The node:

    <Label styleClass="awesome"
           style="-fx-font-family: FontAwesome; -fx-font-size: 16.0;">
        <text><AwesomeIcon fx:constant="FILE"/></text>
    </Label>
    
  1. 进口:

    <?import de.jensd.fx.fontawesome.AwesomeIcon?>
    
  2. 节点:

    <Label styleClass="awesome"
           style="-fx-font-family: FontAwesome; -fx-font-size: 16.0;">
        <text><AwesomeIcon fx:constant="FILE"/></text>
    </Label>
    

I end up implementing an Icon Widget/Control/Component for resuming the amount of code with two properties:

我最终实现了一个图标小部件/控件/组件,用于恢复具有两个属性的代码量:

  1. value:FA Icon Name;
  2. size:styleable attribute for style -fx-font-sizeon label.
  1. 值:FA 图标名称;
  2. 尺寸:-fx-font-size标签样式的可样式化属性。

New code (same effect):

新代码(效果相同):

<Icon value="FILE" size="16"/>

The code for that control can be found here. You can also find an working example as it includes the font and testing code.

可以在此处找到该控件的代码。您还可以找到一个工作示例,因为它包含字体和测试代码。

回答by Martin Pfeffer

I ported the Android-Iconics library, developed by Mike Penz, to FX. Updates will follow soon (docs, as well)..

我将 Mike Penz 开发的 Android-Iconics 库移植到 FX。更新将很快跟进(文档,以及)。

The showcase.jar will give you an overview of the icons.

Showcase.jar 将为您提供图标的概述。

Usage (Java 1.8 required):

用法(需要 Java 1.8):

FxIconicsLabel labelTextDefault =
                (FxIconicsLabel) new FxIconicsLabel.Builder(FxFontGoogleMaterial.Icons.gmd_folder_special)
                        .size(24)
                        .text("Right (default)")
                        .color(MaterialColor.ORANGE_500)
                        .build();

(or see DialogPlayGround.java)

(或参见 DialogPlayGround.java)

FxIconics on GitHub

GitHub 上的 FxIconics

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Sedrick

If you use SceneBuildertry this.

如果你使用SceneBuilder试试这个。

  • First, download 'fontawesomefx'.
  • Second, import jar to SceneBuilderusing SceneBuilder's Jar/FXML Manager.
  • Third, Library search for FontAwesomeIconView, GlyphCheckBox, MaterialDesignIconView, MaterialIconView, or WeatherIconView
  • 首先,下载 'fontawesomefx'
  • 其次,将 jar 导入SceneBuilderusingSceneBuilder的 Jar/FXML Manager。
  • 三、图书馆搜索FontAwesomeIconView, GlyphCheckBox, MaterialDesignIconView, MaterialIconView, 或WeatherIconView

Sample FXML:

示例 FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.control.GlyphCheckBox?>
<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import de.jensd.fx.glyphs.materialicons.MaterialIconView?>
<?import de.jensd.fx.glyphs.weathericons.WeatherIconView?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox maxHeight="-Infinity" maxWidth="-Infinity">
         <children>
            <Label text="FontAwesomeIconView">
               <graphic>
                  <FontAwesomeIconView />
               </graphic>
            </Label>
            <Label text="GlyphCheckBox">
               <graphic>
                  <GlyphCheckBox />
               </graphic>
            </Label>
            <Label text="MaterialDesignIconView">
               <graphic>
                  <MaterialDesignIconView />
               </graphic>
            </Label>
            <Label text="MaterialIconView">
               <graphic>
                  <MaterialIconView />
               </graphic>
            </Label>
            <Label text="WeatherIconView">
               <graphic>
                  <WeatherIconView />
               </graphic>
            </Label>
         </children>
      </VBox>
   </children>
</StackPane>

enter image description here

在此处输入图片说明

Don't forget to add these jars to your project's classpath!

不要忘记将这些 jars 添加到项目的类路径中!

回答by Amine ABBAOUI

You can use fontawesomefxlibrary by using .jar file in Scene Builder and you can browse all available icons with Fontawesome-fx Glyph browser

您可以通过在 Scene Builder 中使用 .jar 文件来使用fontawesomefx库,并且您可以使用Fontawesome-fx Glyph 浏览器浏览所有可用的图标

回答by Omkar

As said by @Sedrick you can use fontawesomefxlibrary and use it in FXML as follows:

正如@Sedrick 所说,您可以使用fontawesomefx库并在 FXML 中使用它,如下所示:

Note:JavaFX 8 and FontAwesomeFx v8.9

注意:JavaFX 8 和 FontAwesomeFx v8.9

dashboard.fxml

仪表板.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns="http://javafx.com/javafx/8.0.121" 
    xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.example.DashboardController" 
    prefHeight="760" prefWidth="1080">

    <Button text="Close" AnchorPane.topAnchor="0" AnchorPane.leftAnchor="0">
        <graphic>
            <FontAwesomeIconView glyphName="CLOSE" glyphSize="24"/>
        </graphic>
    </Button>

</AnchorPane>

In scene builder looks like below: screenshot of iconised button

在场景构建器中,如下所示: 图标化按钮的屏幕截图