我试图创建一个Stopwatch
应用程序,但我一直遇到相同的运行时异常。我已经和它斗争了好几天,我不知道如何修复它。任何建议都将不胜感激!这是我在Java中的第一个相当长的项目,所以我决心解决它;只是有相当多的麻烦,我不知道如何克服这一点。
TimerStopWatch.java
import java.text.DecimalFormat;
import java.util.Optional;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
class TimerStopWatch {
private ImageView dialImageView = new ImageView();
private ImageView handImageView = new ImageView();
private String dialImageName = "clockface.png";
private String handImageName = "hand.png";
private Image dialImage = new Image(getClass().getResourceAsStream(dialImageName));
private Image handImage = new Image(getClass().getResourceAsStream(handImageName));
private StackPane analogContainer = new StackPane();
private Text digitalLabel = new Text();
private Text timerLabel = new Text();
private Label championLabel = new Label();
private VBox timeInfoBox = new VBox();
private Label lapTimeLabel = new Label();
private Text record1Label = new Text();
private Text record2Label = new Text();
private Text record3Label = new Text();
private VBox recInfoBox = new VBox();
private HBox infoBox = new HBox();
private Button funcButton = new Button("Record");
private Button recButton = new Button("Start");
private HBox buttonsBox = new HBox();
private GridPane grid = new GridPane();
private double tickTimeInSeconds = 0.01;
private double angleDeltaPerSeconds = 6.0;
double secondsElapsed = 0;
private Timeline timeline;
private KeyFrame keyFrame;
private String timeInfo = "00:00.00";
private int timerStartTime = 60;
private double timerSecondsLeft = 60.00;
private int timeUpTag = 0;
private Alert notNumbersAlert = new Alert(Alert.AlertType.ERROR,"Insert only integers!", ButtonType.CLOSE);
private DecimalFormat dfTimer = new DecimalFormat("#.00");
private DecimalFormat dfRec = new DecimalFormat("00");
private double oldTime = 0.00;
private int numberOfRecords = 0;
public TimerStopWatch(){
setupTimer();
setupUI();
setupTimeline();
setupButtonAction();
}
private void setupTimer(){
TextInputDialog dialog = new TextInputDialog("");
dialog.setTitle("Timer Start Time Set Up");
dialog.setHeaderText("Set up the start time:");
dialog.setContentText("Please set up the start time (Integer):");
boolean legalTime = false;
while(!legalTime){
Optional<String> input = dialog.showAndWait();
if(input.isPresent()){
if((input.get().matches("[0-9]+") && input.get().length() > 0 ) == false){
notNumbersAlert.showAndWait();
}else{
timerStartTime = Integer.valueOf(input.get());
legalTime = true;
}
}
}
timerSecondsLeft = timerStartTime;
}
private void setupUI(){
dialImageView.setImage(dialImage);
handImageView.setImage(handImage);
analogContainer.getChildren().addAll(dialImageView, handImageView);
digitalLabel.setText(timeInfo);
digitalLabel.setFont(Font.font ("Verdana", 30));
timerLabel.setText("Timer: " + String.valueOf(timerSecondsLeft) + ".00");
timerLabel.setFont(Font.font ("Verdana", 20));
championLabel.setText("Champion: 00:00:00");
championLabel.setFont(Font.font ("Verdana", 20));
lapTimeLabel.setText("Lap Time");
lapTimeLabel.setFont(Font.font ("Verdana", 20));
record1Label.setText("Rec 00 +00:00.00");
record1Label.setFont(Font.font ("Verdana", 18));
record2Label.setText("Rec 00 +00:00.00");
record2Label.setFont(Font.font ("Verdana", 18));
record3Label.setText("Rec 00 +00:00.00");
record3Label.setFont(Font.font ("Verdana", 18));
infoBox.setAlignment(Pos.CENTER);
infoBox.setSpacing(20);
infoBox.getChildren().addAll(timeInfoBox, recInfoBox);
timeInfoBox.setAlignment(Pos.CENTER);
timeInfoBox.setSpacing(10);
timeInfoBox.getChildren().addAll(digitalLabel, timerLabel, championLabel);
recInfoBox.setAlignment(Pos.CENTER);
recInfoBox.setSpacing(5);
recInfoBox.getChildren().addAll(lapTimeLabel, record1Label, record2Label, record3Label);
buttonsBox.setAlignment(Pos.CENTER);
buttonsBox.setSpacing(10);
buttonsBox.getChildren().addAll(funcButton, recButton);
grid.setVgap(30);
grid.setHgap(10);
grid.setAlignment(Pos.CENTER);
grid.add(analogContainer, 0, 0);
grid.add(infoBox, 0, 1);
grid.add(buttonsBox, 0, 2);
}
private void setupTimeline(){
if(isRunning()){
timeline.stop();
}
keyFrame = new KeyFrame(Duration.millis(tickTimeInSeconds*1000), (ActionEvent event) -> {
update();
});
timeline = new Timeline(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
}
private void setupButtonAction(){
recButton.setOnAction((ActionEvent event) -> {
if(timeline.getStatus() != Animation.Status.RUNNING){
timeline.play();
recButton.setText("Stop");
funcButton.setText("Record");
}else {
timeline.stop();
recButton.setText("Start");
funcButton.setText("Reset");
}
});
funcButton.setOnAction((ActionEvent event) -> {
if(timeline.getStatus() == Animation.Status.RUNNING){
if(timerLabel.getText() != "Time's Up!"){
numberOfRecords++;
if(numberOfRecords == 1){
championLabel.setText("Champion: " + getMessage(secondsElapsed));
}
if((numberOfRecords % 3) == 1){
record1Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
}else if ((numberOfRecords % 3) == 2){
record2Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
}else{
record3Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
}
oldTime = secondsElapsed;
}else{
timeUp();
}
}else{
reset();
}
});
}
public void timeUp(){
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Time is up... No more records...");
alert.setTitle("Time is up!!!");
alert.setHeaderText("Alert");
alert.show();
}
public void reset(){
secondsElapsed = 0.0;
timerSecondsLeft = timerStartTime;
handImageView.setRotate(0.0);
funcButton.setText("Record");
digitalLabel.setText("00:00:00");
timerLabel.setText("Timer: " + String.valueOf(timerSecondsLeft) + ".00");
championLabel.setText("Champion: 00:00.00");
record1Label.setText("Rec 00 +00:00.00");
record2Label.setText("Rec 00 +00:00.00");
record3Label.setText("Rec 00 +00:00.00");
timeUpTag = 0;
numberOfRecords = 0;
oldTime = 0.00;
}
public boolean isRunning(){
if(timeline != null){
if(timeline.getStatus() == Animation.Status.RUNNING){
return true;
}
}
return false;
}
public void update(){
secondsElapsed += tickTimeInSeconds;
double rotation = secondsElapsed * angleDeltaPerSeconds;
handImageView.setRotate(rotation);
digitalLabel.setText(getMessage(secondsElapsed));
timerSecondsLeft -= tickTimeInSeconds;
if(timerSecondsLeft <= 0.0 && timeUpTag == 0){
timerLabel.setText("Time's Up!");
timeUpTag = 1;
}else if(timeUpTag == 0){
timerLabel.setText("Timer: " + dfTimer.format(timerSecondsLeft));
}
}
public String getMessage(double secondsElapsed){
timeInfo = (String.format("%02d:", (int)secondsElapsed/60) +
String.format("%02d.", (int)secondsElapsed%60) +
String.format("%02d", (int)((secondsElapsed*100)%100)));
return timeInfo;
}
public Double getWidth(){
if(dialImage != null)
return dialImage.getWidth();
else
return 0.0;
}
public Double getHeight(){
if(dialImage != null)
return dialImage.getHeight();
else
return 0.0;
}
public Parent getRootGrid(){
return grid;
}
}
StopWatch.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StopWatch extends Application {
@Override
public void start(Stage primaryStage) {
TimerStopWatch timerStopWatch = new TimerStopWatch();
Scene scene = new Scene(timerStopWatch.getRootGrid(),
timerStopWatch.getWidth() + 200,
timerStopWatch.getHeight() + 220);
primaryStage.setTitle("Timer Stopwatch");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Result
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: stopwatch/TimerStopWatch
at stopwatch.StopWatch.start(StopWatch.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.ClassNotFoundException: stopwatch.TimerStopWatch
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more
您的应用程序使用已知的映像工作,如下所示。要确定代码中的故障,请检查图像的错误属性,如下图所示。可以在此处详细了解资源路径。下面的任何一种方法都会产生所示的结果;不正确的拨号ImageName
会重现问题中显示的异常。
// load from web
private Image dialImage = new Image("https://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png", 400, 100, false, false);
private Image handImage = new Image("https://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png", 100, 25, false, false);
// load from local file
private Image dialImage = new Image(getClass().getResourceAsStream(dialImageName), 400, 100, false, false);
private Image handImage = new Image(getClass().getResourceAsStream(dialImageName), 100, 25, false, false);
…
analogContainer.setAlignment(Pos.TOP_CENTER);
经测试:
import java.text.DecimalFormat;
import java.util.Optional;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StopWatch extends Application {
class TimerStopWatch {
private ImageView dialImageView = new ImageView();
private ImageView handImageView = new ImageView();
private String dialImageName = "clockface.png";
private String handImageName = "hand.png";
private Image dialImage = new Image("https://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png", 400, 100, false, false);
private Image handImage = new Image("https://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png", 100, 25, false, false);
private StackPane analogContainer = new StackPane();
private Text digitalLabel = new Text();
private Text timerLabel = new Text();
private Label championLabel = new Label();
private VBox timeInfoBox = new VBox();
private Label lapTimeLabel = new Label();
private Text record1Label = new Text();
private Text record2Label = new Text();
private Text record3Label = new Text();
private VBox recInfoBox = new VBox();
private HBox infoBox = new HBox();
private Button funcButton = new Button("Record");
private Button recButton = new Button("Start");
private HBox buttonsBox = new HBox();
private GridPane grid = new GridPane();
private double tickTimeInSeconds = 0.01;
private double angleDeltaPerSeconds = 6.0;
double secondsElapsed = 0;
private Timeline timeline;
private KeyFrame keyFrame;
private String timeInfo = "00:00.00";
private int timerStartTime = 60;
private double timerSecondsLeft = 60.00;
private int timeUpTag = 0;
private Alert notNumbersAlert = new Alert(Alert.AlertType.ERROR, "Insert only integers!", ButtonType.CLOSE);
private DecimalFormat dfTimer = new DecimalFormat("#.00");
private DecimalFormat dfRec = new DecimalFormat("00");
private double oldTime = 0.00;
private int numberOfRecords = 0;
public TimerStopWatch() {
setupTimer();
setupUI();
setupTimeline();
setupButtonAction();
}
private void setupTimer() {
TextInputDialog dialog = new TextInputDialog("");
dialog.setTitle("Timer Start Time Set Up");
dialog.setHeaderText("Set up the start time:");
dialog.setContentText("Please set up the start time (Integer):");
boolean legalTime = false;
while (!legalTime) {
Optional<String> input = dialog.showAndWait();
if (input.isPresent()) {
if ((input.get().matches("[0-9]+") && input.get().length() > 0) == false) {
notNumbersAlert.showAndWait();
} else {
timerStartTime = Integer.valueOf(input.get());
legalTime = true;
}
}
}
timerSecondsLeft = timerStartTime;
}
private void setupUI() {
dialImageView.setImage(dialImage);
handImageView.setImage(handImage);
analogContainer.getChildren().addAll(dialImageView, handImageView);
analogContainer.setAlignment(Pos.TOP_CENTER);
digitalLabel.setText(timeInfo);
digitalLabel.setFont(Font.font("Verdana", 30));
timerLabel.setText("Timer: " + String.valueOf(timerSecondsLeft) + ".00");
timerLabel.setFont(Font.font("Verdana", 20));
championLabel.setText("Champion: 00:00:00");
championLabel.setFont(Font.font("Verdana", 20));
lapTimeLabel.setText("Lap Time");
lapTimeLabel.setFont(Font.font("Verdana", 20));
record1Label.setText("Rec 00 +00:00.00");
record1Label.setFont(Font.font("Verdana", 18));
record2Label.setText("Rec 00 +00:00.00");
record2Label.setFont(Font.font("Verdana", 18));
record3Label.setText("Rec 00 +00:00.00");
record3Label.setFont(Font.font("Verdana", 18));
infoBox.setAlignment(Pos.CENTER);
infoBox.setSpacing(20);
infoBox.getChildren().addAll(timeInfoBox, recInfoBox);
timeInfoBox.setAlignment(Pos.CENTER);
timeInfoBox.setSpacing(10);
timeInfoBox.getChildren().addAll(digitalLabel, timerLabel, championLabel);
recInfoBox.setAlignment(Pos.CENTER);
recInfoBox.setSpacing(5);
recInfoBox.getChildren().addAll(lapTimeLabel, record1Label, record2Label, record3Label);
buttonsBox.setAlignment(Pos.CENTER);
buttonsBox.setSpacing(10);
buttonsBox.getChildren().addAll(funcButton, recButton);
grid.setVgap(30);
grid.setHgap(10);
grid.setAlignment(Pos.CENTER);
grid.add(analogContainer, 0, 0);
grid.add(infoBox, 0, 1);
grid.add(buttonsBox, 0, 2);
}
private void setupTimeline() {
if (isRunning()) {
timeline.stop();
}
keyFrame = new KeyFrame(Duration.millis(tickTimeInSeconds * 1000), (ActionEvent event) -> {
update();
});
timeline = new Timeline(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
}
private void setupButtonAction() {
recButton.setOnAction((ActionEvent event) -> {
if (timeline.getStatus() != Animation.Status.RUNNING) {
timeline.play();
recButton.setText("Stop");
funcButton.setText("Record");
} else {
timeline.stop();
recButton.setText("Start");
funcButton.setText("Reset");
}
});
funcButton.setOnAction((ActionEvent event) -> {
if (timeline.getStatus() == Animation.Status.RUNNING) {
if (timerLabel.getText() != "Time's Up!") {
numberOfRecords++;
if (numberOfRecords == 1) {
championLabel.setText("Champion: " + getMessage(secondsElapsed));
}
if ((numberOfRecords % 3) == 1) {
record1Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
} else if ((numberOfRecords % 3) == 2) {
record2Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
} else {
record3Label.setText("Rec " + dfRec.format(numberOfRecords) + " +" + getMessage(secondsElapsed - oldTime));
}
oldTime = secondsElapsed;
} else {
timeUp();
}
} else {
reset();
}
});
}
public void timeUp() {
Alert alert = new Alert(Alert.AlertType.INFORMATION, "Time is up... No more records...");
alert.setTitle("Time is up!!!");
alert.setHeaderText("Alert");
alert.show();
}
public void reset() {
secondsElapsed = 0.0;
timerSecondsLeft = timerStartTime;
handImageView.setRotate(0.0);
funcButton.setText("Record");
digitalLabel.setText("00:00:00");
timerLabel.setText("Timer: " + String.valueOf(timerSecondsLeft) + ".00");
championLabel.setText("Champion: 00:00.00");
record1Label.setText("Rec 00 +00:00.00");
record2Label.setText("Rec 00 +00:00.00");
record3Label.setText("Rec 00 +00:00.00");
timeUpTag = 0;
numberOfRecords = 0;
oldTime = 0.00;
}
public boolean isRunning() {
if (timeline != null) {
if (timeline.getStatus() == Animation.Status.RUNNING) {
return true;
}
}
return false;
}
public void update() {
secondsElapsed += tickTimeInSeconds;
double rotation = secondsElapsed * angleDeltaPerSeconds;
handImageView.setRotate(rotation);
digitalLabel.setText(getMessage(secondsElapsed));
timerSecondsLeft -= tickTimeInSeconds;
if (timerSecondsLeft <= 0.0 && timeUpTag == 0) {
timerLabel.setText("Time's Up!");
timeUpTag = 1;
} else if (timeUpTag == 0) {
timerLabel.setText("Timer: " + dfTimer.format(timerSecondsLeft));
}
}
public String getMessage(double secondsElapsed) {
timeInfo = (String.format("%02d:", (int) secondsElapsed / 60)
+ String.format("%02d.", (int) secondsElapsed % 60)
+ String.format("%02d", (int) ((secondsElapsed * 100) % 100)));
return timeInfo;
}
public Double getWidth() {
System.out.println(dialImage.getWidth());
if (dialImage != null) {
return dialImage.getWidth();
} else {
return 0.0;
}
}
public Double getHeight() {
System.out.println(dialImage.getHeight());
if (dialImage != null) {
return dialImage.getHeight();
} else {
return 0.0;
}
}
public Parent getRootGrid() {
return grid;
}
}
@Override
public void start(Stage primaryStage) {
TimerStopWatch timerStopWatch = new TimerStopWatch();
Scene scene = new Scene(timerStopWatch.getRootGrid(),
timerStopWatch.getWidth() + 200,
timerStopWatch.getHeight() + 220);
primaryStage.setTitle("Timer Stopwatch");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
问题内容: 我只是从JavaFX开始,我正在尝试构建一个带有标签,文本字段和按钮的简单应用程序,单击该按钮即可将标签的值设置为文本字段的值。一切顺利,直到我将控制器连接到Main文件。这是我的代码: Main.java MainWindowView.fxml MainWindowController.java 我已经尝试过在StackOverflow上找到的多个答案,但是我发现的所有答案都是两年前
FXML 错误 应用程序启动方法java.lang.Reflect.InvocationTargetException位于Sun.Reflect.NativeMethodAccessorImpl.Invoke0(本机方法)位于Sun.Reflect.NativeMethodAccessorImpl.Invoke(NativeMethodAccessorImpl.Invoke)(nativeMeth
我的主 LoginController.java 请注意,我还没有在userController.java中编写任何代码,我只是为user.fxml编写了ui 在javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2597)在javafx.fxml.fxmlloader.access$100(fxmlloader.java:1
乍一看,这个问题似乎是重复的。我已经在谷歌搜索了一些,但不幸的是,没有一个结果不符合我。我给出了下面的问题链接。 应用程序启动方法java.lang.Reflect.InvocationTargetException JavaFX图像转换中出现异常 JavaFX-应用程序启动方法中的异常? 应用程序启动方法中出现异常 堆栈跟踪: 无法从此StackTrace跟踪错误。然后我在start方法中使用了
我刚刚开始使用JavaFX,我试图构建一个简单的应用程序,其中包含标签、文本字段和按钮,当单击这些按钮时,将标签的值设置为文本字段的值。在我把控制器连接到主文件之前,一切都很顺利。这是我的代码: 我尝试了在StackOverflow上找到的多个答案,但我找到的都是2年前的答案,对我的代码没有任何积极的影响。 编辑:在此处堆栈跟踪:
堆栈跟踪 我试过关于这个话题的所有解答,但我总是得到同样的错误。