java - 在标签上使用 FadeTransition 会导致标签在过渡开始时显示不同

标签 java text javafx label fade

我的项目需要一个Label或一个Text。我需要标签以便可以使用省略号。但问题是,当我尝试使用 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);

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

关于java - 在标签上使用 FadeTransition 会导致标签在过渡开始时显示不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32275118/

相关文章:

java - 在 Java 中将 JScrollPane 包装到 TextArea(与 GridBagConstraints 一起使用)

文本编辑器显示\r\n?

HTML 和 CSS 文本背景黑色

text - 将 ElasticSearch 与 Hadoop Map Reduce 结合使用

java - 通过读取每行的第一个字符将文件读入 JavaFX 网格以模块化地创建表单?

java - 如何将 Java Swing 应用程序提交到 Mac App Store

java - 如何将包含List的Java Map转换为Scala Map

java - 如何确定属于空间中两条不相交线之间距离的点

java - "Line of Sight"多边形上的顶点到所有其他多边形顶点

JavaFX - 为什么每个 Control 元素下都有一行以及如何删除它?