编写JavaFX程序,实现一个信息采集功能,主体采用BorderPane,要求: 1)Top区放置一个标签“个人信息”,并设置字体大小和颜色; 2)Bottom区放置两个按钮“提交”和“重填”; 3)Right区放置一个文本区; 4)Center区是放置信息填写的各种组件,如图所示,其中证件类型有:身份证、军人证、学生证和护照。 当点击“提交”按钮时,将填写的信息采集到Right区的文本区中;当点击“重填”按钮时,清空所有填写的内容。设计合适的舞台和场景,进行测试。
时间: 2024-02-22 13:01:07 浏览: 61
以下是一个简单的JavaFX程序,实现一个信息采集功能,满足题目要求:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class InformationCollection extends Application {
private TextField nameField;
private ComboBox<String> genderBox;
private ComboBox<String> idTypeBox;
private TextField idNumField;
private DatePicker birthDatePicker;
private TextArea infoArea;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("信息采集");
// 创建Top区域的标签
Label titleLabel = new Label("个人信息");
titleLabel.setFont(new Font(24));
titleLabel.setTextFill(javafx.scene.paint.Color.BLUE);
// 创建Center区域的表单
Label nameLabel = new Label("姓名:");
nameField = new TextField();
Label genderLabel = new Label("性别:");
genderBox = new ComboBox<>();
genderBox.getItems().addAll("男", "女");
genderBox.setPromptText("请选择性别");
Label idTypeLabel = new Label("证件类型:");
idTypeBox = new ComboBox<>();
idTypeBox.getItems().addAll("身份证", "军人证", "学生证", "护照");
idTypeBox.setPromptText("请选择证件类型");
Label idNumLabel = new Label("证件号码:");
idNumField = new TextField();
Label birthLabel = new Label("出生日期:");
birthDatePicker = new DatePicker();
GridPane centerPane = new GridPane();
centerPane.setAlignment(Pos.CENTER);
centerPane.setHgap(10);
centerPane.setVgap(10);
centerPane.setPadding(new Insets(10));
centerPane.add(nameLabel, 0, 0);
centerPane.add(nameField, 1, 0);
centerPane.add(genderLabel, 0, 1);
centerPane.add(genderBox, 1, 1);
centerPane.add(idTypeLabel, 0, 2);
centerPane.add(idTypeBox, 1, 2);
centerPane.add(idNumLabel, 0, 3);
centerPane.add(idNumField, 1, 3);
centerPane.add(birthLabel, 0, 4);
centerPane.add(birthDatePicker, 1, 4);
// 创建Bottom区域的按钮
Button submitButton = new Button("提交");
submitButton.setOnAction(event -> submit());
Button resetButton = new Button("重填");
resetButton.setOnAction(event -> reset());
HBox bottomPane = new HBox(10);
bottomPane.setAlignment(Pos.CENTER);
bottomPane.setPadding(new Insets(10));
bottomPane.getChildren().addAll(submitButton, resetButton);
// 创建Right区域的文本区
infoArea = new TextArea();
infoArea.setEditable(false);
infoArea.setWrapText(true);
// 创建BorderPane并添加各个区域
BorderPane root = new BorderPane();
root.setTop(titleLabel);
root.setCenter(centerPane);
root.setBottom(bottomPane);
root.setRight(infoArea);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private void submit() {
String name = nameField.getText();
String gender = genderBox.getValue();
String idType = idTypeBox.getValue();
String idNum = idNumField.getText();
String birth = birthDatePicker.getValue().toString();
StringBuilder sb = new StringBuilder();
sb.append("姓名:").append(name).append("\n");
sb.append("性别:").append(gender).append("\n");
sb.append("证件类型:").append(idType).append("\n");
sb.append("证件号码:").append(idNum).append("\n");
sb.append("出生日期:").append(birth).append("\n");
infoArea.setText(sb.toString());
}
private void reset() {
nameField.setText("");
genderBox.getSelectionModel().clearSelection();
idTypeBox.getSelectionModel().clearSelection();
idNumField.setText("");
birthDatePicker.setValue(null);
infoArea.setText("");
}
public static void main(String[] args) {
launch(args);
}
}
```
运行结果如下图所示:
![信息采集界面](https://img-blog.csdnimg.cn/20211116153613709.png)
阅读全文