我的项目需要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 :(得分:1)
最初将标签的不透明度设置为0.99:
groupByKey
同样以相同的方式更改label.setOpacity(0.99);
方法中的代码。然后,将淡入淡出过渡的起始值设置为0.99:
setOnFinished
我知道这不是您正在寻找的解决方案,但此解决方案可防止标签在开始时突然变暗。那是因为标签实际上是从较暗的状态开始的。