题目:java 日历

时间:2019.3.8

介绍:做一个窗体显示日历,最上边是两个按钮切换上下月,中间是日期展示,最下方显示当前的年月。

注:所有代码在Eclipse下调试运行。

代码:

窗体布局: Frame.java

package calendar;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Frame extends JFrame{
	JPanel panel1,panel2,panel3;//三个面板展示不同的内容
	JButton button1,button2;//两个按钮分别代表上月和下月
	String []str={"日","一","二","三","四","五","六"};
	JButton button[]=new JButton[str.length];
	JLabel [] label;
	JLabel now=new JLabel();
	calendarBean calen=new calendarBean();
	String []day;
	Frame(){
		init();
	}
	void init(){
		calen.setYear(2019);
		calen.setMonth(3);
		button1=new JButton("上月");
		button1.setActionCommand("last");
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				calen.actionPerformed(e);
			}
		});
		button2=new JButton("下月");
		button2.setActionCommand("next");
		button2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				calen.actionPerformed(e);
			}
		});
		panel1=new JPanel();
		panel1.add(button1);
		panel1.add(button2);
		GridLayout grid=new GridLayout(7,7);
		add(panel1,BorderLayout.NORTH);
		panel2=new JPanel();
		for(int i=0;i<str.length;i++){
			button[i]=new JButton(str[i]);
			panel2.add(button[i]);
		}
		day=calen.getCalendar();
		label=new JLabel[42];
		for(int i=0;i<42;i++){
			label[i]=new JLabel();
			panel2.add(label[i]);
		}
		calen.label=this.label;
		for(int i=0;i<day.length;i++){
			label[i].setText("        "+day[i]);
		}
		add(panel2,BorderLayout.CENTER);
		panel2.setLayout(grid);
		panel3=new JPanel();
		now.setText(calen.year+"年"+calen.month+"月");
		calen.now=now;
		panel3.add(now);
		add(panel3,BorderLayout.SOUTH);
	}
}

日历:calendarBean.java 

package calendar;

import javax.swing.*;
import java.awt.event.*;
import java.util.Calendar;

public class calendarBean implements ActionListener {
	int year = 0, month = 0;
	JLabel now;
	JLabel[] label;
	String[] day;

	public void setYear(int year) {
		this.year = year;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public String[] getCalendar() {
		String[] a = new String[42];
		Calendar rill = Calendar.getInstance();// 初始化一个日历对象
		rill.set(year, month - 1, 1);
		int weekday = rill.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 = 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;

	}

	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("last")) {
			month--;
			if (month == 0) {
				year--;
				month = 12;
			}
		} else if (str.equals("next")) {
			month++;
			if (month == 13) {
				year++;
				month = 1;
			}
		}
		now.setText(year + "年" + month + "月");
		day = getCalendar();
		for (int i = 0; i < day.length; i++) {
			label[i].setText("        " + day[i]);
		}
	}
}

main.java

package calendar;
import javax.swing.JFrame;
public class main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Frame f=new Frame();
		f.setTitle("日历");
		f.setBounds(100, 100, 400, 400);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

 运行截图:

java 日历控件 java窗口日历_初始化

java 日历控件 java窗口日历_i++_02