JavaFx之Ikonli图标库大全(十五)

35 篇文章 50 订阅

JavaFx之Ikonli图标库大全(十五)

Ikonli给java提供了大量的图标库,
官网:https://kordamp.org/ikonli/

Ikonli 提供了可以在 Java 应用程序中使用的图标包。目前支持 Swing 和 JavaFX UI 工具包。

其中ikonli有非常多图标库,下面我只显示fontawesome,想要其他图标可引入对应依赖即可。
这里我演示使用javafx+java 8:ikonli-javafx-12.2.0 (JDK 11+)或ikonli-javafx-2.6.0 (JDK 8)
先引入依赖:

<!-- javafx 支持 -->
<dependency>
    <groupId>org.kordamp.ikonli</groupId>
    <artifactId>ikonli-javafx</artifactId>
    <version>2.6.0</version>
</dependency>
<!-- fontawesome图标 java8 不支持 ikonli-fontawesome5-pack -->
<dependency>
    <groupId>org.kordamp.ikonli</groupId>
    <artifactId>ikonli-fontawesome-pack</artifactId>
    <version>2.6.0</version>
</dependency>

对应的图标编码在这里:https://kordamp.org/ikonli/cheat-sheet-fontawesome.html

一、使用XML加载

import java.net.URL;

/**
 * @author lingkang
 */
public class Ikonli01 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // 加载 xml
        URL resource = getClass().getResource("/fxml/test/ikonli-icon.fxml");
        AnchorPane load = FXMLLoader.load(resource);

        primaryStage.setScene(new Scene(load));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.kordamp.ikonli.javafx.FontIcon?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            prefHeight="400.0" prefWidth="600.0">
    <Button text="按钮图标" style="-fx-background-color: red">
        <graphic>
            <FontIcon fill="white" iconLiteral="fa-address-book"/>
        </graphic>
    </Button>
    <Button text="普通按钮图标" layoutY="30" style="-fx-background-color: blue"></Button>
</AnchorPane>

在这里插入图片描述

二、使用代码加载

AnchorPane anchorPane=new AnchorPane();
        anchorPane.setPrefWidth(400);
        anchorPane.setPrefHeight(300);

        FontIcon fontIcon=new FontIcon("fa-address-card-o");
        fontIcon.setFill(Paint.valueOf("red"));

        // 图标需要放在label中,或者其他标签,例如按钮等
        Label label=new Label();
        label.setGraphic(fontIcon);
        label.setFont(Font.font(18));
        label.setLayoutX(10);
        label.setLayoutY(10);

        FontIcon icon=new FontIcon("fa-apple");
        
        Button button=new Button("图标按钮");
        button.setGraphic(icon);
        button.setLayoutX(10);
        button.setLayoutY(40);
        button.setFont(Font.font(30));
        anchorPane.getChildren().addAll(label,button);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.show();

在这里插入图片描述

三、一健三连

创作不易,你觉得对你有帮助请给我点个赞!一健三连…谢谢啦!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌康ACG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值