(Java代码)编写具有图形用户界面(GUI)的日历组件显示日期和时间。 ·主要功能: 1、显示当月日历,当前日期、当前时间; 2、可查询任意月以及任意年的日历: 3、正常运行和退出程序。
时间: 2024-02-04 15:03:47 浏览: 162
以下是一个简单的Java Swing程序,可以实现一个具有图形用户界面的日历组件,能够显示当前日期和时间,并可查询任意月份和年份的日历。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalendarGUI extends JFrame implements ActionListener {
private JLabel titleLabel, dateLabel, timeLabel;
private JButton prevMonthButton, nextMonthButton, prevYearButton, nextYearButton, goButton;
private JTextField monthField, yearField;
private JPanel calendarPanel;
private Calendar cal;
public CalendarGUI() {
super("Calendar");
// 设置窗口大小和位置
setSize(500, 400);
setLocationRelativeTo(null);
// 创建标题标签
titleLabel = new JLabel("Calendar");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
// 创建日期和时间标签
dateLabel = new JLabel();
timeLabel = new JLabel();
updateTime();
// 创建查询按钮和文本框
prevMonthButton = new JButton("<");
nextMonthButton = new JButton(">");
prevYearButton = new JButton("<<");
nextYearButton = new JButton(">>");
goButton = new JButton("Go");
monthField = new JTextField(2);
yearField = new JTextField(4);
// 设置按钮和文本框的事件监听器
prevMonthButton.addActionListener(this);
nextMonthButton.addActionListener(this);
prevYearButton.addActionListener(this);
nextYearButton.addActionListener(this);
goButton.addActionListener(this);
monthField.addActionListener(this);
yearField.addActionListener(this);
// 创建日历面板
calendarPanel = new JPanel(new GridLayout(0, 7));
calendarPanel.setBorder(BorderFactory.createEtchedBorder());
// 添加组件到窗口中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(titleLabel, BorderLayout.NORTH);
contentPane.add(dateLabel, BorderLayout.CENTER);
contentPane.add(timeLabel, BorderLayout.SOUTH);
contentPane.add(prevMonthButton, BorderLayout.WEST);
contentPane.add(nextMonthButton, BorderLayout.EAST);
contentPane.add(prevYearButton, BorderLayout.WEST);
contentPane.add(nextYearButton, BorderLayout.EAST);
contentPane.add(monthField, BorderLayout.WEST);
contentPane.add(yearField, BorderLayout.EAST);
contentPane.add(goButton, BorderLayout.CENTER);
contentPane.add(calendarPanel, BorderLayout.SOUTH);
// 初始化日历
cal = Calendar.getInstance();
updateCalendar(cal);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
// 处理按钮事件
if (source == prevMonthButton) {
if (month == Calendar.JANUARY) {
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.YEAR, year - 1);
} else {
cal.set(Calendar.MONTH, month - 1);
}
updateCalendar(cal);
} else if (source == nextMonthButton) {
if (month == Calendar.DECEMBER) {
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.YEAR, year + 1);
} else {
cal.set(Calendar.MONTH, month + 1);
}
updateCalendar(cal);
} else if (source == prevYearButton) {
cal.set(Calendar.YEAR, year - 1);
updateCalendar(cal);
} else if (source == nextYearButton) {
cal.set(Calendar.YEAR, year + 1);
updateCalendar(cal);
} else if (source == goButton || source == monthField || source == yearField) {
try {
int newMonth = Integer.parseInt(monthField.getText()) - 1;
int newYear = Integer.parseInt(yearField.getText());
if (newMonth < Calendar.JANUARY || newMonth > Calendar.DECEMBER || newYear < 0) {
throw new IllegalArgumentException();
}
cal.set(Calendar.MONTH, newMonth);
cal.set(Calendar.YEAR, newYear);
updateCalendar(cal);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input");
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, "Invalid month or year");
}
}
}
private void updateCalendar(Calendar cal) {
// 清空日历面板
calendarPanel.removeAll();
// 创建日历标题
String[] daysOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (String day : daysOfWeek) {
JLabel label = new JLabel(day, JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder());
calendarPanel.add(label);
}
// 获取当前月份的第一天
Calendar tempCal = (Calendar) cal.clone();
tempCal.set(Calendar.DAY_OF_MONTH, 1);
// 计算当前月份的总天数
int numDays = tempCal.getActualMaximum(Calendar.DAY_OF_MONTH);
// 计算当前月份的第一天是星期几
int firstDayOfWeek = tempCal.get(Calendar.DAY_OF_WEEK) - 1;
// 添加当前月份的日期到日历面板中
for (int i = 0; i < firstDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
for (int i = 1; i <= numDays; i++) {
JLabel label = new JLabel(Integer.toString(i), JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder());
calendarPanel.add(label);
}
// 更新窗口标题和日期标签
titleLabel.setText(cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " " + cal.get(Calendar.YEAR));
dateLabel.setText(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()) + ", " + cal.get(Calendar.DAY_OF_MONTH));
// 重新绘制窗口
revalidate();
repaint();
}
private void updateTime() {
// 更新时间标签
Date now = new Date();
timeLabel.setText(now.toString());
// 每秒钟更新一次时间
Timer timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date now = new Date();
timeLabel.setText(now.toString());
}
});
timer.start();
}
public static void main(String[] args) {
CalendarGUI app = new CalendarGUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
```
阅读全文