编写具有图形用户界面(GUI)的日历组件显示日期和时间并进行适当 的功能扩充。Java
时间: 2024-02-09 13:10:46 浏览: 84
好的,我可以帮你回答这个问题。
首先,你可以使用Java Swing库来创建GUI界面。接下来,你需要考虑如何实现一个日历组件来显示日期和时间。下面是一个简单的实现:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CalendarComponent extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private Calendar calendar;
private JLabel monthLabel;
private JPanel calendarPanel;
public CalendarComponent() {
super("Calendar");
// 初始化日历对象
calendar = Calendar.getInstance();
// 创建面板
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel controlPanel = new JPanel(new GridLayout(1, 3));
calendarPanel = new JPanel(new GridLayout(0, 7));
// 创建月份标签和按钮
monthLabel = new JLabel("", JLabel.CENTER);
JButton prevMonthButton = new JButton("<<");
JButton nextMonthButton = new JButton(">>");
prevMonthButton.addActionListener(this);
nextMonthButton.addActionListener(this);
// 添加控件到面板
controlPanel.add(prevMonthButton);
controlPanel.add(monthLabel);
controlPanel.add(nextMonthButton);
mainPanel.add(controlPanel, BorderLayout.NORTH);
mainPanel.add(calendarPanel, BorderLayout.CENTER);
// 设置面板边框
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
// 创建日历
createCalendar();
// 添加面板到窗口
getContentPane().add(mainPanel);
// 设置窗口属性
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 创建日历
private void createCalendar() {
// 清空日历面板
calendarPanel.removeAll();
// 设置月份标签
SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
monthLabel.setText(sdf.format(calendar.getTime()));
monthLabel.setFont(new Font("Arial", Font.BOLD, 14));
// 设置星期标签
JLabel[] weekLabels = new JLabel[7];
String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (int i = 0; i < weekLabels.length; i++) {
weekLabels[i] = new JLabel(weekDays[i], JLabel.CENTER);
weekLabels[i].setFont(new Font("Arial", Font.BOLD, 12));
weekLabels[i].setForeground(Color.blue);
calendarPanel.add(weekLabels[i]);
}
// 设置日期按钮
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = 1;
calendar.set(year, month, day);
int startDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i < startDayOfWeek; i++) {
calendarPanel.add(new JLabel(""));
}
for (int i = 1; i <= daysInMonth; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(this);
calendarPanel.add(button);
}
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("<<")) { // 上个月
calendar.add(Calendar.MONTH, -1);
} else if (command.equals(">>")) { // 下个月
calendar.add(Calendar.MONTH, 1);
} else { // 日期按钮
JButton button = (JButton) e.getSource();
int day = Integer.parseInt(button.getText());
calendar.set(Calendar.DAY_OF_MONTH, day);
Date date = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
}
createCalendar();
}
public static void main(String[] args) {
new CalendarComponent();
}
}
```
这个程序创建了一个日历组件,并实现了以下功能:
1. 显示当前月份的日历;
2. 点击“<<”按钮可以显示上个月的日历;
3. 点击“>>”按钮可以显示下个月的日历;
4. 点击日期按钮可以输出该日期的字符串表示。
你可以根据需要添加更多的功能,例如选择日期和时间,添加事件等等。
阅读全文