在 JavaFX 中,可以通过 `setStyle()` 方法动态修改内联 CSS 样式。以下是一个示例代码,演示如何动态修改一个按钮的内联 CSS 样式:
```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class DynamicCSSExample extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Click me");
// 添加初始的内联 CSS 样式
button.setStyle("-fx-background-color: red; -fx-text-fill: white;");
// 当按钮被点击时,动态修改内联 CSS 样式
button.setOnAction(event -> {
button.setStyle("-fx-background-color: blue; -fx-text-fill: yellow;");
});
StackPane root = new StackPane(button);
Scene scene = new Scene(root, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
在上述示例中,创建了一个按钮,并设置了初始的内联 CSS 样式。然后,通过按钮的 `setOnAction()` 方法添加了一个事件处理程序,当按钮被点击时,动态修改内联 CSS 样式。
你可以根据需要修改内联 CSS 样式的值,以实现所需的效果。
希望这个示例对你有帮助!如果你有任何其他问题,请随时提问。