2017-10-09 130 views
-2

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

} 

} 
+0

讓我明白這一點:您將整個TextField寫入文本文件?那麼這不會做到這一點。僅將字段的文本用於此類任務。 –

+0

是的,這是我使用getText()方法,以便我可以使用裏面的文本,但它會引起我的錯誤 –

+0

請一個最小的runnable程序,我們可以嘗試我們的自我而不缺少方法或自定義類,不僅上面的代碼是不完整的,而且你有錯誤,比如你用參數調用的構造函數或RulesPane(Stage,Scene,String),它的定義是(Stage,Scene,Pane),另外我不知道RulesScene是關於什麼的。試着做一個小例子來重現你的問題,同時你可能會發現錯誤的轉換。 – JKostikiadis

回答

0

未來既然不能看到那裏的窗格究竟來自哪裏,我會承擔當你說你在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(); 
      } 
      } 
     } 
    } 

而不是你爲它試圖使一個窗格成文本字段的試塊循環,這會得到窗格中的孩子,假設他們VBOX(如果是這樣,)然後得到VBox的孩子們,假設他們是TextFields(如果他們是)

如果這樣做不行,我會建議您發佈其餘的代碼(Pane是從哪裏來的),以及究竟是什麼錯誤拋出。

+0

我正在通過的窗格將是一個VBox。其餘的代碼是不需要的,因爲我嘗試在VBox和TextFields實例化的類中進行測試,並引發相同的錯誤。 –

+0

不要說你應該把它放在代碼的其他部分,爲了理解這裏發生了什麼(你試圖使用的面板裏面有什麼),看看它來自哪裏會很有幫助。 –

+0

好吧我加了 –