我正在使用 JavaFX 开发一个程序,我在代码中没有看到任何错误(没有下划线),但是当我使用单独的文件运行代码时,出现以下错误:
这是完整的堆栈跟踪:
Exception in Application init method
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(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(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: Exception in Application init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at website.CreateAccountPane.<init>(CreateAccountPane.java:60)
at website.WebsiteRootPane.<init>(WebsiteRootPane.java:22)
at main.ApplicationLoader.init(ApplicationLoader.java:14)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Exception running application main.ApplicationLoader
这是我的应用程序启动器的代码:
package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import website.WebsiteRootPane;
public class ApplicationLoader extends Application {
private WebsiteRootPane root;
@Override
public void init() {
root = new WebsiteRootPane();
}
@Override
public void start(Stage stage) throws Exception {
stage.setMinWidth(1000);
stage.setMinHeight(750);
stage.setTitle("Planet Generator");
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我网站的选项卡 Pane 之一,到目前为止,它是我构建的唯一一个。
package website;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class CreateAccountPane extends GridPane {
private ComboBox<String> cboTitle;
private TextField txtSurname, txtFirstName, txtEmail;
private Button btnCreate;
private PasswordField pwrd;
public CreateAccountPane() {
//create labels
Label lblTitle = new Label("Title");
Label lblFirstName = new Label("First name");
Label lblSurname = new Label("Surname");
Label lblEmail = new Label("Email");
Label lblPassword = new Label("Password");
// setup combobox
ObservableList<String> titles = FXCollections.observableArrayList("Mr", "Mrs", "Miss", "Ms");
cboTitle = new ComboBox<String>(titles);
cboTitle.getSelectionModel().select(0);
// setup text fields
txtFirstName = new TextField();
txtSurname = new TextField();
txtEmail = new TextField();
pwrd = new PasswordField();
this.add(lblTitle, 0, 0);
this.add(cboTitle, 1, 0);
this.add(lblFirstName, 0, 1);
this.add(txtFirstName, 1, 1);
this.add(lblSurname, 0, 2);
this.add(txtSurname, 1, 2);
this.add(lblEmail, 0, 3);
this.add(txtEmail, 1, 3);
this.add(lblPassword, 0, 4);
this.add(pwrd, 1, 4);
this.getChildren().add(txtFirstName);
}
}
最佳答案
问题
您添加 txtFirstName
两次,但只能向 UI 添加一个元素一次。
你添加它
this.add(txtFirstName, 1, 1);
和
this.getChildren().add(txtFirstName);
您应该能够通过删除来修复它
this.getChildren().add(txtFirstName);
如何找到它(阅读它,它会对您有帮助:))
看看你的异常:
Exception in Application init method java.lang.reflect.InvocationTargetException
这表示执行init
代码时出现问题。
InvokedTargetException
表示使用反射调用的方法(init 的情况如何,因为 JavaFX 为此使用反射)引发了异常。这可能是任何异常(exception)。堆栈跟踪中的 Caused by
部分显示了真正的异常。让我们看一下:
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT at javafx.scene.Parent$2.onProposedChange(Parent.java:454) at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206) at website.CreateAccountPane.(CreateAccountPane.java:60)
异常消息(Children:已添加重复子项:parent = Grid hgap=0.0,vgap=0.0,alignment=TOP_LEFT
)告诉您问题是您已经添加了该元素。
at website.CreateAccountPane.(CreateAccountPane.java:60)
告诉您它发生在 CreateAccountPane
(CreateAccountPane.java:60
) 的第 60 行。
接下来,我们仔细看看这一行:
this.getChildren().add(txtFirstName);
它实际上向网格 Pane 添加了一个元素。
所以,这个元素似乎已经是网格 Pane 的一部分了。那么,让我们看看您之前添加了哪些内容:
this.add(txtFirstName, 1, 1);
您两次添加了相同的元素。这就是问题所在。
<小时/>结论
如您所见,读取和解释堆栈跟踪是编程中非常非常重要的部分。如果继续下去,您将看到越来越多的像这样的堆栈跟踪。
不要害怕那些巨大的红色文字。将它们分开并尝试找出问题。
这可能既耗时又困难,尤其是在开始时但随着时间的推移,你会变得更好!
查找、理解和修复错误是编程中非常重要的部分(也许是最重要的部分)。
如果可以的话,这部分会容易得多
- 读取并解释异常
和
- 调试。如果您不知道什么是调试,请查找、学习并尝试。它几乎肯定会对您有所帮助。
关于java - 在Java中收到错误并且不明白问题: Exception in Application init method java. lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61127339/