Text 如何在JavaFX中使文本内容在一段时间后消失?
我想让文本在一段时间后消失,比如3或4秒。有没有可能在JavaFX中实现(借助计时器或其他您知道的东西)?如果是,如何使用?使用和/或 此答案适用于问题的上一次迭代 样本溶液代码Text 如何在JavaFX中使文本内容在一段时间后消失?,text,javafx,javafx-2,blink,Text,Javafx,Javafx 2,Blink,我想让文本在一段时间后消失,比如3或4秒。有没有可能在JavaFX中实现(借助计时器或其他您知道的东西)?如果是,如何使用?使用和/或 此答案适用于问题的上一次迭代 样本溶液代码 Text msg = new Text(); msg.setFont(Font.font("Calibri", FontWeight.THIN, 18)); msg.setFill(Color.RED); 其他问题的答案 此处不需要lambda表达式-source 1.7不支持lambda表达式(请使用-source
Text msg = new Text();
msg.setFont(Font.font("Calibri", FontWeight.THIN, 18));
msg.setFill(Color.RED);
其他问题的答案
此处不需要lambda表达式-source 1.7不支持lambda表达式(请使用-source 8或更高版本来启用lambda表达式)
您应该使用Java 8,而不是设置-source 1.7
。如果您希望坚持使用Java 7(我不建议使用JavaFX),可以替换Lambda:
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BlinkingAndFading extends Application {
@Override
public void start(Stage stage) {
Label label = new Label("Blinking");
label.setStyle("-fx-text-fill: red; -fx-padding: 10px;");
Timeline blinker = createBlinker(label);
blinker.setOnFinished(event -> label.setText("Fading"));
FadeTransition fader = createFader(label);
SequentialTransition blinkThenFade = new SequentialTransition(
label,
blinker,
fader
);
stage.setScene(new Scene(new StackPane(label), 100, 50));
stage.show();
blinkThenFade.play();
}
private Timeline createBlinker(Node node) {
Timeline blink = new Timeline(
new KeyFrame(
Duration.seconds(0),
new KeyValue(
node.opacityProperty(),
1,
Interpolator.DISCRETE
)
),
new KeyFrame(
Duration.seconds(0.5),
new KeyValue(
node.opacityProperty(),
0,
Interpolator.DISCRETE
)
),
new KeyFrame(
Duration.seconds(1),
new KeyValue(
node.opacityProperty(),
1,
Interpolator.DISCRETE
)
)
);
blink.setCycleCount(3);
return blink;
}
private FadeTransition createFader(Node node) {
FadeTransition fade = new FadeTransition(Duration.seconds(2), node);
fade.setFromValue(1);
fade.setToValue(0);
return fade;
}
public static void main(String[] args) {
launch(args);
}
}
与:
与:
进一步建议
这是一个很好的通话,文字不会同时闪烁和褪色。闪烁的文本会让用户界面分心,但淡入淡出就可以了
我不建议删除错误消息,至少在用户点击它或类似的东西之前。如果用户在错误信息消失之前没有看到它,该怎么办?请不要在问题中发布不相关的代码,例如数据库查询。UI工作与数据库工作无关,您应该将问题代码和实现代码分开处理。blinker.setOnFinished(事件->标签.setText(“褪色”);(原因-source 1.7(使用-source 8或更高版本来启用lambda表达式))2中不支持lambda表达式。舞台场景(新场景(新StackPane(标签),100,50);(原因-类StackPane中的构造函数StackPane无法应用于给定类型;必需:未找到参数:标签原因:实际参数列表和形式参数列表长度不同)。你能纠正一下吗。这会很有帮助的。再来一个帮助。我编辑了我的问题。请你根据它编辑你的答案好吗?我删除了闪烁。你只是告诉我如何增加褪色前的时间。我也发现了这一点。谢谢你的帮助。
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class BlinkingAndFading extends Application {
@Override
public void start(Stage stage) {
Label label = new Label("Blinking");
label.setStyle("-fx-text-fill: red; -fx-padding: 10px;");
Timeline blinker = createBlinker(label);
blinker.setOnFinished(event -> label.setText("Fading"));
FadeTransition fader = createFader(label);
SequentialTransition blinkThenFade = new SequentialTransition(
label,
blinker,
fader
);
stage.setScene(new Scene(new StackPane(label), 100, 50));
stage.show();
blinkThenFade.play();
}
private Timeline createBlinker(Node node) {
Timeline blink = new Timeline(
new KeyFrame(
Duration.seconds(0),
new KeyValue(
node.opacityProperty(),
1,
Interpolator.DISCRETE
)
),
new KeyFrame(
Duration.seconds(0.5),
new KeyValue(
node.opacityProperty(),
0,
Interpolator.DISCRETE
)
),
new KeyFrame(
Duration.seconds(1),
new KeyValue(
node.opacityProperty(),
1,
Interpolator.DISCRETE
)
)
);
blink.setCycleCount(3);
return blink;
}
private FadeTransition createFader(Node node) {
FadeTransition fade = new FadeTransition(Duration.seconds(2), node);
fade.setFromValue(1);
fade.setToValue(0);
return fade;
}
public static void main(String[] args) {
launch(args);
}
}
blinker.setOnFinished(event -> label.setText("Fading"));
blinker.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
label.setText("Fading");
}
});
stage.setScene(new Scene(new StackPane(label), 100, 50));
StackPane layout = new StackPane();
layout.getChildren().add(label);
stage.setScene(new Scene(layout, 100, 50));