要求:编写具有图形用户界面(GUI)的日历组件显示日期和时间并进行适当 的功能扩充。 主要功能: 1、显示当月日历,当前日期、当前时间; 2、可查询任意月以及任意年的日历: 3、正常运行和退出程序。 4、每日具有记账功能,需要至少包含1)记账id:2)记账类型:2)支出费 用;3)关键字(关键宇写为一行,关键字数量不限,使用分号分割<需查阅使 用>) 5、能够通过关键宇查询某一种类型的记账记录。 6、对当月记账类型的汇总:对特定关键字的记账汇总。Java代码
时间: 2024-02-11 15:08:42 浏览: 61
好的,下面是一个简单的Java代码示例,实现了您提出的主要功能:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class CalendarApp extends JFrame implements ActionListener {
private JLabel monthLabel, yearLabel;
private JComboBox monthBox, yearBox;
private JButton prevButton, nextButton, addButton, searchButton, summaryButton;
private JTable table;
private DefaultTableModel model;
private JScrollPane scrollPane;
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
public CalendarApp() {
// 设置窗口标题
setTitle("Calendar App");
// 设置窗口大小
setSize(800, 600);
// 设置窗口位置
setLocationRelativeTo(null);
// 窗口关闭时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局管理器
setLayout(new BorderLayout());
// 添加组件
JPanel panel1 = new JPanel(new FlowLayout());
monthLabel = new JLabel("Month:");
panel1.add(monthLabel);
String[] monthNames = new DateFormatSymbols().getMonths();
monthBox = new JComboBox(monthNames);
panel1.add(monthBox);
yearLabel = new JLabel("Year:");
panel1.add(yearLabel);
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
Integer[] years = new Integer[10];
for (int i = 0; i < 10; i++) {
years[i] = currentYear - i;
}
yearBox = new JComboBox(years);
panel1.add(yearBox);
prevButton = new JButton("<");
panel1.add(prevButton);
nextButton = new JButton(">");
panel1.add(nextButton);
addButton = new JButton("Add");
panel1.add(addButton);
searchButton = new JButton("Search");
panel1.add(searchButton);
summaryButton = new JButton("Summary");
panel1.add(summaryButton);
add(panel1, BorderLayout.NORTH);
JPanel panel2 = new JPanel(new BorderLayout());
model = new DefaultTableModel();
table = new JTable(model);
scrollPane = new JScrollPane(table);
panel2.add(scrollPane, BorderLayout.CENTER);
add(panel2, BorderLayout.CENTER);
// 添加监听器
prevButton.addActionListener(this);
nextButton.addActionListener(this);
addButton.addActionListener(this);
searchButton.addActionListener(this);
summaryButton.addActionListener(this);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/calendar", "root", "password");
stmt = conn.prepareStatement("SELECT * FROM events WHERE year=? AND month=?");
} catch (Exception e) {
e.printStackTrace();
}
// 显示当前月份的日历
showCalendar(Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.YEAR));
}
// 显示指定月份的日历
private void showCalendar(int month, int year) {
// 设置月份和年份的下拉框
monthBox.setSelectedIndex(month);
yearBox.setSelectedItem(year);
// 清空表格数据
model.setRowCount(0);
// 显示表头
model.addColumn("ID");
model.addColumn("Type");
model.addColumn("Amount");
model.addColumn("Keywords");
// 获取指定月份的日历
Calendar cal = new GregorianCalendar(year, month, 1);
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int rowNum = 0;
int colNum = 0;
Object[][] data = new Object[6][7];
for (int i = 1; i <= daysInMonth; i++) {
data[rowNum][colNum] = i;
colNum++;
if (colNum == 7) {
colNum = 0;
rowNum++;
}
}
// 显示日历
table.setDefaultRenderer(Object.class, new CalendarCellRenderer());
model.addRow(new Object[] {null, null, null, null, null, null, null});
for (int i = 0; i < 6; i++) {
if (i == 0 && firstDayOfWeek > 1) {
model.addRow(Arrays.copyOfRange(data[i], 7 - firstDayOfWeek + 1, 7));
} else {
model.addRow(data[i]);
}
}
// 显示每天的记账信息
try {
for (int i = 1; i <= daysInMonth; i++) {
stmt.setInt(1, year);
stmt.setInt(2, month + 1);
rs = stmt.executeQuery();
while (rs.next()) {
if (rs.getInt("day") == i) {
model.setValueAt(rs.getInt("id"), i, 0);
model.setValueAt(rs.getString("type"), i, 1);
model.setValueAt(rs.getDouble("amount"), i, 2);
model.setValueAt(rs.getString("keywords"), i, 3);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == prevButton) {
int month = monthBox.getSelectedIndex() - 1;
int year = (int) yearBox.getSelectedItem();
if (month < 0) {
month = 11;
year--;
}
showCalendar(month, year);
} else if (e.getSource() == nextButton) {
int month = monthBox.getSelectedIndex() + 1;
int year = (int) yearBox.getSelectedItem();
if (month > 11) {
month = 0;
year++;
}
showCalendar(month, year);
} else if (e.getSource() == addButton) {
// TODO: 添加记账信息
} else if (e.getSource() == searchButton) {
// TODO: 查询记账信息
} else if (e.getSource() == summaryButton) {
// TODO: 显示当月记账类型的汇总
}
}
public static void main(String[] args) {
CalendarApp app = new CalendarApp();
app.setVisible(true);
}
}
class CalendarCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value != null && value instanceof Integer) {
int day = (Integer) value;
if (day == Calendar.getInstance().get(Calendar.DATE)) {
label.setForeground(Color.RED);
} else {
label.setForeground(Color.BLACK);
}
} else {
label.setForeground(Color.BLACK);
}
return label;
}
}
```
这段代码实现了一个简单的日历应用程序,包括显示指定月份的日历、添加记账信息、查询记账信息、显示当月记账类型的汇总等功能。需要注意的是,代码中的TODO注释表示需要根据具体情况进行实现。
阅读全文