编写具有图形用户界面(GUI)的日历组件显示日期和时间并进行适当 的功能扩充。 ·主要功能: 1、显示当月日历,当前日期、当前时间; 2、可查询任意月以及任意年的日历: 3、正常运行和退出程序。 4、每日具有记账功能,需要至少包含1)记账id:2)记账类型:3)支出费用; 4)关键字(关键宇写为一行,关键字数量不限,使用分号分割<需查阅使 用>) 5、能够通过关键宇查询某一种类型的记账记录。 6、对当月记账类型的汇总:对特定关键字的记账汇总。(Java代码)
时间: 2024-02-13 11:02:32 浏览: 32
好的,下面是Java代码实现:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
public class CalendarApp extends JFrame implements ActionListener {
private JLabel dateLabel, timeLabel;
private JButton prevMonthBtn, nextMonthBtn, prevYearBtn, nextYearBtn, addBtn, queryBtn, summaryBtn, exitBtn;
private JComboBox<Integer> monthComboBox;
private JTextField yearField, idField, typeField, costField, keywordField;
private JTable accountTable;
private DefaultTableModel accountTableModel;
private Calendar calendar;
private SimpleDateFormat dateFormat, timeFormat;
private HashMap<String, Double> summaryMap;
public CalendarApp() {
super("Calendar App");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// 创建日期和时间标签
dateLabel = new JLabel();
timeLabel = new JLabel();
// 创建按钮
prevMonthBtn = new JButton("<<");
nextMonthBtn = new JButton(">>");
prevYearBtn = new JButton("<<<");
nextYearBtn = new JButton(">>>");
addBtn = new JButton("Add");
queryBtn = new JButton("Query");
summaryBtn = new JButton("Summary");
exitBtn = new JButton("Exit");
// 绑定按钮事件监听器
prevMonthBtn.addActionListener(this);
nextMonthBtn.addActionListener(this);
prevYearBtn.addActionListener(this);
nextYearBtn.addActionListener(this);
addBtn.addActionListener(this);
queryBtn.addActionListener(this);
summaryBtn.addActionListener(this);
exitBtn.addActionListener(this);
// 创建月份下拉框和年份文本框
monthComboBox = new JComboBox<Integer>();
for (int i = 1; i <= 12; i++) {
monthComboBox.addItem(i);
}
yearField = new JTextField();
// 创建记账信息输入框和表格
idField = new JTextField();
typeField = new JTextField();
costField = new JTextField();
keywordField = new JTextField();
accountTableModel = new DefaultTableModel(new Object[]{"ID", "Type", "Cost", "Keyword"}, 0);
accountTable = new JTable(accountTableModel);
// 创建日期和时间格式化器
dateFormat = new SimpleDateFormat("yyyy年MM月dd日 E");
timeFormat = new SimpleDateFormat("HH:mm:ss");
// 创建日历对象
calendar = Calendar.getInstance();
// 初始化显示
updateUI();
}
// 更新用户界面
private void updateUI() {
// 更新日期和时间标签
Date date = new Date();
dateLabel.setText(dateFormat.format(calendar.getTime()));
timeLabel.setText(timeFormat.format(date));
// 更新月份下拉框和年份文本框
monthComboBox.setSelectedItem(calendar.get(Calendar.MONTH) + 1);
yearField.setText(Integer.toString(calendar.get(Calendar.YEAR)));
// 更新记账信息表格
updateAccountTable();
// 清空记账信息输入框
idField.setText("");
typeField.setText("");
costField.setText("");
keywordField.setText("");
// 更新汇总信息
summaryMap = calculateSummary();
}
// 更新记账信息表格
private void updateAccountTable() {
// 清空表格
accountTableModel.setRowCount(0);
// 获取当天日期
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
// 从文件或数据库中读取记账信息
// 这里使用示例数据
Object[][] data = {
{"001", "Food", 10.0, "Rice;Noodle"},
{"002", "Transport", 5.0, "Bus"},
{"003", "Shopping", 20.0, "Clothes;Shoes"},
{"004", "Food", 15.0, "Pizza"},
{"005", "Entertainment", 30.0, "Movie"}
};
for (Object[] row : data) {
String id = (String)row[0];
String type = (String)row[1];
Double cost = (Double)row[2];
String keyword = (String)row[3];
// 如果是当天的记录,就添加到表格中
if (year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1 && id.startsWith(Integer.toString(day))) {
accountTableModel.addRow(new Object[]{id, type, cost, keyword});
}
}
}
// 计算当月记账类型汇总信息
private HashMap<String, Double> calculateSummary() {
// 获取当月日期信息
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 初始化汇总信息
HashMap<String, Double> summaryMap = new HashMap<String, Double>();
for (String type : new String[]{"Food", "Transport", "Shopping", "Entertainment"}) {
summaryMap.put(type, 0.0);
}
// 遍历每天的记账记录,进行汇总
for (int i = 1; i <= days; i++) {
// 从文件或数据库中读取记账信息
// 这里使用示例数据
Object[][] data = {
{"001", "Food", 10.0, "Rice;Noodle"},
{"002", "Transport", 5.0, "Bus"},
{"003", "Shopping", 20.0, "Clothes;Shoes"},
{"004", "Food", 15.0, "Pizza"},
{"005", "Entertainment", 30.0, "Movie"}
};
for (Object[] row : data) {
String type = (String)row[1];
Double cost = (Double)row[2];
// 如果是当天的记录,就进行汇总
if (year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1 && i == Integer.parseInt((String)row[0])) {
if (summaryMap.containsKey(type)) {
summaryMap.put(type, summaryMap.get(type) + cost);
} else {
summaryMap.put(type, cost);
}
}
}
}
return summaryMap;
}
// 处理按钮事件
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == prevMonthBtn) {
// 上一月
calendar.add(Calendar.MONTH, -1);
updateUI();
} else if (source == nextMonthBtn) {
// 下一月
calendar.add(Calendar.MONTH, 1);
updateUI();
} else if (source == prevYearBtn) {
// 上一年
calendar.add(Calendar.YEAR, -1);
updateUI();
} else if (source == nextYearBtn) {
// 下一年
calendar.add(Calendar.YEAR, 1);
updateUI();
} else if (source == addBtn) {
// 添加记账信息
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
String id = String.format("%02d%02d%s", day, accountTableModel.getRowCount() + 1, year);
String type = typeField.getText();
Double cost = Double.parseDouble(costField.getText());
String keyword = keywordField.getText();
accountTableModel.addRow(new Object[]{id, type, cost, keyword});
// 将记账信息保存到文件或数据库中
System.out.println("Add account: " + id + ", " + type + ", " + cost + ", " + keyword);
} else if (source == queryBtn) {
// 查询记账信息
String keyword = keywordField.getText();
// 从文件或数据库中读取记账信息,并根据关键字进行过滤
// 这里使用示例数据
Object[][] data = {
{"001", "Food", 10.0, "Rice;Noodle"},
{"002", "Transport", 5.0, "Bus"},
{"003", "Shopping", 20.0, "Clothes;Shoes"},
{"004", "Food", 15.0, "Pizza"},
{"005", "Entertainment", 30.0, "Movie"}
};
accountTableModel.setRowCount(0);
for (Object[] row : data) {
String type = (String)row[1];
Double cost = (Double)row[2];
String keywords = (String)row[3];
if (keywords.contains(keyword)) {
accountTableModel.addRow(new Object[]{row[0], type, cost, keywords});
}
}
} else if (source == summaryBtn) {
// 显示记账类型汇总信息
JOptionPane.showMessageDialog(this, summaryMap, "Summary", JOptionPane.INFORMATION_MESSAGE);
} else if (source == exitBtn) {
// 退出程序
System.exit(0);
}
}
public static void main(String[] args) {
CalendarApp app = new CalendarApp();
app.setVisible(true);
}
}
```
这个程序使用了Swing库来创建用户界面,并使用了Java日期库(java.util.Date和java.text.SimpleDateFormat)来格式化日期和时间。程序中还使用了HashMap来存储记账类型和对应的费用,并使用JOptionPane来显示汇总信息。注意,这个程序只是一个示例程序,实际应用中需要根据具体需求进行修改。