编写JavaFX程序,实现一个信息采集功能,主体采用BorderPane,要求: 1)Top区放置一个标签“个人信息”,并设置字体大小和颜色; 2)Bottom区放置两个按钮“提交”和“重填”; 3)Right区放置一个文本区; 4)Center区是放置信息填写的各种组件,如图所示,其中证件类型有:身份证、军人证、学生证和护照。 当点击“提交”按钮时,将填写的信息采集到Right区的文本区中;当点击“重填”按钮时,清空所有填写的内容。设计合适的舞台和场景,进行测试。
时间: 2024-02-22 21:01:07 浏览: 15
好的,我会根据您的要求编写JavaFX程序,实现信息采集功能。
首先,我们需要创建一个JavaFX项目,并添加必要的依赖库。
接着,我们可以开始编写代码。以下是一个简单的实现:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class InfoCollector extends Application {
private TextField nameField;
private TextField idField;
private ChoiceBox<String> certTypeChoiceBox;
private Label resultLabel;
@Override
public void start(Stage primaryStage) throws Exception {
// 创建顶部标签
Label titleLabel = new Label("个人信息");
titleLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
// 创建底部按钮
Button submitButton = new Button("提交");
Button resetButton = new Button("重填");
// 创建右侧文本区
resultLabel = new Label();
resultLabel.setWrapText(true);
// 创建中间信息填写区
GridPane infoGrid = new GridPane();
infoGrid.setAlignment(Pos.CENTER);
infoGrid.setHgap(10);
infoGrid.setVgap(10);
infoGrid.setPadding(new Insets(25, 25, 25, 25));
infoGrid.add(new Label("姓名:"), 0, 0);
nameField = new TextField();
infoGrid.add(nameField, 1, 0);
infoGrid.add(new Label("证件类型:"), 0, 1);
certTypeChoiceBox = new ChoiceBox<>();
certTypeChoiceBox.getItems().addAll("身份证", "军人证", "学生证", "护照");
certTypeChoiceBox.setValue("身份证");
infoGrid.add(certTypeChoiceBox, 1, 1);
infoGrid.add(new Label("证件号码:"), 0, 2);
idField = new TextField();
infoGrid.add(idField, 1, 2);
// 创建底部按钮区
HBox buttonBox = new HBox();
buttonBox.setAlignment(Pos.CENTER);
buttonBox.setSpacing(10);
buttonBox.getChildren().addAll(submitButton, resetButton);
// 创建主布局
BorderPane root = new BorderPane();
root.setTop(titleLabel);
root.setCenter(infoGrid);
root.setBottom(buttonBox);
root.setRight(resultLabel);
BorderPane.setAlignment(titleLabel, Pos.CENTER);
BorderPane.setAlignment(resultLabel, Pos.TOP_RIGHT);
// 绑定按钮事件
submitButton.setOnAction(event -> {
submit();
});
resetButton.setOnAction(event -> {
reset();
});
// 创建场景
Scene scene = new Scene(root, 600, 400);
// 设置窗口标题和场景
primaryStage.setTitle("信息采集");
primaryStage.setScene(scene);
// 显示窗口
primaryStage.show();
}
private void submit() {
String name = nameField.getText();
String certType = certTypeChoiceBox.getValue();
String id = idField.getText();
resultLabel.setText("姓名:" + name + "\n证件类型:" + certType + "\n证件号码:" + id);
}
private void reset() {
nameField.setText("");
certTypeChoiceBox.setValue("身份证");
idField.setText("");
resultLabel.setText("");
}
public static void main(String[] args) {
launch(args);
}
}
```
这个程序创建了一个顶部的标签、一个底部的提交和重填按钮、一个右侧的文本区和一个中间的信息填写区。
当点击提交按钮时,程序会将填写的信息采集到右侧的文本区中;当点击重填按钮时,程序会清空所有填写的内容。
您可以根据需要进行修改和优化。