开始日期和结束日期的校验弹窗代码java怎么写
时间: 2024-04-30 07:21:23 浏览: 45
以下是一个简单的日期校验弹窗代码示例,使用了JavaFX框架:
```java
import java.time.LocalDate;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class DateChecker extends Application {
@Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(5);
grid.setHgap(5);
DatePicker startDatePicker = new DatePicker();
startDatePicker.setPromptText("Start Date");
GridPane.setConstraints(startDatePicker, 0, 0);
grid.getChildren().add(startDatePicker);
DatePicker endDatePicker = new DatePicker();
endDatePicker.setPromptText("End Date");
GridPane.setConstraints(endDatePicker, 0, 1);
grid.getChildren().add(endDatePicker);
Button checkButton = new Button("Check Dates");
GridPane.setConstraints(checkButton, 1, 0);
grid.getChildren().add(checkButton);
checkButton.setOnAction((ActionEvent e) -> {
LocalDate startDate = startDatePicker.getValue();
LocalDate endDate = endDatePicker.getValue();
if (startDate == null || endDate == null) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Date Checker");
alert.setHeaderText(null);
alert.setContentText("Please select both start and end dates.");
alert.showAndWait();
} else if (startDate.isAfter(endDate)) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Date Checker");
alert.setHeaderText(null);
alert.setContentText("Start date cannot be after end date.");
alert.showAndWait();
} else {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Date Checker");
alert.setHeaderText(null);
alert.setContentText("Start date: " + startDate.toString() + "\nEnd date: " + endDate.toString());
alert.showAndWait();
}
});
Scene scene = new Scene(grid, 300, 150);
primaryStage.setTitle("Date Checker");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
该代码创建了一个简单的JavaFX应用程序,用户可以选择开始日期和结束日期,然后单击“Check Dates”按钮来检查这些日期。 如果开始日期晚于结束日期,将会弹出错误警告框,否则将会弹出信息框显示所选日期。
阅读全文