基于jdk8和swing图形写一个万年历
时间: 2023-07-18 09:45:21 浏览: 92
基于Java Swing 的万年历
好的,我可以为你提供一个简单的万年历的实现。首先是界面的代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
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 javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CalendarFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private String[] weekDays = { "日", "一", "二", "三", "四", "五", "六" };
private JButton lastMonth, nextMonth;
private JLabel title;
private JPanel calendarPanel;
private int year, month, day;
public CalendarFrame() {
super("万年历");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel topPanel = new JPanel(new FlowLayout());
lastMonth = new JButton("上个月");
nextMonth = new JButton("下个月");
lastMonth.addActionListener(this);
nextMonth.addActionListener(this);
title = new JLabel("", SwingConstants.CENTER);
topPanel.add(lastMonth);
topPanel.add(title);
topPanel.add(nextMonth);
contentPane.add(topPanel, BorderLayout.NORTH);
calendarPanel = new JPanel(new GridLayout(7, 7));
calendarPanel.setPreferredSize(new Dimension(300, 200));
contentPane.add(calendarPanel, BorderLayout.CENTER);
// 获取当前日期
Calendar now = Calendar.getInstance();
year = now.get(Calendar.YEAR);
month = now.get(Calendar.MONTH) + 1;
day = now.get(Calendar.DAY_OF_MONTH);
updateCalendar(year, month);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new CalendarFrame();
}
private void updateCalendar(int year, int month) {
// 清空面板上的所有组件
calendarPanel.removeAll();
// 设置标题
title.setText(year + "年" + month + "月");
// 设置字体
Font font = new Font("宋体", Font.PLAIN, 14);
// 添加星期标签
for (int i = 0; i < weekDays.length; i++) {
JLabel label = new JLabel(weekDays[i], SwingConstants.CENTER);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setFont(font);
calendarPanel.add(label);
}
// 获取当月第一天的星期
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
// 添加前面的空白格子
for (int i = 0; i < weekDay; i++) {
calendarPanel.add(new JLabel(""));
}
// 添加日期格子
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= maxDay; i++) {
JButton button = new JButton(String.valueOf(i));
button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
button.setFont(font);
button.addActionListener(this);
// 标记当前日期
if (year == this.year && month == this.month && i == day) {
button.setBackground(Color.YELLOW);
}
calendarPanel.add(button);
}
// 重绘面板
calendarPanel.validate();
calendarPanel.repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
int y = year, m = month;
if (e.getSource() == lastMonth) {
if (month == 1) {
y--;
m = 12;
} else {
m--;
}
} else if (e.getSource() == nextMonth) {
if (month == 12) {
y++;
m = 1;
} else {
m++;
}
} else {
JButton button = (JButton) e.getSource();
int d = Integer.parseInt(button.getText());
y = year;
m = month;
day = d;
}
updateCalendar(y, m);
}
}
```
这个程序使用了Swing图形库,可以在JDK8中编译运行。主要实现了以下功能:
1. 显示当前日期的万年历。
2. 点击上个月或下个月按钮可以切换月份。
3. 点击日期按钮可以直接跳转到对应的日期。
4. 标记当前日期,方便查看。
界面比较简单,使用了GridLayout布局来排列日期格子。updateCalendar方法根据年份和月份生成日期格子,并将它们添加到面板上。使用了Calendar类来获取当前日期和计算每个月的天数。actionPerformed方法监听按钮点击事件,根据点击的按钮来切换月份或跳转日期。
希望这个简单的实现能够帮助你完成你的需求。
阅读全文