JavaFX TextField to File

时间:2015-04-26 17:32:47

标签: java file javafx javafx-2

亲爱的Stackoverflow Java爱好者

我尝试在JavaFX中创建一个小应用程序,它在textField中获取用户输入,单击按钮并将其写入.txt文件。

这里的问题是:程序实际上工作正常并打印在.txt文件的用户输入中,但它只在我关闭程序后出现。

我可能认为是按钮Eventhandler有问题。

希望你们中的一些人能够得到一个简单的解决方案。

public class TextFileOPG extends Application {

private Stage switchStage;
private static TextField text1 = new TextField();
private TextField text2 = new TextField();
private Button implFileBtn = new Button("CATCH ME FILE!");

@Override
public void start(Stage primaryStage) {
    try {

            switchStage = primaryStage;

            GridPane root = new GridPane();

            root.setAlignment(Pos.CENTER);

            Text scenetitle = new Text("Welcome");
            scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 40));
            root.add(scenetitle, 3, 2);

            primaryStage.setTitle("Welcome");
            Scene scene = new Scene(root,350,400);
            primaryStage.setScene(scene);
            primaryStage.show();

            Timeline tick0 = new Timeline();
            tick0.setCycleCount(Timeline.INDEFINITE);
            tick0.getKeyFrames().add(
                    new KeyFrame(new Duration(30), new EventHandler<ActionEvent>() {
                        public void handle(ActionEvent t) {
                            root.setOpacity(root.getOpacity()-0.01);
                            if(root.getOpacity()<0.01){//30divided by 0.01 equals 3000 so you take the duration and divide it be the opacity to get your stansition.
                                loggedIn();
                                tick0.stop();
                            }
                        }}));
            tick0.play();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void loggedIn() 
{
    switchStage.setTitle("Try");

    GridPane grid = new GridPane();

    grid.setAlignment(Pos.CENTER);

    text1.setText("");
    text1.setAlignment(Pos.CENTER);
    Text thisIsSoCoolText = new Text("Enter a text to the file");
    thisIsSoCoolText.setFont(Font.font("Tahoma", FontWeight.NORMAL, 28));
    grid.add(thisIsSoCoolText, 0, 0);


    grid.add(text1, 0, 1);

    HBox hbox = new HBox();

    hbox.getChildren().add(implFileBtn);
    hbox.setAlignment(Pos.CENTER);
    grid.add(hbox, 0, 2);

    implFileBtn.setOnAction(e -> text1.setText(text1.getText()));


    Scene scene = new Scene(grid, 350, 400);
    switchStage.setScene(scene);
    switchStage.show();
}

public static void main(String[] args) {
    launch(args);

    try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true))))
    {
       out.println(text1.getText());
    }
    catch (IOException e)
    {
       //TO-DO
    }
}
}

此致 亚历

1 个答案:

答案 0 :(得分:0)

您只能在主UI线程完成后写入文件。移动按钮EventHandler

内的功能
implFileBtn.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent e) {
        try (PrintWriter out = new PrintWriter(new BufferedWriter(
                new FileWriter("myfile.txt", true)))) {
            out.println(text1.getText());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
});