javaGUI显示日历小程序,不用字符串,使用下拉框,通过选择下拉框的年,月显示对应的当月日历,没有额外按钮,只通过选择下拉框显示日历
时间: 2023-10-03 21:09:13 浏览: 110
以下是一个简单的Java GUI显示日历的小程序,使用了下拉框来选择年月,并根据选择的年月动态生成当月的日历。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalendarApp implements ActionListener {
private JFrame frame;
private JComboBox<String> yearComboBox, monthComboBox;
private JLabel[] dayLabels;
private JPanel calendarPanel;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CalendarApp().createAndShowGUI();
}
});
}
public void createAndShowGUI() {
// 创建主窗口
frame = new JFrame("Calendar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// 创建选择年份和月份的下拉框
yearComboBox = new JComboBox<String>();
for (int i = 1900; i <= 2100; i++) {
yearComboBox.addItem(Integer.toString(i));
}
yearComboBox.addActionListener(this);
monthComboBox = new JComboBox<String>();
String[] months = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November", "December"};
for (String month : months) {
monthComboBox.addItem(month);
}
monthComboBox.addActionListener(this);
// 创建日历面板
calendarPanel = new JPanel(new GridLayout(7, 7));
// 创建星期标签
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
JLabel[] weekLabels = new JLabel[7];
for (int i = 0; i < 7; i++) {
weekLabels[i] = new JLabel(days[i], JLabel.CENTER);
calendarPanel.add(weekLabels[i]);
}
// 创建日期标签
dayLabels = new JLabel[42];
for (int i = 0; i < 42; i++) {
dayLabels[i] = new JLabel("", JLabel.CENTER);
calendarPanel.add(dayLabels[i]);
}
// 将下拉框和日历面板添加到主窗口
JPanel selectPanel = new JPanel();
selectPanel.add(yearComboBox);
selectPanel.add(monthComboBox);
frame.add(selectPanel, BorderLayout.NORTH);
frame.add(calendarPanel, BorderLayout.CENTER);
// 显示主窗口
frame.setVisible(true);
// 初始显示当前年月的日历
int year = Integer.parseInt(yearComboBox.getSelectedItem().toString());
int month = monthComboBox.getSelectedIndex() + 1;
showCalendar(year, month);
}
public void actionPerformed(ActionEvent e) {
int year = Integer.parseInt(yearComboBox.getSelectedItem().toString());
int month = monthComboBox.getSelectedIndex() + 1;
showCalendar(year, month);
}
private void showCalendar(int year, int month) {
// 计算当月第一天是星期几
int firstDayOfWeek = getFirstDayOfWeek(year, month);
// 计算当月有多少天
int daysInMonth = getDaysInMonth(year, month);
// 显示当月的日期
for (int i = 0; i < 42; i++) {
if (i < firstDayOfWeek || i >= firstDayOfWeek + daysInMonth) {
dayLabels[i].setText("");
} else {
int day = i - firstDayOfWeek + 1;
dayLabels[i].setText(Integer.toString(day));
}
}
}
private int getDaysInMonth(int year, int month) {
int daysInMonth;
switch (month) {
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
default:
daysInMonth = 31;
}
return daysInMonth;
}
private int getFirstDayOfWeek(int year, int month) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int totalDays = 0;
for (int i = 1900; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) {
totalDays += 366;
} else {
totalDays += 365;
}
}
for (int i = 0; i < month - 1; i++) {
totalDays += daysInMonth[i];
if (i == 1 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
totalDays++;
}
}
return (totalDays + 1) % 7;
}
}
```
该程序使用了 `JComboBox` 来选择年月,然后根据选择的年月动态生成对应的日历。其中,`getFirstDayOfWeek` 方法用于计算当月第一天是星期几,`getDaysInMonth` 方法用于计算当月有多少天。
阅读全文