我知道在这个论坛上发布了很多问题,但是我仍然无法追踪我的程序的错误。
我的程序的功能与以下发布的代码完全不同,但我只是删除了不必显示的代码。我只是包含了gui代码,我相信它是导致错误的代码。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application{
Label username;
Label password;
Button submit;
TextField user;
PasswordField pass;
Label numObj;
Label tri;
TextField numOpt;
Button sub;
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Authentication");
GridPane layout = new GridPane();
layout.setPadding(new Insets(10,10,10,10));
layout.setAlignment(Pos.CENTER);
layout.setVgap(8);
layout.setHgap(10);
Scene scene = new Scene(layout, 400,200);
primaryStage.setScene(scene);
primaryStage.show();
username = new Label("username: ");
GridPane.setConstraints(username, 0,0);
password = new Label("password: ");
GridPane.setConstraints(password, 0,1);
submit = new Button("submit");
GridPane.setConstraints(submit, 1,2);
user = new TextField();
GridPane.setConstraints(user, 1,0);
pass = new PasswordField();
GridPane.setConstraints(pass, 1,1);
layout.getChildren().addAll(username,password,submit,user,pass);
BorderPane layout2 = new BorderPane();
layout2.setPrefSize(500, 350);
layout2.setPadding(new Insets(10,10,10,10));
Scene secondScene = new Scene(layout2);
submit.setOnAction(e -> {
primaryStage.setScene(secondScene);
});
HBox atTop = new HBox();
numObj = new Label("Number of Object/s: ");
numOpt = new TextField("5");
sub = new Button("submit");
atTop.getChildren().addAll(numObj, numOpt, sub);
layout2.getChildren().add(atTop);
layout2.setTop(atTop);
}
}
并显示错误
Exception in Application start 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 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(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane@5ac4cb88[styleClass=root]
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at javafx.scene.layout.BorderPane$BorderPositionProperty.invalidated(BorderPane.java:680)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.layout.BorderPane.setTop(BorderPane.java:278)
at Main.start(Main.java:74)
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
Exception running application Main
答案 0 :(得分:1)
删除此“ layout2.getChildren()。add(atTop);