首页 > 解决方案 > 保存输入数据 JavaFX

问题描述

我是编程新手,正在上 OOP 课程。我正在尝试提出一个 JavaFX 应用程序,它可以保存情绪输入以便能够收集数据。截至目前,数据必须在控制台命令窗口中,但在添加新输入时会被替换。任何方向将不胜感激。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class comboBoxPTSD extends Application {

    Stage window;
    Scene scene;
    Button button;
    ComboBox<String> comboBox;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("PTSD Tracker App");
        button = new Button("Submit");

        comboBox = new ComboBox<>();
        comboBox.getItems().addAll(
                "Happy", "Content", "Good", "Sad", "Angry", 
            "Hopeless", "Suicidal");

        //sets combo boxes text name. 
        comboBox.setPromptText("How are you doing today?");
        comboBox.setEditable(true);
        /*
         * can set the comboBox to input user input with 
        *"comboBox.setEditable(true);"
        *will override box name. 
        *want to save input and output...
        *
        *use variable string.
        */
        button.setOnAction(e -> printMood());

        //ComboBoxes also generate actions if you need to get value instantly
        comboBox.setOnAction( e -> System.out.println("User selected " + comboBox.getValue()) );

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20, 20, 20, 20));
        layout.getChildren().addAll(comboBox, button);

        scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();
    }

    private void printMood(){
        System.out.println(comboBox.getValue());
    }


}

应用程序 JavaFX

控制台图片

标签: javajavafx

解决方案


推荐阅读