尝试创建一个程序,显示用户输入数字的窗格,然后按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
答案 0 :(得分:1)
您的代码中有几处错误:
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);
}
}
您应该解决许多其他问题(例如,如果用户在动画进行过程中对文本字段进行操作时会发生什么情况等),但这至少会使其“正常工作”。< / p>