javafx 关于图标

main

package org.example;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import org.kordamp.ikonli.Ikon;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.concurrent.Callable;

public class Main extends Application {
    public static void main(String[] args) {

        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        FlowPane root = new FlowPane();
        Button button = new Button("测试");
        button.setStyle("-fx-font-size: 24px;-fx-text-fill: blue");
        button.setOnAction(actionEvent -> {
            System.out.println(123);
        });
        FontIcon fontIcon = new FontIcon(AntDesignIconsFilled.ALIPAY_SQUARE);
        fontIcon.setIconSize(30);
        fontIcon.setIconColor(Color.BLUE);
        button.setGraphic(fontIcon);


        Button button2 = new Button("GitHub");
        button2.setGraphic(new FontIcon());
        button2.getStyleClass().add("btn2");


        ToggleButton toggleButton = new ToggleButton("麦克风");
        toggleButton.setGraphic(new FontIcon());
        toggleButton.getStyleClass().add("toggleButton");

        ToggleButton toggleButton2 = new ToggleButton("麦克风");
        FontIcon fontIcon1 = new FontIcon();


//        fontIcon1.iconCodeProperty().bind(
//                Bindings.when(toggleButton2.selectedProperty())
//                        .then(AntDesignIconsFilled.AUDIO)
//                        .otherwise(AntDesignIconsFilled.PAUSE_CIRCLE)
//        );
//        fontIcon1.iconCodeProperty().bind(
//                toggleButton2.selectedProperty().map(
//                        s -> s?AntDesignIconsFilled.ALIPAY_SQUARE:AntDesignIconsFilled.API
//                )
//        );
//
        fontIcon1.iconCodeProperty().bind(Bindings.createObjectBinding(new Callable<Ikon>() {
            @Override
            public Ikon call() throws Exception {
                return !toggleButton2.isSelected() ? AntDesignIconsFilled.ALIPAY_SQUARE : AntDesignIconsFilled.API;
            }
        }, toggleButton2.selectedProperty()));
        toggleButton2.setGraphic(fontIcon1);


        root.getChildren().addAll(button, button2, toggleButton, toggleButton2);

        root.getStylesheets().add(getClass().getResource("/css/icon-app.css").toExternalForm());
        stage.setScene(new Scene(root, 300, 100));

        stage.setTitle("图标测试");
        stage.show();

    }
}

css

.btn2{
    -fx-font-size: 20px;
}

.btn2 .ikonli-font-icon{
    -fx-icon-color: #2d2d86;
    -fx-icon-code: "antf-aliwangwang";
}
.toggleButton{
    -fx-font-size: 20px;
}
.toggleButton:selected .ikonli-font-icon{
    -fx-icon-code: "antf-behance-circle";
}
.toggleButton .ikonli-font-icon{
    -fx-icon-color: #f102ae;
    -fx-icon-code: "antf-behance-square";
    -fx-icon-size: 25px;
}

枚举类

open module jdk20ceshi {
    requires javafx.controls;
    requires org.kordamp.ikonli.core;
    requires org.kordamp.ikonli.javafx;
    requires org.kordamp.ikonli.antdesignicons;
}

pom


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>jdk20ceshiartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>20maven.compiler.source>
        <maven.compiler.target>20maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>
    <dependencies>

        <dependency>
            <groupId>org.openjfxgroupId>
            <artifactId>javafx-controlsartifactId>
            <version>20version>
        dependency>
        <dependency>
            <groupId>org.kordamp.ikonligroupId>
            <artifactId>ikonli-javafxartifactId>
            <version>12.3.1version>
        dependency>
        <dependency>
            <groupId>org.kordamp.ikonligroupId>
            <artifactId>ikonli-antdesignicons-packartifactId>
            <version>12.3.1version>
        dependency>
    dependencies>
project>

你可能感兴趣的:(Java,java)