我正在尝试为我的Java类做一个应用程序。主要是我们正在学习使用github,但我的问题来自JavaFX。我想在几个窗格和场景上创建一个表单,当用户点击提交时,最后一个表格应该放在文件中。
我的想法是将文本字段传递到最后一个窗格有一个参数,并在按下提交按钮后创建一个缓冲区,将所有文本添加到一个新文件中。所以这个想法很完美,我通过了一个Pane,因为所有的textFields都在一个VBox里面,当我写它们时,我把它们转换为textFields,但是当我运行它时告诉我文本不能转换为textfield,所以当我尝试转而投入文字,它会告诉我相反的情况!那个textfield不能转换为文本!我在代码中使用textArea尝试了缓冲区,它工作正常!我不明白为什么不通过我传递的窗格
注意:最后我尝试连接到一个字符串,看看缓冲区是否有问题,但即使这样做也告诉我同样的错误
这是代码:
public class RulesPane extends VBox{
public RulesPane(Stage mainStage, Scene mainScene, Pane pane) {
this.setAlignment(Pos.TOP_CENTER);
BackgroundImage background = new BackgroundImage(new Image("water.png",900,600,false,true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
this.setBackground(new Background(background));
File logoPath = new File("src/swimclub_Logo.png");
Image logoImage = new Image(logoPath.toURI().toString());
ImageView logo = new ImageView(logoImage);
TextArea agreement = new TextArea();
agreement.setEditable(false);
agreement.setPrefHeight(400);
agreement.setMaxWidth(600);
agreement.setWrapText(true);
agreement.setText("This is text");
CheckBox agree = new CheckBox("I Agree");
HBox buttons = new HBox();
buttons.setPadding(new Insets(50, 0, 0, 60));
buttons.setSpacing(300);
Button back = new Button();
back.setText("Back");
back.setOnMouseClicked(e->{
mainStage.setScene(mainScene);
});
Button exit = new Button();
exit.setText("Exit");
exit.setOnMouseClicked(e->{
Platform.exit();
});
Button submit = new Button();
submit.setText("Submit Form");
submit.setDisable(true);
submit.setOnMouseClicked(e->{
File file = new File("user_info.txt");
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
String contentToSave = "";
for(int i = 0; i < pane.getChildren().size(); i++)
contentToSave += ( (TextField) pane.getChildren().get(i)).getText();
out.write(contentToSave);
out.flush();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
});
//CheckBox event handler to make the submit button available or unavailable when pressed
agree.setOnAction(e->{
if(agree.isSelected())
submit.setDisable(false);
else
submit.setDisable(true);
});
buttons.getChildren().addAll(back, exit, submit);
this.getChildren().addAll(logo , agreement, agree, buttons);
}
}
这是来自
的地方 public class registration extends Application{
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) throws Exception {
BorderPane main = new BorderPane();
VBox questions = new VBox();
VBox titlePane = new VBox();
HBox buttonPane = new HBox();
Text title = new Text("Welcome to the Swimming Club Registration!");
Text question1 = new Text("How did you hear about our club?");
Text question2 = new Text("Have you ever been in any clubs for swimming before?");
Text question3 = new Text("Do you swim competitvly or for fun?");
TextField question1Answer = new TextField();
TextField question2Answer = new TextField();
TextField question3Answer = new TextField();
question1Answer.setPromptText("Enter where you heard about our club (Internet, Flyer, etc)");
question2Answer.setPromptText("Enter Yes or No");
question3Answer.setPromptText("Enter Competitvly or Fun");
Button cancelButton = new Button("Cancel");
Button continueButton = new Button("Continue");
File logoPath = new File("src/swimclub_Logo.png");
Image logoImage = new Image(logoPath.toURI().toString());
ImageView logo = new ImageView(logoImage);
buttonPane.getChildren().addAll(cancelButton, continueButton);
titlePane.getChildren().addAll(title, logo);
questions.getChildren().addAll(question1,question1Answer,question2,question2Answer,question3,question3Answer);
titlePane.setAlignment(Pos.CENTER);
buttonPane.setAlignment(Pos.CENTER);
titlePane.setPadding(new Insets(10,0,20,0));
questions.setPadding(new Insets(0,10,0,10));
buttonPane.setPadding(new Insets(0,0,10,0));
main.setTop(titlePane);
main.setCenter(questions);
main.setBottom(buttonPane);
Scene scene = new Scene(main, 900, 600);
primaryStage.setScene(scene);
primaryStage.show();
/**
* @josegeorges
* here I am adding the rules pane with the rules scene and setting up the
* continueButton to go to it for now
* I am also saving the textFields into a string and passing the content to the pane
*/
continueButton.setOnMouseClicked(e->{
String contentToSave = "";
contentToSave += question1Answer.getText() + ",";
contentToSave += question2Answer.getText() + ",";
contentToSave += question3Answer.getText() + ",";
RulesPane rulesPane = new RulesPane(primaryStage, scene, contentToSave);
RulesScene rulesScene = new RulesScene(rulesPane);
primaryStage.setScene(rulesScene);
});
}
}
答案 0 :(得分:0)
由于我无法看到Pane的确切来源,我会假设当你说你在VBox中有TextField时,那个VBox就在那个窗格里面。如果是这样,问题似乎是你将TextFields转换为Pane,而不是TextFields转换为Pane的孩子,如果它在VBox中你想要得到它的话。 (无论如何)(Vbox的)孩子们。所以:
for (int i = 0; i < pane.getChildren().size(); i++) {
Node child1 = group.getChildren().get(i);
if (child1 instanceof VBox) {
for(Node child2:((VBox) child1).getChildren()) {
if (child2 instanceof TextField) {
contentToSave += (TextField) child2.getText();
}
}
}
}
而不是尝试将窗格设置为TextField的try-block中的for循环,这将获得窗格的子项,假设它们是vbox(如果它们是),然后获取VBox的子项如果(如果是的话),则假设他们是TextFields。
如果没有这样做,我建议您发布剩下的代码(Pane来自哪里),以及它正在抛出的错误。