用java语言,综合运用GUI编程、事件处理、Calendar类应用等知识设计一款月历,要求能通过输入(或选择)年月的方式正确显示当前月份的所有日期。
时间: 2024-02-24 13:57:18 浏览: 71
好的,我会尽力回答你的问题。首先,我们可以使用Java Swing GUI框架来实现月历的设计。下面是一个示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MonthCalendar extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel monthLabel;
private JButton previousButton, nextButton;
private JPanel calendarPanel;
private Container container;
private Calendar calendar;
private int year, month, day;
public MonthCalendar() {
super("Month Calendar");
container = getContentPane();
container.setLayout(new BorderLayout());
// 创建月份标签
monthLabel = new JLabel("", JLabel.CENTER);
monthLabel.setForeground(Color.BLUE);
// 创建日历面板
calendarPanel = new JPanel(new GridLayout(0, 7));
calendarPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
// 创建上一个月按钮
previousButton = new JButton("<<");
previousButton.addActionListener(this);
// 创建下一个月按钮
nextButton = new JButton(">>");
nextButton.addActionListener(this);
// 添加组件到窗体
container.add(monthLabel, BorderLayout.NORTH);
container.add(calendarPanel, BorderLayout.CENTER);
container.add(previousButton, BorderLayout.WEST);
container.add(nextButton, BorderLayout.EAST);
// 设置窗口属性
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化日历
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DATE);
updateCalendar(year, month);
}
/**
* 更新日历面板
*
* @param year
* @param month
*/
private void updateCalendar(int year, int month) {
calendar.set(year, month, 1);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// 清空原有组件
calendarPanel.removeAll();
// 添加星期标签
String[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
for (int i = 0; i < weekDays.length; i++) {
JLabel weekLabel = new JLabel(weekDays[i], JLabel.CENTER);
weekLabel.setFont(new Font("Serif", Font.BOLD, 14));
calendarPanel.add(weekLabel);
}
// 填充日历面板
for (int i = 1; i < firstDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
for (int i = 1; i <= daysInMonth; i++) {
JButton dayButton = new JButton(Integer.toString(i));
dayButton.setFont(new Font("Serif", Font.PLAIN, 14));
dayButton.setPreferredSize(new Dimension(50, 50));
dayButton.addActionListener(new DateActionListener(i));
calendarPanel.add(dayButton);
}
// 更新月份标签
monthLabel.setText(year + "-" + (month + 1));
}
/**
* 上一个月按钮事件处理
*/
private void previousButtonActionPerformed() {
if (month == 0) {
year -= 1;
month = 11;
} else {
month -= 1;
}
updateCalendar(year, month);
}
/**
* 下一个月按钮事件处理
*/
private void nextButtonActionPerformed() {
if (month == 11) {
year += 1;
month = 0;
} else {
month += 1;
}
updateCalendar(year, month);
}
/**
* 日期按钮事件处理
*/
private class DateActionListener implements ActionListener {
private int day;
public DateActionListener(int day) {
this.day = day;
}
public void actionPerformed(ActionEvent e) {
System.out.println(year + "-" + (month + 1) + "-" + day);
}
}
/**
* 按钮事件处理
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == previousButton) {
previousButtonActionPerformed();
} else if (e.getSource() == nextButton) {
nextButtonActionPerformed();
}
}
public static void main(String[] args) {
new MonthCalendar();
}
}
```
上述代码实现了一个基本的月历,包括月份标签、日历面板、上一个月按钮、下一个月按钮等组件。通过`updateCalendar()`方法可以更新日历面板,通过`previousButtonActionPerformed()`和`nextButtonActionPerformed()`方法可以切换月份,通过`DateActionListener`类可以处理日期按钮的事件。你可以根据自己的需求进行修改和扩展。
阅读全文