package calendarTest;
import javax.swing.*;
/**
* Created with IntelliJ IDEA.
* 启动入口
* @Auther: ljt
* @Version 1.0
* @Date: 2020/06/30/9:34
* @Description:
*/
public class CalendarMain {
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格
}catch (Exception e) {
e.printStackTrace();
}
//构建日期frame
CalendarFrame frame=new CalendarFrame();
//左,上,宽,高
frame.setBounds(100,100,360,300);
//窗体标题
frame.setTitle("日历");
//窗体居中显示
frame.setLocationRelativeTo(null);
//需要显示则使用true,需要隐藏则显示false。
frame.setVisible(true);
//窗体右上角退出
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package calendarTest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
/**
* Created with IntelliJ IDEA.
* 监听事件
*
* @Auther: ljt
* @Version 1.0
* @Date: 2020/06/30/9:33
* @Description:
*/
public class CalendarFrame extends JFrame implements ActionListener {
JLabel labelDay[]=new JLabel[42]; //天数
JButton titleName[]=new JButton[7]; // 周数
String name[]={"日","一","二","三", "四","五","六"}; //一周天数
JButton nextMonth,previousMonth;
//启动程序显示的日期信息
Calendar cal = Calendar.getInstance();
int year=cal.get(Calendar.YEAR); //当前年
int month=cal.get(Calendar.MONTH)+ 1;; //当前月
int day = cal.get(Calendar.DATE); //当前日
CalendarBean calendar;
JLabel showMessage=new JLabel("",JLabel.CENTER); //初始化左上角,当前时间文本展示
public CalendarFrame() {
//设置窗体颜色
setBackground(new Color(110, 123, 128));
JPanel pCenter=new JPanel();
pCenter.setBackground(new Color(110, 123, 128));
//将pCenter的布局设置为7行7列的GridLayout 布局。
pCenter.setLayout(new GridLayout(7,7));
//pCenter添加组件titleName[i]
for(int i=0;i<7;i++) {
titleName[i]=new JButton(name[i]);
pCenter.add(titleName[i]);
}
//pCenter添加组件labelDay[i]
for(int i=0;i<42;i++) {
labelDay[i]=new JLabel("",JLabel.CENTER);
pCenter.add(labelDay[i]);
}
//创建日历
calendar=new CalendarBean();
calendar.setYear(year); // 设置当前年
calendar.setMonth(month); //设置当前月
String day[]=calendar.getCalendar(); // 获取当月天数
//循环天数
for(int i=0;i<42;i++) {
labelDay[i].setText(day[i]);
}
nextMonth=new JButton("下月"); //设置主题下月按钮
previousMonth=new JButton("上月"); //设置主题上月按钮
//注册监听器
nextMonth.addActionListener(this); //下月按钮点击触发事件
previousMonth.addActionListener(this); //上月按钮点击触发事件
//创建主题面板,添加绑定,当前时间,上月、下月按钮
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(showMessage);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
//展示
showMessage.setText("当前日期:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" + this.day +"日" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
getContentPane().add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心区域
getContentPane().add(pNorth,BorderLayout.NORTH);// 窗口添加pNorth 在北面区域
getContentPane().add(pSouth,BorderLayout.SOUTH);// 窗口添加pSouth 在南区域。
}
/**
* 监听事件
* @param e
*/
public void actionPerformed(ActionEvent e) {
if(e.getSource()==nextMonth) { //点击下月按钮判断
month=month+1;
if(month>12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++) {
labelDay[i].setText(day[i]);
}
} else if(e.getSource()==previousMonth) { //点击上月按钮判断
month=month-1;
if(month<1)
month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i<42;i++) {
labelDay[i].setText(day[i]);
}
}
}
}
package calendarTest;
import java.util.Calendar;
/**
* Created with IntelliJ IDEA.
* 创建日历实体类
*
* @Auther: ljt
* @Version 1.0
* @Date: 2020/06/30/9:32
* @Description:
*/
public class CalendarBean {
String day[]; //天数数组
int year=0,month=0; // 年月
public void setYear(int year) {
this.year=year;
}
public int getYear() {
return year;
}
public void setMonth(int month) {
this.month=month;
}
public int getMonth() {
return month;
}
public String[] getCalendar() {
String a[]=new String[42];
Calendar date=Calendar.getInstance();
date.set(year,month,1);
int week=date.get(Calendar.DAY_OF_WEEK)-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=week,n=1;i<week+day;i++) {
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}