在JavaFX中经过一段时间后,如何使Text内容消失? [英] How to make a Text content disappear after some time in JavaFX?

查看:129
本文介绍了在JavaFX中经过一段时间后,如何使Text内容消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b1.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connect = DriverManager
                        .getConnection("jdbc:mysql://localhost:3306/project?"
                                + "user=root&password=virus");
                statement = connect.createStatement();

                preparedStatement = connect
                        .prepareStatement("select * from mark where clsnum = " + txt1.getText() + "");
                rs = preparedStatement.executeQuery();
                if (rs.next()) {
                    delete();
                } else {
                    msg.setText("Student Not Found...!");
                }
            } catch (ClassNotFoundException | SQLException ex) {
                Logger.getLogger(DeleteMark.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

这是我的代码,如果查询不起作用(我的意思是如果没有行返回到ResultSet rs),则显示一条消息. msg是Text的对象,其声明和其他详细信息是-

This is my code to display a message if the query not worked(I mean if no row is returned to ResultSet rs). msg is an object of Text and its declaration and other details are -

Text msg = new Text();
msg.setFont(Font.font("Calibri", FontWeight.THIN, 18));
msg.setFill(Color.RED);

我想让文本在3或4秒后消失.是否可以在JavaFX中执行此操作(借助计时器或您知道的其他方法)?如果是,怎么办?

I want to make the Text disappear after sometime, like 3 or 4 seconds. Is it possible to do it in JavaFX (with the help of timer or something else you know) ? If yes, how ?

推荐答案

使用时间轴和/或转换.

此答案用于问题的先前迭代.

This answer is for a previous iteration of the question.

示例解决方案代码

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);
    }
}  

其他问题的答案

此处不希望使用lambda表达式--source 1.7中不支持lambda表达式(使用-source 8或更高版本来启用lambda表达式)

lambda expression not expected here lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

您应该使用Java 8,而不要设置-source 1.7.如果您希望坚持使用Java 7(我不建议您使用JavaFX进行工作),则可以替换Lambda:

You should use Java 8 and not set -source 1.7. If you wish to stick with Java 7 (which I don't advise for JavaFX work), you can replace the Lambda:

blinker.setOnFinished(event -> label.setText("Fading"));

具有:

blinker.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        label.setText("Fading");
    }
});

实际和形式参数列表的长度不同

actual and formal argument lists differ in length

同样,您应该使用Java8.但是,如果您希望使用Java 7,请替换:

Again, you should use Java 8. But if you wish to use Java 7, replace:

stage.setScene(new Scene(new StackPane(label), 100, 50));

具有:

StackPane layout = new StackPane();
layout.getChildren().add(label);
stage.setScene(new Scene(layout, 100, 50));

其他推荐

好消息,不要让文本同时闪烁和褪色.闪烁文本会使用户分心,但仅淡入淡出就可以了.

Good call on not having the text both blink and fade. Blinking text makes for pretty distracting UI, but just fading is fine.

我不建议我淡出错误消息,至少在用户单击它或类似内容之前.如果用户在错误消失之前没有看到错误消息怎么办?

I don't think I'd recommend fading an error message, at least until the user clicks on it or something like that. What if the user didn't see the error message before it faded away?

这篇关于在JavaFX中经过一段时间后,如何使Text内容消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆