如何在fxml项目中使用字体awesome(javafx)

时间:2014-06-26 12:07:38

标签: java javafx javafx-2 javafx-8 fxml

我想在我的项目中使用字体字体很棒,但我不知道如何在我的项目中使用字体真棒。

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

font awesome javafx

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

谢谢。

6 个答案:

答案 0 :(得分:11)

我通过调整Jens Deters's approach来实现使用FA图标。

他的惯用目标是反对fxml的声明方式的动态组合。尽管如此,his AwesomeIcon enumeration(使用unicode字符映射FA可理解的名称)完全符合我的意图。

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

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

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

  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>
    
  3. 我最终实现了一个Icon Widget / Control / Component,用于恢复具有两个属性的代码量:

    1. :FA图标名称;
    2. 尺寸:标签上的样式-fx-font-size的可设置样式属性。
    3. 新代码(效果相同):

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

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

答案 1 :(得分:7)

我认为这是您需要的ControlFX,其中包括字体非常棒的支持。 请参阅javadoc了解更多信息(但我有一天测试了它并且工作正常)

答案 2 :(得分:6)

我将由Mike Penz开发的Android-Iconics库移植到FX。更新将很快跟进(文档也是如此)..

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

用法(需要Java 1.8 ):

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

(或参见DialogPlayGround.java)

FxIconics on GitHub

enter image description here enter image description here

答案 3 :(得分:3)

如果你使用SceneBuilder试试这个。

  • 首先,下载 'fontawesomefx'
  • 其次,使用SceneBuilder的Jar / FXML管理器将jar导入FontAwesomeIconView
  • 第三,图书馆搜索GlyphCheckBoxMaterialDesignIconViewMaterialIconViewWeatherIconView<?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>

样本FXML:

$('#toggle').on('click', function(){
    if ( $(this).is(':checked') ) {
        $('#link').show();
    } 
    else {
        $('#link').hide();
    }
});

enter image description here

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

答案 4 :(得分:1)

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

注意:JavaFX 8和FontAwesomeFx v8.9

dashboard.fxml

categories = [],
series = [];

$.getJSON('ajax/ams_sla_report_chart.php', function(data,name){


    data.forEach(function(arr) {
        arr.forEach(function(el, i) {

            if (i === 0) {
                categories.push(el);

            } else if (series[i - 1]) {
                series[i - 1].data.push(el);

            } else {

                series.push({
                    name:['MR','MR_HIT','MR_HIT_PERCENTAGE'],       
                    data: [el]

                });


            }
        });
    });

    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'containers',
            type: 'column',
            inverted: false    
        },
        legend: {
            layout: 'horizontal',
            align: 'right',
            verticalAlign: 'middle'
          },
        xAxis: {
            categories: categories
        },
        title: {
           text: 'Priority Based on SLA'
        },

        series:series

    });


    function showValues() {
    $('#alpha-value').html(chart.options.chart.options3d.alpha);
    $('#beta-value').html(chart.options.chart.options3d.beta);
    $('#depth-value').html(chart.options.chart.options3d.depth);
    }

    // Activate the sliders
    $('#sliders_eng input').on('input change', function () {
        chart.options.chart.options3d[this.id] = parseFloat(this.value);
        showValues();
        chart.redraw(false);
    });

    showValues();


});

在场景构建器中如下所示: screenshot of iconised button

答案 5 :(得分:0)

您可以通过使用Scene Builder中的.jar文件来使用fontawesomefx库 您可以使用Fontawesome-fx Glyph browser

浏览所有可用图标