|
|
|
|
@ -0,0 +1,273 @@
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class Main {
|
|
|
|
|
private DefaultListModel<TaskItem> listModel;
|
|
|
|
|
private JList<TaskItem> taskList;
|
|
|
|
|
private JTextField taskField;
|
|
|
|
|
private JTextField dateField;
|
|
|
|
|
private JComboBox<String> priorityComboBox;
|
|
|
|
|
private List<TaskItem> data = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public Main() {
|
|
|
|
|
JFrame frame = new JFrame("待办事项列表");
|
|
|
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
frame.setSize(600, 600);
|
|
|
|
|
|
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
|
panel.setLayout(new BorderLayout(10, 10));
|
|
|
|
|
|
|
|
|
|
// 设置中文字体
|
|
|
|
|
Font font = new Font("微软雅黑", Font.PLAIN, 14);
|
|
|
|
|
UIManager.put("Label.font", font);
|
|
|
|
|
UIManager.put("TextField.font", font);
|
|
|
|
|
UIManager.put("ComboBox.font", font);
|
|
|
|
|
UIManager.put("Button.font", font);
|
|
|
|
|
UIManager.put("List.font", font);
|
|
|
|
|
UIManager.put("OptionPane.messageFont", font);
|
|
|
|
|
|
|
|
|
|
listModel = new DefaultListModel<>();
|
|
|
|
|
taskList = new JList<>(listModel);
|
|
|
|
|
taskList.setCellRenderer(new CustomListRenderer());
|
|
|
|
|
panel.add(new JScrollPane(taskList), BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
JPanel inputPanel = new JPanel(new GridLayout(4, 2, 10, 10));
|
|
|
|
|
inputPanel.add(new JLabel("任务:"));
|
|
|
|
|
taskField = new JTextField();
|
|
|
|
|
inputPanel.add(taskField);
|
|
|
|
|
|
|
|
|
|
inputPanel.add(new JLabel("截止日期 (yyyy-mm-dd):"));
|
|
|
|
|
dateField = new JTextField();
|
|
|
|
|
inputPanel.add(dateField);
|
|
|
|
|
|
|
|
|
|
inputPanel.add(new JLabel("优先级:"));
|
|
|
|
|
String[] priorities = {"低", "中", "高"};
|
|
|
|
|
priorityComboBox = new JComboBox<>(priorities);
|
|
|
|
|
inputPanel.add(priorityComboBox);
|
|
|
|
|
|
|
|
|
|
JPanel buttonPanel = new JPanel(new GridLayout(1, 5, 10, 10));
|
|
|
|
|
JButton addButton = new JButton("添加");
|
|
|
|
|
JButton removeButton = new JButton("删除");
|
|
|
|
|
JButton editButton = new JButton("编辑");
|
|
|
|
|
JButton saveButton = new JButton("保存");
|
|
|
|
|
JButton toggleButton = new JButton("完成");
|
|
|
|
|
|
|
|
|
|
buttonPanel.add(addButton);
|
|
|
|
|
buttonPanel.add(removeButton);
|
|
|
|
|
buttonPanel.add(editButton);
|
|
|
|
|
buttonPanel.add(saveButton);
|
|
|
|
|
buttonPanel.add(toggleButton);
|
|
|
|
|
|
|
|
|
|
addButton.addActionListener(e -> addTask());
|
|
|
|
|
removeButton.addActionListener(e -> removeTask());
|
|
|
|
|
editButton.addActionListener(e -> editTask());
|
|
|
|
|
saveButton.addActionListener(e -> saveTasks());
|
|
|
|
|
toggleButton.addActionListener(e -> toggleTaskStatus());
|
|
|
|
|
|
|
|
|
|
panel.add(inputPanel, BorderLayout.NORTH);
|
|
|
|
|
panel.add(buttonPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
frame.add(panel);
|
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean validateDate(String dateStr) {
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
sdf.setLenient(false);
|
|
|
|
|
try {
|
|
|
|
|
Date date = sdf.parse(dateStr);
|
|
|
|
|
Date today = new Date();
|
|
|
|
|
return date.after(today) || date.equals(today);
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addTask() {
|
|
|
|
|
String taskText = taskField.getText().trim();
|
|
|
|
|
String dueDateText = dateField.getText().trim();
|
|
|
|
|
String priority = (String) priorityComboBox.getSelectedItem();
|
|
|
|
|
|
|
|
|
|
if (taskText.isEmpty() || dueDateText.isEmpty()) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "请输入任务和截止日期!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!validateDate(dueDateText)) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "日期格式不正确或日期已过期!请使用格式:yyyy-mm-dd");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TaskItem newTask = new TaskItem(taskText, dueDateText, priority);
|
|
|
|
|
listModel.addElement(newTask);
|
|
|
|
|
data.add(newTask);
|
|
|
|
|
taskField.setText("");
|
|
|
|
|
dateField.setText("");
|
|
|
|
|
priorityComboBox.setSelectedIndex(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeTask() {
|
|
|
|
|
int selectedIndex = taskList.getSelectedIndex();
|
|
|
|
|
if (selectedIndex != -1) {
|
|
|
|
|
listModel.remove(selectedIndex);
|
|
|
|
|
data.remove(selectedIndex);
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "请选择要删除的任务!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void editTask() {
|
|
|
|
|
int selectedIndex = taskList.getSelectedIndex();
|
|
|
|
|
if (selectedIndex != -1) {
|
|
|
|
|
TaskItem item = listModel.getElementAt(selectedIndex);
|
|
|
|
|
|
|
|
|
|
String currentTask = item.getTask();
|
|
|
|
|
String currentDueDate = item.getDueDate();
|
|
|
|
|
String currentPriority = item.getPriority();
|
|
|
|
|
|
|
|
|
|
String newTask = JOptionPane.showInputDialog(null, "当前任务: " + currentTask + "\n请输入新任务:", currentTask);
|
|
|
|
|
if (newTask == null) return;
|
|
|
|
|
|
|
|
|
|
String newDueDate = JOptionPane.showInputDialog(null, "当前截止日期: " + currentDueDate + "\n请输入新截止日期 (yyyy-mm-dd):", currentDueDate);
|
|
|
|
|
if (newDueDate == null) return;
|
|
|
|
|
|
|
|
|
|
if (!validateDate(newDueDate)) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "日期格式不正确或日期已过期!请使用格式:yyyy-mm-dd");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] priorities = {"低", "中", "高"};
|
|
|
|
|
String newPriority = (String) JOptionPane.showInputDialog(
|
|
|
|
|
null,
|
|
|
|
|
"请选择优先级:",
|
|
|
|
|
"优先级选择",
|
|
|
|
|
JOptionPane.QUESTION_MESSAGE,
|
|
|
|
|
null,
|
|
|
|
|
priorities,
|
|
|
|
|
currentPriority
|
|
|
|
|
);
|
|
|
|
|
if (newPriority == null) return;
|
|
|
|
|
|
|
|
|
|
item.setTask(newTask);
|
|
|
|
|
item.setDueDate(newDueDate);
|
|
|
|
|
item.setPriority(newPriority);
|
|
|
|
|
|
|
|
|
|
taskList.repaint();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "请选择要编辑的任务!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void toggleTaskStatus() {
|
|
|
|
|
int selectedIndex = taskList.getSelectedIndex();
|
|
|
|
|
if (selectedIndex != -1) {
|
|
|
|
|
TaskItem item = listModel.getElementAt(selectedIndex);
|
|
|
|
|
item.toggleCompleted();
|
|
|
|
|
taskList.repaint();
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "请选择要切换状态的任务!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveTasks() {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "任务已保存!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
SwingUtilities.invokeLater(Main::new);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TaskItem {
|
|
|
|
|
private String task;
|
|
|
|
|
private String dueDate;
|
|
|
|
|
private String priority;
|
|
|
|
|
private boolean isCompleted;
|
|
|
|
|
|
|
|
|
|
public TaskItem(String task, String dueDate, String priority) {
|
|
|
|
|
this.task = task;
|
|
|
|
|
this.dueDate = dueDate;
|
|
|
|
|
this.priority = priority;
|
|
|
|
|
this.isCompleted = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void toggleCompleted() {
|
|
|
|
|
isCompleted = !isCompleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isCompleted() {
|
|
|
|
|
return isCompleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getTask() {
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTask(String task) {
|
|
|
|
|
this.task = task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getDueDate() {
|
|
|
|
|
return dueDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDueDate(String dueDate) {
|
|
|
|
|
this.dueDate = dueDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getPriority() {
|
|
|
|
|
return priority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPriority(String priority) {
|
|
|
|
|
this.priority = priority;
|
|
|
|
|
}
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return task + " | 截止日期: " + dueDate + " | 优先级: " + priority;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CustomListRenderer extends DefaultListCellRenderer {
|
|
|
|
|
@Override
|
|
|
|
|
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
|
|
|
|
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
|
|
|
|
|
|
|
|
|
if (value instanceof TaskItem) {
|
|
|
|
|
TaskItem task = (TaskItem) value;
|
|
|
|
|
|
|
|
|
|
// 使用文字标记任务状态
|
|
|
|
|
String status = task.isCompleted() ? "[完成] " : "[未完成] ";
|
|
|
|
|
label.setText(status + task.toString());
|
|
|
|
|
|
|
|
|
|
// 设置不同优先级的颜色
|
|
|
|
|
switch (task.getPriority()) {
|
|
|
|
|
case "高":
|
|
|
|
|
label.setForeground(Color.RED);
|
|
|
|
|
break;
|
|
|
|
|
case "中":
|
|
|
|
|
label.setForeground(Color.ORANGE);
|
|
|
|
|
break;
|
|
|
|
|
case "低":
|
|
|
|
|
label.setForeground(Color.GREEN);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果任务已完成,添加删除线并变灰
|
|
|
|
|
if (task.isCompleted()) {
|
|
|
|
|
label.setFont(label.getFont().deriveFont(Font.PLAIN | Font.ITALIC));
|
|
|
|
|
label.setForeground(Color.GRAY);
|
|
|
|
|
} else {
|
|
|
|
|
label.setFont(label.getFont().deriveFont(Font.PLAIN));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return label;
|
|
|
|
|
}
|
|
|
|
|
}
|