【问题标题】:Add Java FX form data to ArrayList将 Java FX 表单数据添加到 ArrayList
【发布时间】:2020-05-14 06:32:48
【问题描述】:

在学校,我的任务是创建一个 JavaFX 表单 GUI,我可以在其中编写一些字符串,例如 nameeMail...

首先我应该创建一个类Person,数据字段位于其中。

应该有一个类AdressDB,我在其中创建了一个关于Person的列表

GUI 类中,我应该创建一个可用于将“Persons”添加到列表的表单。例如使用按钮。

"创建一个类AdressDB。该类保存Person的列表。Person保存典型的数据字段如:eMail,Name,...。在GUI类中您实现了一个表单,您可以使用该表单将“人员”添加到列表中。”

我已经完成了表格。现在我的问题是:如何将表单中的数据添加到 ArrayList

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
import java.util.ArrayList;
import javafx.scene.text.Text;
import javafx.scene.text.*;

public class GUI extends Application {
    private TextField fieldName, fieldActor;
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Registration Form JavaFX Application");

        GridPane gridPane = createRegistrationFormPane();
        addUIControls(gridPane);
        Scene scene = new Scene(gridPane, 800, 500);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private GridPane createRegistrationFormPane() {
        // Instantiate a new Grid Pane
        GridPane gridPane = new GridPane();

        // Position the pane at the center of the screen, both vertically and horizontally
        gridPane.setAlignment(Pos.CENTER);

        // Set a padding of 20px on each side
        gridPane.setPadding(new Insets(40, 40, 40, 40));

        // Set the horizontal gap between columns
        gridPane.setHgap(10);

        // Set the vertical gap between rows
        gridPane.setVgap(10);

        // Add Column Constraints

        // columnOneConstraints will be applied to all the nodes placed in column one.
        ColumnConstraints columnOneConstraints = new ColumnConstraints(100, 100, Double.MAX_VALUE);
        columnOneConstraints.setHalignment(HPos.RIGHT);

        // columnTwoConstraints will be applied to all the nodes placed in column two.
        ColumnConstraints columnTwoConstrains = new ColumnConstraints(200,200, Double.MAX_VALUE);
        columnTwoConstrains.setHgrow(Priority.ALWAYS);

        gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);

        return gridPane;
    }

    private void addUIControls(GridPane gridPane) {

        Label headerLabel = new Label("Bitte geben sie Ihre Daten ein");
        headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
        gridPane.add(headerLabel, 0,0,2,1);
        GridPane.setHalignment(headerLabel, HPos.CENTER);
        GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));

        // Add Name Label
        Label nameLabel = new Label("Name : ");
        gridPane.add(nameLabel, 0,1);

        // Add Name Text Field
        TextField name = new TextField();
        name.setPrefHeight(40);
        gridPane.add(name, 1,1);

        // Add Email Label
        Label emailLabel = new Label("E-Mail: ");
        gridPane.add(emailLabel, 0, 2);

        // Add Email Text Field
        TextField email = new TextField();
        email.setPrefHeight(40);
        gridPane.add(email, 1, 2);

        // Add Birthday Label
        Label Geburtsdatum = new Label("Geburtsdatum : ");
        gridPane.add(Geburtsdatum, 0, 3);

        // Add Birhday Field
        TextField geburtsdatum = new TextField();
        geburtsdatum.setPrefHeight(40);
        gridPane.add(geburtsdatum, 1, 3);
        geburtsdatum.setText("Geburtsdatum");


        // Add Submit Button
        Button submitButton = new Button("Submit");
        submitButton.setPrefHeight(40);
        submitButton.setDefaultButton(true);
        submitButton.setPrefWidth(100);
        gridPane.add(submitButton, 0, 4, 2, 1);
        GridPane.setHalignment(submitButton, HPos.CENTER);
        GridPane.setMargin(submitButton, new Insets(20, 0,20,0));





        submitButton.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {

        ;







                    if(name.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your name");
                        return;
                    }
                    if(email.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter your email id");
                        return;
                    }
                    if(geburtsdatum.getText().isEmpty()) {
                        showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Form Error!", "Please enter a password");
                        return;
                    }

                    showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Wir haben ihre Daten erhalten!", "Vielen dank für ihr Vertrauen " + name.getText());





                }
            });

    }



        private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
            Alert alert = new Alert(alertType);
            alert.setTitle(title);
            alert.setHeaderText(null);
            alert.setContentText(message);
            alert.initOwner(owner);
            alert.show();
        }





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


}

【问题讨论】:

  • 你所有的 TextFields 都应该作为私有字段存储在你的类中。例如,private TextField name;private TextField email; 等等。然后您的操作处理程序将可以访问每个 TextField 的 getText() 方法。
  • 答案取决于类AdressDB的实现。在submitButton.setOnAction 中,它可能看起来像adressDB.addPerson(new Person(name.getText(), email.getText(), geburtsdatum.getText()));

标签: java forms list javafx arraylist


【解决方案1】:

在 submitButton.setOnAction 的句柄方法中,您需要读取字段以创建新的 Person。这个新的 Person 可以添加到 ArrayList 中。

只要您在定义 TextField 的同一方法中实现按钮的 setOnAction 方法,就可以很好地工作。但是,只要将按钮 setOnAction 实现移动到另一个方法,就需要将 TextFields 定义为类“变量”(Fields!)来访问它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多