3
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で(タイマーまたはあなたが知っている何かを使って)それを行うことは可能ですか?はいの場合、どのように?

4

1 に答える 1

11

タイムライントランジションを使用します。

この回答は、質問の以前の繰り返しに対するものです。

サンプル ソリューション コード

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

追加の質問への回答

ラムダ式はここでは予期されません ラムダ式は -source 1.7 ではサポートされていません (ラムダ式を有効にするには -source 8 以降を使用してください)

set ではなく 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");
    }
});

実引数リストと仮引数リストの長さが異なります

繰り返しますが、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 がかなり気になりますが、フェードするだけで問題ありません。

少なくともユーザーがクリックするまで、またはそのようなエラーメッセージをフェードアウトすることはお勧めしません。エラー メッセージが消える前にユーザーが表示しなかった場合はどうなりますか?

于 2014-04-21T06:34:37.413 に答える