用到java图形交互的基础知识,可以练练手。
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormatSymbols;
import java.util.*;
import javax.swing.*;
public class WindowCalendar extends JFrame implements ActionListener {
private JButton previousButton, nextButton;
private JComboBox monthComboBox, yearComboBox;
private JLabel monthLabel, yearLabel;
private JPanel calendarPanel;
public WindowCalendar() {
setTitle("日历");
// 创建月份下拉框
String[] monthStrings = new DateFormatSymbols().getMonths();
monthComboBox = new JComboBox(monthStrings);
monthComboBox.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
monthComboBox.addActionListener(this);
// 创建年份下拉框
Integer[] yearIntegers = new Integer[200];
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
//System.out.println(currentYear);
for (int i = 0; i < yearIntegers.length; i++) {
yearIntegers[i] = new Integer(currentYear - 122 + i);
// System.out.println(yearIntegers[i]);
}
yearComboBox = new JComboBox(yearIntegers);
yearComboBox.setSelectedItem(new Integer(currentYear));
yearComboBox.addActionListener(this);
// 创建月份和年份标签
monthLabel = new JLabel("Month:");
yearLabel = new JLabel("Year:");
// 创建上一个和下一个按钮
previousButton = new JButton("<");
previousButton.addActionListener(this);
nextButton = new JButton(">");
nextButton.addActionListener(this);
// 创建日历面板
calendarPanel = new JPanel(new GridLayout(6, 7));
calendarPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
updateCalendar();
// 将组件添加到容器中
JPanel topPanel = new JPanel();
topPanel.add(monthLabel);
topPanel.add(monthComboBox);
topPanel.add(yearLabel);
topPanel.add(yearComboBox);
topPanel.add(previousButton);
topPanel.add(nextButton);
add(topPanel, BorderLayout.NORTH);
add(calendarPanel, BorderLayout.CENTER);
// 设置窗口属性
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == previousButton) {
moveBackwards();
updateCalendar();
}
else if (source == nextButton) {
moveForwards();
updateCalendar();
}
else if (source == monthComboBox || source == yearComboBox) {
updateCalendar();
}
}
private void moveBackwards() {
Calendar calendar = Calendar.getInstance();
calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
calendar.add(Calendar.MONTH, -1);
monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
}
private void moveForwards() {
Calendar calendar = Calendar.getInstance();
calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
calendar.add(Calendar.MONTH, 1);
monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
}
private void updateCalendar() {
int year = ((Integer)yearComboBox.getSelectedItem()).intValue();
int month = monthComboBox.getSelectedIndex();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
// 清除日历面板上的所有组件
calendarPanel.removeAll();
// 添加星期标签
String[] weekdayStrings = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
for (int i = 0; i < weekdayStrings.length; i++) {
JLabel label = new JLabel(weekdayStrings[i], JLabel.CENTER);
calendarPanel.add(label);
}
// 添加日期按钮
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i < weekday; i++) {
JLabel label = new JLabel();
calendarPanel.add(label);
}
for (int i = 1; i <= maxDays; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(new ButtonListener());
calendarPanel.add(button);
}
// 重新绘制日历
revalidate();
repaint();
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(WindowCalendar.this, "You clicked on day " + ((JButton)event.getSource()).getText());
}
}
public static void main(String[] args) {
new WindowCalendar();
}
}
效果图: