在学校时,我的任务是创建一个 JavaFX 表单 GUI
,在其中我可以编写一些字符串,例如 name
、eMail
...
首先,我应该创建一个类 Person
,其中包含数据字段。
应该有一个类AdressDB
,我在其中创建一个关于Person
的列表
在GUI
类中,我应该创建一个可用于将“Persons”添加到列表中的表单。例如使用按钮。
“创建一个类 AdressDB
。该类保留一个 Person
列表。Person
包含典型的数据字段,例如:eMail
、Name
...。在 GUI 类中,您可以实现一个表单,可用于将“Persons”添加到列表中。”
我已经填写了表格。现在我的问题是:如何将表单中的数据添加到 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);
}
}
最佳答案
在submitButton.setOnAction的handle方法中,您需要读取字段来创建一个新的Person。这个新的 Person 可以添加到 ArrayList 中。
只要您在定义 TextField 的同一方法中实现按钮 setOnAction 方法,效果就很好。但是,一旦您将按钮 setOnAction 实现移至另一个方法,您就需要将 TextField 定义为类“变量”(字段!)来访问它们。
关于java - 将 Java FX 表单数据添加到 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59955600/