在 Label 上使用 FadeTransition 会导致 Label 在过渡开始时显示不同

Using a FadeTransition on a Label causes the Label to appear different at start of transition

我的项目需要 LabelText。我需要标签以便可以使用省略号。但问题是,当我尝试使用 FadeTransition 并播放它时,标签在开始时会稍微变暗。这是一些演示代码:

package com.neonorb.test;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;

/**
 * Created by chris on 7/20/15.
 */
public class Test extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {
        Label label = new Label("hello");
        //Text label = new Text("hello);//Using Text instead of Label does not cause the weird behavior
        FadeTransition fadeTransition = new FadeTransition(Duration.seconds(3), label);
        fadeTransition.setFromValue(1.0);
        fadeTransition.setToValue(0.0);
        fadeTransition.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                label.setOpacity(1.0);
            }
        });
        Button button = new Button("play");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                fadeTransition.play();
            }
        });
        BorderPane borderPane = new BorderPane();
        borderPane.setCenter(label);
        borderPane.setBottom(button);
        primaryStage.setScene(new Scene(borderPane));
        primaryStage.show();
    }
}

所以我要么需要解决这个问题,要么需要一种在 Text 中使用省略号的方法。有什么想法吗?

最初将标签的不透明度设置为 0.99:

label.setOpacity(0.99);

同样修改setOnFinished方法里面的代码。然后,将淡入淡出过渡的起始值设置为 0.99:

fadeTransition.setFromValue(0.99);

我知道这不是您正在寻找的解决方案,但此解决方案可防止标签在开始时突然变暗。那是因为标签实际上以较暗的状态开始。