简单的日历程序:使用Swing有关组件开发一个简单的图形界面的日历程序,要求程序运行显示当前月份的日历,通过组合框选择年月,可显示指定年月的日历
时间: 2024-05-10 17:19:29 浏览: 87
简单的日历程序
4星 · 用户满意度95%
好的,我可以为您提供一个简单的日历程序的代码。代码使用Java Swing组件开发,可以显示当前月份的日历,并且可以通过组合框选择指定的年月。
请注意,这个程序可能不是很完善,但可以作为一个起点来帮助您开始开发自己的日历程序。
下面是代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class CalendarProgram extends JFrame implements ActionListener {
private JComboBox<String> yearComboBox, monthComboBox;
private JLabel monthLabel;
private JPanel calendarPanel;
private int currentYear, currentMonth;
public CalendarProgram() {
super("简单日历程序");
// 获取当前年份和月份
Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
currentMonth = c.get(Calendar.MONTH) + 1;
// 创建顶部面板,包含年份和月份选择器
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout());
yearComboBox = new JComboBox<String>();
for (int year = currentYear - 50; year <= currentYear + 50; year++) {
yearComboBox.addItem(String.valueOf(year));
}
yearComboBox.setSelectedItem(String.valueOf(currentYear));
yearComboBox.addActionListener(this);
monthComboBox = new JComboBox<String>();
for (int month = 1; month <= 12; month++) {
monthComboBox.addItem(String.valueOf(month));
}
monthComboBox.setSelectedItem(String.valueOf(currentMonth));
monthComboBox.addActionListener(this);
monthLabel = new JLabel(getMonthString(currentMonth) + " " + currentYear);
topPanel.add(yearComboBox);
topPanel.add(monthComboBox);
topPanel.add(monthLabel);
// 创建日历面板
calendarPanel = new JPanel();
calendarPanel.setLayout(new GridLayout(7, 7));
// 显示当前月份的日历
updateCalendar(currentYear, currentMonth);
// 将顶部面板和日历面板添加到窗口中
add(topPanel, BorderLayout.NORTH);
add(calendarPanel, BorderLayout.CENTER);
// 设置窗口大小,可见性,关闭操作
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 根据年份和月份更新日历面板
private void updateCalendar(int year, int month) {
// 清空日历面板
calendarPanel.removeAll();
// 添加星期标签
JLabel[] weekLabels = { new JLabel("日"), new JLabel("一"), new JLabel("二"), new JLabel("三"), new JLabel("四"), new JLabel("五"), new JLabel("六") };
for (JLabel label : weekLabels) {
label.setHorizontalAlignment(JLabel.CENTER);
calendarPanel.add(label);
}
// 获取指定月份的第一天
Calendar c = Calendar.getInstance();
c.set(year, month - 1, 1);
int firstDayOfWeek = c.get(Calendar.DAY_OF_WEEK);
// 添加空白格子
for (int i = 1; i < firstDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
// 添加日期格子
int daysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int day = 1; day <= daysInMonth; day++) {
JLabel label = new JLabel(String.valueOf(day));
label.setHorizontalAlignment(JLabel.CENTER);
calendarPanel.add(label);
}
// 刷新窗口
validate();
repaint();
}
// 获取月份字符串
private String getMonthString(int month) {
String[] months = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
return months[month - 1];
}
// 处理年份和月份选择器的事件
public void actionPerformed(ActionEvent e) {
int year = Integer.parseInt((String) yearComboBox.getSelectedItem());
int month = Integer.parseInt((String) monthComboBox.getSelectedItem());
monthLabel.setText(getMonthString(month) + " " + year);
updateCalendar(year, month);
}
public static void main(String[] args) {
new CalendarProgram();
}
}
```
希望这可以帮助您开始开发自己的日历程序!
阅读全文