java—使用javafx创建一个从一个数字开始倒计时并在0处播放音乐的程序

pu3pd22g  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(407)

尝试创建一个程序,该程序显示一个窗格,用户在其中输入一个数字并按enter键。
然后程序应该每隔一秒钟从这个数字开始倒计时。
一旦计数器达到0,它应该播放一个声音。
我很难让我的程序正常工作,也不能完全确定我把事情搞砸了,或者我做错了什么。
所以理论上,如果用户输入“30”,它应该开始倒计时到0,每次减去1。29 ... 28 ... 27 ... 等
这是我的密码:

public class Counter extends Application {

    private static final String MEDIA_URL = "http://cs.armstrong.edu/liang/common/sample.mp4";
    private TextField text = new TextField();
    int countDown = Integer.parseInt(text.getText());

    @Override
    public void start(Stage primaryStage) {

        Media media = new Media(MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);

        // create a pane and add a TextField
        BorderPane pane = new BorderPane();
        pane.setCenter(text);
        text.setFont(Font.font("Times", 35));

        // create a new animation
        Timeline animation = new Timeline(
            new KeyFrame(Duration.millis(1000), e -> {
                if (countDown > 0) {
                    countDown--;
                    text.setText(Integer.toString(countDown));
                }
                else {
                    mediaPlayer.play();
                }
              }));
              animation.setCycleCount(Timeline.INDEFINITE);

        // create and register a handler
        text.setOnAction(e -> text.setText(text.getText()));
        text.setOnAction(e -> animation.play());

        // create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String args[]) {

        Application.launch(args);

    }

}

编辑:运行时,我得到一长串错误:

Exception in Application constructor
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class Counter
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    ... 1 more
Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at Counter.<init>(Counter.java:18)
    ... 13 more
Exception running application Counter
rdlzhqv9

rdlzhqv91#

代码中有几个错误:
您正在读取文本字段和设置 countDown 立即,即在用户输入任何内容之前。当用户对文本字段执行操作时,需要读取文本。
您正在将动画的循环计数设置为 INDEFINITE 而不是 countDown 设置一次循环计数,而不是在用户提交文本字段时设置
你在设置 onAction 按钮的属性两次。 onAction 只是一个属性,就像其他属性一样:如果您设置它,然后再次设置它,它将只保存第二个值,即对的第一个调用 textField.setOnAction(...) 不会有任何影响。
文本字段上的第一个操作处理程序不会执行任何操作:它将文本字段的文本设置为文本字段的当前文本。
你需要这样的东西:

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Counter extends Application {

    private static final String MEDIA_URL = "http://cs.armstrong.edu/liang/common/sample.mp4";
    private TextField text = new TextField();
    int countDown;

    @Override
    public void start(Stage primaryStage) {

        Media media = new Media(MEDIA_URL);
        MediaPlayer mediaPlayer = new MediaPlayer(media);

        // create a pane and add a TextField
        BorderPane pane = new BorderPane();
        pane.setCenter(text);
        text.setFont(Font.font("Times", 35));

        // create a new animation
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
            if (countDown > 0) {
                countDown--;
                text.setText(Integer.toString(countDown));
            } else {
                mediaPlayer.play();
            }
        }));

        // create and register a handler
        // text.setOnAction(e -> text.setText(text.getText()));
        text.setOnAction(e -> {
            countDown = Integer.parseInt(text.getText());
            animation.setCycleCount(countDown + 1);
            animation.play();
        });

        // create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String args[]) {

        Application.launch(args);

    }

}

您可能还需要解决一些其他问题(例如,如果用户在动画进行过程中对文本字段执行操作,会发生什么情况等),但这至少会让它“工作”。

相关问题