サンプル アプリケーションから始めましょう。
Main.java
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Button");
VBox vBox = new VBox(button);
vBox.setPadding(new Insets(10.0));
Scene scene = new Scene(vBox, 200, 100);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
System.out.println();
}
public static void main(String[] args) {
launch(args);
}
}
アプリケーション.css
.button {
-fx-graphic: url(image.png);
}
結果

方法 1 (画像に使用されているクラスを調べる)
これは、デバッガーを使用して簡単に実行できます ( println()にブレークポイントを設定し、 button.graphic.valueの内容を確認します)。ここで使用されるクラスはImageViewです。これは、次を使用して画像を回転できることを意味します。
.button .image-view {
-fx-rotate: 45;
}
結果

方法 2 (グラフィック オブジェクトのカスタム クラスを設定する)
これは、ChangeListener を使用して実行できます。
button.graphicProperty().addListener((ChangeListener<Node>) (observable, oldValue, newValue) -> {
newValue.getStyleClass().add("my-class");
});
次に、以下を使用して画像を回転できます。
.my-class {
-fx-rotate: 45;
}
結果

パディング
画像が占めるスペースが大きすぎる場合は、ボタンに追加のパディングを追加する必要がある場合があります。
.button {
-fx-graphic: url(image.png);
-fx-graphic-text-gap: 10;
-fx-label-padding: 5 0 5 5;
}
結果
