Java 编写日历程序
程序员文章站
2022-04-05 23:39:24
...
直接贴代码
package com.chent.dome.rili;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author Administrator
*
*/
public class CalendarBean {
int year = 0, month = 0;
public CalendarBean() {
Calendar rili = Calendar.getInstance();
rili.setTime(new Date());
year = rili.get(Calendar.YEAR);
month = rili.get(Calendar.MONTH) + 1;// Calendar.MONTH应该等于实际月份减一
}
public void setYear(int _year) {
year = _year;
}
public void setMonth(int _month) {
month = _month;
}
public String[] getCalendar() {
String a[] = new String[42];
Calendar c = Calendar.getInstance();
c.set(year, month - 1, 1);
int WeekDay = c.get(Calendar.DAY_OF_WEEK) - 1;// 计算出1号的星期
int day = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
day = 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
day = 30;
}
if (month == 2) {
if ((year % 4 == 0) && (year % 100 != 0) || year % 400 == 0) {
day = 29;
} else {
day = 28;
}
}
for (int i = 0; i < WeekDay; i++) {
a[i] = " ";
}
for (int i = WeekDay, n = 1; i < WeekDay + day; i++) {
a[i] = String.valueOf(n);
n++;
}
for (int i = WeekDay + day; i < a.length; i++) {
a[i] = " ";
}
return a;
}
}
package com.chent.dome.rili;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author Administrator
*
*/
public class Calendar extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton week[][], previousMonth, nextMonth;
JLabel day[][], message;
String w[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel pCenter, pSouth;
CalendarBean cb;
String a[];
public Calendar(String s) {
super(s);
cb = new CalendarBean();
a = cb.getCalendar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel pNorth = new JPanel();
pNorth.setBackground(new Color(245, 222, 179));
pNorth.setLayout(new FlowLayout());
getContentPane().add(pNorth, BorderLayout.NORTH);
previousMonth = new JButton("\u4E0A\u6708");
previousMonth.setBackground(new Color(192, 192, 192));
previousMonth.addActionListener(this);
pNorth.add(previousMonth);
nextMonth = new JButton("\u4E0B\u6708");
nextMonth.setBackground(new Color(192, 192, 192));
nextMonth.addActionListener(this);
pNorth.add(nextMonth);
pCenter = new JPanel();
pCenter.setLayout(new GridLayout(7, 7));
pCenter.setBackground(new Color(255, 250, 240));
getContentPane().add(pCenter, BorderLayout.CENTER);
pSouth = new JPanel();
pSouth.setBackground(new Color(245, 222, 179));
pSouth.setLayout(new FlowLayout());
getContentPane().add(pSouth, BorderLayout.SOUTH);
message = new JLabel("日历:" + cb.year + "年" + cb.month + "月");
message.setFont(new Font("宋体", Font.BOLD, 15));
pSouth.add(message);
day=new JLabel[7][7];
week=new JButton[7][7];
for (int i = 0; i < 7; i++) {//周几用botton表示,日期用label表示
for (int j = 0; j < 7; j++) {
if (i == 0) {
week[i][j] = new JButton(w[j]);
week[i][j].setFont(new Font("宋体", Font.BOLD, 15));
week[i][j].setBackground(new Color(192, 192, 192));
pCenter.add(week[i][j]);
} else {
day[i][j] = new JLabel("");
day[i][j].setFont(new Font("黑体", Font.BOLD, 15));
day[i][j].setHorizontalAlignment(SwingConstants.CENTER);
pCenter.add(day[i][j]);
}
}
}
setBounds(100, 100, 450, 300);
Toolkit kit = Toolkit.getDefaultToolkit();// 设置窗体居中显示
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width / 2;
int screenHeight = screenSize.height / 2;
int windowWidth = getWidth();
int windowHeight = getHeight();
setLocation(screenWidth - windowWidth / 2, screenHeight - windowHeight / 2);
setVisible(true);// 窗口的可见性设置
PrintCalendar();
validate();
}
public void PrintCalendar() {
int c = 0;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (i != 0) {
day[i][j].setText(a[c++]);
}
}
}
message.setText("日历:" + cb.year + "年" + cb.month + "月");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == nextMonth) {
int m = cb.month + 1;
if (m == 13) {
int y = cb.year + 1;
cb.setYear(y);
m = 1;
}
cb.setMonth(m);
a = cb.getCalendar();
PrintCalendar();
} else if (e.getSource() == previousMonth) {
int m = cb.month - 1;
if (m == 0) {
int y = cb.year - 1;
cb.setYear(y);
m = 12;
}
cb.setMonth(m);
a = cb.getCalendar();
PrintCalendar();
}
}
}
package com.chent.dome.rili;
/**
*
* @author Administrator
*
*/
public class MainClass {
public static void main(String[] args) {
new Calendar("日历");//新建名为日历的窗口
}
}
看一下效果: