text - 如何在 JavaFX 中让文本内容在一段时间后消失?

标签 text javafx javafx-2 blink

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 的一个对象,它的声明和其他细节是 -

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

我想让文本在某个时间后消失,比如 3 或 4 秒。是否可以在 JavaFX 中完成(借助计时器或您知道的其他东西)?如果是,如何?

最佳答案

使用 Timelines和/或 Transitions .

此答案适用于该问题的先前迭代。

示例解决方案代码

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 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:

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

同样,您应该使用 Java 8。但如果您希望使用 Java 7,请替换:

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

与:

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

进一步的建议

很好地呼吁不要让文本同时闪烁和淡出。闪烁的文本会使 UI 分散注意力,但只是淡出就可以了。

我认为我不建议淡化错误消息,至少在用户点击它或类似的东西之前。如果用户在它消失之前没有看到错误消息怎么办?

关于text - 如何在 JavaFX 中让文本内容在一段时间后消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190049/

相关文章:

vba - 导出时 Excel VBA 在文本文件末尾放置额外的空行

Python:从列表列表中删除最后一个元素?

file - 使用 mapreduce 为不同的输入文件在不同的文件中写入输出

JavaFX VBox 一行上有多个元素

R正则表达式: how to extract elements that contains two character in a certain order?

JavaFX 11 : "JavaFX runtime components are missing"

JavaFX 8 : Blank scene after monitor out of standby

java - 如何为 OSX 编译 .app 包

java - 将 modena.css 与 JavaFX 和 Java 7 结合使用

javafx-2 - Java FX 2 TreeView 模型引用