我想为我的Java类做一个应用程序。主要我们正在学习使用github,但是我的问题随JavaFX一起提供。我想在几个窗格和场景上创建一个窗体,当用户点击提交时,最后一个窗体应该放在文件中。JavaFx无法将文本转换为textField,也无法将textField转换为文本?
我的想法是将textfields传递给最后一个窗格有一个参数,并在按下submit按钮时创建一个bufferedwriter,将所有文本添加到一个新文件中。所以这个想法是完美的,我通过了一个窗格,因为所有的textField都在一个VBox里面,当我写它们时我会施放textFields,但是当我运行它时告诉我文本不能转换为文本字段,所以当我尝试将其转换为文本,它会告诉我相反的情况!该文本字段不能转换为文本!我在代码中使用textArea尝试缓冲区,它工作正常!我不明白为什么不与窗格IM传递
注意工作:就在我试图串联为一个字符串是否缓冲区是问题的结束,但它告诉我同样的错误,即使做这种方式
这是代码:
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);
});
}
}
让我明白这一点:您将整个TextField写入文本文件?那么这不会做到这一点。仅将字段的文本用于此类任务。 –
是的,这是我使用getText()方法,以便我可以使用里面的文本,但它会引起我的错误 –
请一个最小的runnable程序,我们可以尝试我们的自我而不缺少方法或自定义类,不仅上面的代码是不完整的,而且你有错误,比如你用参数调用的构造函数或RulesPane(Stage,Scene,String),它的定义是(Stage,Scene,Pane),另外我不知道RulesScene是关于什么的。试着做一个小例子来重现你的问题,同时你可能会发现错误的转换。 – JKostikiadis