如何计算 javafx 中 css 的效果?

How to time the effect of a css look in javafx?

是否可以为CSS中描述的效果提供时间?

所以我想要的是,如果你按下一个按钮,那么它的外观变化会持续更长时间,甚至在释放后会持续一段时间。

所以我会想象 .button:pressed{} 部分中的某种命令,这使得这成为可能。

或者如果它只能在 java 代码中出现,那也很好! 你有好主意吗?谢谢!

例如:

.button:hover:pressed {
-fx-background-color: #A6C3F1;
-fx-background-radius: 30;
-fx-text-fill: black;
//pseudo: -fx-pressed-lasts: 300 (milisec);

因此,如果您在按住按钮的同时单击按钮,按钮将具有这种颜色。即使在释放鼠标点击后,我也希望这种颜色持续 300 毫秒。然后它会变回 .button:hover{ - whatever -}

我认为 css 内是不可能的。在 Java 代码中,您可以使用

@Override
public void start( Stage primaryStage )
{
    final Button btn = new Button( "Say 'Hello World'" );
    final PauseTransition pt = new PauseTransition( Duration.millis( 3000 ) );
    pt.setOnFinished( ( ActionEvent event ) ->
    {
        btn.getStyleClass().remove( "my-button-pressed" );
    } );

    btn.setOnMousePressed( ( MouseEvent event ) ->
    {
        if ( pt.getStatus() != Animation.Status.RUNNING )
        {
            btn.getStyleClass().add( "my-button-pressed" );
            pt.play();
        }
    } );

    StackPane root = new StackPane();
    root.getChildren().add( btn );

    Scene scene = new Scene( root, 300, 250 );
    scene.getStylesheets().add( this.getClass().getResource( "style.css" ).toExternalForm() );
    primaryStage.setScene( scene );
    primaryStage.show();
}

和style.css喜欢

.my-button-pressed {
    -fx-font: 16px "Serif";
    -fx-padding: 10;
    -fx-background-color: #CCFF99;
}