我使用Eclipse创建了一个简单的UI,没有像往常一样的错误迹象,我在教程中尽可能地遵循代码。虽然它没有显示任何错误,但它仍然无法运行,并自动打开调试面板,并说“应用程序启动方法中的异常。”
以下是我的代码:
package groupProject;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Try extends Application
{
Stage window;
Scene newUserScene;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
window = primaryStage;
window.setTitle("Automated Teller Machine");
GridPane nUserGrid = new GridPane();
nUserGrid.setPadding(new Insets(10,10,10,10));
nUserGrid.setVgap(8);
nUserGrid.setHgap(10);
//New matric card - Label 1
Label newMatricLabel = new Label("Matric Number: ");
GridPane.setConstraints(newMatricLabel,0,0);//Set Position
TextField newMatricInput = new TextField();
GridPane.setConstraints(newMatricInput,1,0);//Set position
//New user name - Label 2
Label newUsername = new Label("Your name: ");
GridPane.setConstraints(newUsername,0,1);//Set position
TextField newUsernameInput = new TextField();
GridPane.setConstraints(newUsernameInput,1,1);//Set Position
//Pin number - Label 3
Label newPin = new Label("Your pin: ");
GridPane.setConstraints(newPin,0,2);//Set position
TextField newPinInput = new TextField();
GridPane.setConstraints(newPinInput,1,2);//Set position
//Deposit - Label 4
Label newDepositValue = new Label("Deposit: ");
GridPane.setConstraints(newDepositValue,0,3);//Set position
TextField newDepositInput = new TextField();
GridPane.setConstraints(newDepositInput,1,1);//Set position
//Create submit button---//
Button submitEntree = new Button("Submit");
GridPane.setConstraints(submitEntree,1,4);//Set Position
nUserGrid.getChildren().addAll(newMatricLabel,newMatricInput,newMatricInput,newUsername,newUsernameInput,newPin,newPinInput,newDepositValue,newDepositInput,submitEntree);//add all to GridPane
newUserScene = new Scene(nUserGrid,500,500);
window.setScene(newUserScene);
window.show();
}
}
调试透视图上显示的完整消息是:
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=10.0, vgap=8.0, alignment=TOP_LEFT
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
at groupProject.Try.start(Try.java:61)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Exception running application groupProject.Try
此时此按钮和用户输入没有与之相关的任何操作,但将来也会这样做。
错误的原因是什么?如何解决此类错误,以便它可以运行并显示我所创建的用户界面?提前谢谢。
答案 0 :(得分:2)
在此代码中:
nUserGrid.getChildren().addAll(newMatricLabel,newMatricInput,newMatricInput,newUsername,newUsernameInput,newPin,newPinInput,newDepositValue,newDepositInput,submitEntree);//add all to GridPane
您添加newMatricInput
两次。删除一个。