我想爲我的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