目标:完成一个 JAVA Application 应用开发:时钟日历 ,要求实现待办项功能:支持在日历中添加代办项,允许用户在日历中的某一天中插入一定数量的待办项,而后用户 可以勾选这些待办项是否完成,以及增删代办项。
时间: 2024-03-15 16:43:21 浏览: 18
好的,以下是一个简单的实现示例,供您参考:
1. 创建GUI界面
可以使用Java Swing或JavaFX来创建GUI界面,这里以JavaFX为例。界面中包含一个时钟模块、一个日历模块和一个待办项列表,以及添加、删除待办项的按钮。
```java
public class ClockCalendarApp extends Application {
private Label clockLabel;
private DatePicker calendar;
private ListView<TodoItem> todoList;
@Override
public void start(Stage primaryStage) {
// 创建界面组件
clockLabel = new Label();
calendar = new DatePicker();
todoList = new ListView<>();
Button addButton = new Button("添加");
Button deleteButton = new Button("删除");
// 设置界面布局
VBox layout = new VBox();
layout.getChildren().addAll(clockLabel, calendar, todoList, addButton, deleteButton);
Scene scene = new Scene(layout, 400, 400);
// 显示界面
primaryStage.setScene(scene);
primaryStage.show();
}
}
```
2. 实现时钟模块
可以使用Java的Timeline类来实现定时更新界面上的时钟。
```java
@Override
public void start(Stage primaryStage) {
// ...
// 更新时钟
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
clockLabel.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
```
3. 实现日历模块
可以监听DatePicker的日期变化事件来更新日历显示。
```java
@Override
public void start(Stage primaryStage) {
// ...
// 更新日历
calendar.setOnAction(event -> {
LocalDate date = calendar.getValue();
System.out.println("Selected date: " + date);
});
}
```
4. 实现待办项的添加和删除功能
可以使用Java的ObservableList和Map来存储待办项,其中Map的键为日期,值为该日期下的所有待办项。
```java
public class TodoItem {
private String content;
private boolean completed;
public TodoItem(String content) {
this.content = content;
this.completed = false;
}
public String getContent() {
return content;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
}
public class ClockCalendarApp extends Application {
// ...
private ObservableList<TodoItem> todoItems;
private Map<LocalDate, List<TodoItem>> todoMap;
@Override
public void start(Stage primaryStage) {
// ...
// 初始化待办项列表和Map
todoItems = FXCollections.observableArrayList();
todoMap = new HashMap<>();
// 添加待办项
addButton.setOnAction(event -> {
LocalDate date = calendar.getValue();
if (date == null) {
System.out.println("请先选择日期!");
return;
}
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("添加待办项");
dialog.setHeaderText("请输入待办项内容:");
Optional<String> result = dialog.showAndWait();
result.ifPresent(content -> {
TodoItem todoItem = new TodoItem(content);
todoItems.add(todoItem);
List<TodoItem> todoList = todoMap.getOrDefault(date, new ArrayList<>());
todoList.add(todoItem);
todoMap.put(date, todoList);
});
});
// 删除待办项
deleteButton.setOnAction(event -> {
TodoItem selected = todoList.getSelectionModel().getSelectedItem();
if (selected == null) {
System.out.println("请先选择待办项!");
return;
}
LocalDate date = calendar.getValue();
todoItems.remove(selected);
List<TodoItem> todoList = todoMap.get(date);
todoList.remove(selected);
if (todoList.isEmpty()) {
todoMap.remove(date);
}
});
// 绑定待办项列表
todoList.setItems(todoItems);
todoList.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(TodoItem item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item.getContent() + (item.isCompleted() ? " (已完成)" : ""));
}
}
});
// 勾选待办项
todoList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
newValue.setCompleted(!newValue.isCompleted());
todoList.refresh();
}
});
}
}
```
5. 实现保存和读取待办项的功能
可以使用Java的文件读写API来实现,比如将待办项存储在一个文本文件中。
```java
@Override
public void stop() throws Exception {
// 保存待办项
try (PrintWriter writer = new PrintWriter(new FileWriter("todo.txt"))) {
for (Map.Entry<LocalDate, List<TodoItem>> entry : todoMap.entrySet()) {
LocalDate date = entry.getKey();
List<TodoItem> todoList = entry.getValue();
for (TodoItem todoItem : todoList) {
writer.printf("%s\t%s\t%s\n", date, todoItem.getContent(), todoItem.isCompleted());
}
}
}
}
@Override
public void start(Stage primaryStage) {
// ...
// 读取待办项
try (Scanner scanner = new Scanner(new File("todo.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split("\t");
LocalDate date = LocalDate.parse(parts[0]);
String content = parts[1];
boolean completed = Boolean.parseBoolean(parts[2]);
TodoItem todoItem = new TodoItem(content);
todoItem.setCompleted(completed);
todoItems.add(todoItem);
List<TodoItem> todoList = todoMap.getOrDefault(date, new ArrayList<>());
todoList.add(todoItem);
todoMap.put(date, todoList);
}
} catch (FileNotFoundException e) {
// 文件不存在,忽略
}
}
```
6. 完整的代码示例
```java
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
public class ClockCalendarApp extends Application {
private Label clockLabel;
private DatePicker calendar;
private ListView<TodoItem> todoList;
private ObservableList<TodoItem> todoItems;
private Map<LocalDate, List<TodoItem>> todoMap;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// 创建界面组件
clockLabel = new Label();
calendar = new DatePicker();
todoList = new ListView<>();
Button addButton = new Button("添加");
Button deleteButton = new Button("删除");
// 设置界面布局
VBox layout = new VBox();
layout.getChildren().addAll(clockLabel, calendar, todoList, addButton, deleteButton);
Scene scene = new Scene(layout, 400, 400);
// 更新时钟
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
clockLabel.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// 更新日历
calendar.setOnAction(event -> {
LocalDate date = calendar.getValue();
System.out.println("Selected date: " + date);
});
// 初始化待办项列表和Map
todoItems = FXCollections.observableArrayList();
todoMap = new HashMap<>();
// 添加待办项
addButton.setOnAction(event -> {
LocalDate date = calendar.getValue();
if (date == null) {
System.out.println("请先选择日期!");
return;
}
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("添加待办项");
dialog.setHeaderText("请输入待办项内容:");
Optional<String> result = dialog.showAndWait();
result.ifPresent(content -> {
TodoItem todoItem = new TodoItem(content);
todoItems.add(todoItem);
List<TodoItem> todoList = todoMap.getOrDefault(date, new ArrayList<>());
todoList.add(todoItem);
todoMap.put(date, todoList);
});
});
// 删除待办项
deleteButton.setOnAction(event -> {
TodoItem selected = todoList.getSelectionModel().getSelectedItem();
if (selected == null) {
System.out.println("请先选择待办项!");
return;
}
LocalDate date = calendar.getValue();
todoItems.remove(selected);
List<TodoItem> todoList = todoMap.get(date);
todoList.remove(selected);
if (todoList.isEmpty()) {
todoMap.remove(date);
}
});
// 绑定待办项列表
todoList.setItems(todoItems);
todoList.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(TodoItem item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item.getContent() + (item.isCompleted() ? " (已完成)" : ""));
}
}
});
// 勾选待办项
todoList.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
newValue.setCompleted(!newValue.isCompleted());
todoList.refresh();
}
});
// 读取待办项
try (Scanner scanner = new Scanner(new File("todo.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split("\t");
LocalDate date = LocalDate.parse(parts[0]);
String content = parts[1];
boolean completed = Boolean.parseBoolean(parts[2]);
TodoItem todoItem = new TodoItem(content);
todoItem.setCompleted(completed);
todoItems.add(todoItem);
List<TodoItem> todoList = todoMap.getOrDefault(date, new ArrayList<>());
todoList.add(todoItem);
todoMap.put(date, todoList);
}
} catch (FileNotFoundException e) {
// 文件不存在,忽略
}
// 保存待办项
primaryStage.setOnCloseRequest(event -> {
try (PrintWriter writer = new PrintWriter(new FileWriter("todo.txt"))) {
for (Map.Entry<LocalDate, List<TodoItem>> entry : todoMap.entrySet()) {
LocalDate date = entry.getKey();
List<TodoItem> todoList = entry.getValue();
for (TodoItem todoItem : todoList) {
writer.printf("%s\t%s\t%s\n", date, todoItem.getContent(), todoItem.isCompleted());
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
// 显示界面
primaryStage.setScene(scene);
primaryStage.show();
}
public static class TodoItem {
private String content;
private boolean completed;
public TodoItem(String content) {
this.content = content;
this.completed = false;
}
public String getContent() {
return content;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
}
}
```