用Java帮我完成以下要求的代码: 1.用Java完成日历和时钟,时钟和日历在一个程序里用GUI完成 2.时钟的指针会随着时间而转动 3.必须使用Java中的Calendar类 4.日历可以翻页且要有星期日到星期一的头标
时间: 2024-01-24 19:16:48 浏览: 62
时钟——用java语言编写。。。
以下是用Java完成日历和时钟的代码,时钟和日历在一个程序里用GUI完成,时钟的指针会随着时间而转动,必须使用Java中的Calendar类,日历可以翻页且有星期日到星期一的头标:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
public class ClockAndCalendar extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel clockLabel, calendarLabel;
private Timer clockTimer;
private JButton prevMonthButton, nextMonthButton;
private JPanel calendarPanel;
public ClockAndCalendar() {
super("Clock and Calendar");
// Initialize clock label
clockLabel = new JLabel("", SwingConstants.CENTER);
clockLabel.setFont(new Font("Serif", Font.BOLD, 48));
updateClock();
getContentPane().add(clockLabel, BorderLayout.NORTH);
// Initialize calendar panel
calendarPanel = new JPanel(new GridLayout(0, 7));
calendarPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
getContentPane().add(calendarPanel, BorderLayout.CENTER);
// Initialize calendar header
String[] daysOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (String day : daysOfWeek) {
JLabel dayLabel = new JLabel(day, SwingConstants.CENTER);
dayLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
calendarPanel.add(dayLabel);
}
// Initialize previous month button and add action listener
prevMonthButton = new JButton("<");
prevMonthButton.addActionListener(this);
getContentPane().add(prevMonthButton, BorderLayout.WEST);
// Initialize next month button and add action listener
nextMonthButton = new JButton(">");
nextMonthButton.addActionListener(this);
getContentPane().add(nextMonthButton, BorderLayout.EAST);
// Initialize calendar label and update calendar
calendarLabel = new JLabel("", SwingConstants.CENTER);
calendarLabel.setFont(new Font("Serif", Font.BOLD, 24));
updateCalendar();
getContentPane().add(calendarLabel, BorderLayout.SOUTH);
// Initialize clock timer
clockTimer = new Timer(1000, this);
clockTimer.start();
// Set window properties
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clockTimer) {
updateClock();
} else if (e.getSource() == prevMonthButton) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
updateCalendar(calendar);
} else if (e.getSource() == nextMonthButton) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
updateCalendar(calendar);
}
}
private void updateClock() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String time = String.format("%02d:%02d:%02d", hour, minute, second);
clockLabel.setText(time);
}
private void updateCalendar() {
Calendar calendar = Calendar.getInstance();
updateCalendar(calendar);
}
private void updateCalendar(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year, month, 1);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int daysInPreviousMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) - daysInMonth;
int daysInNextMonth = (7 - ((daysInMonth + daysInPreviousMonth + firstDayOfWeek - 1) % 7)) % 7;
int totalDays = daysInMonth + daysInPreviousMonth + daysInNextMonth;
String[] days = new String[totalDays];
for (int i = 0; i < totalDays; i++) {
if (i < daysInPreviousMonth) {
days[i] = Integer.toString(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) - daysInPreviousMonth + i + 1);
} else if (i < daysInMonth + daysInPreviousMonth) {
days[i] = Integer.toString(i - daysInPreviousMonth + 1);
} else {
days[i] = Integer.toString(i - daysInMonth - daysInPreviousMonth + 1);
}
JLabel dayLabel = new JLabel(days[i], SwingConstants.CENTER);
dayLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
if (i >= daysInPreviousMonth && i < daysInMonth + daysInPreviousMonth) {
dayLabel.setForeground(Color.BLACK);
} else {
dayLabel.setForeground(Color.GRAY);
}
calendarPanel.add(dayLabel);
}
String monthName = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG);
calendarLabel.setText(monthName + " " + year);
}
public static void main(String[] args) {
new ClockAndCalendar();
}
}
```
阅读全文