日历显示读出输入的年月的java代码
import java.awt.*;
import java.awt.event.*;
import java.util.calendar;
import javax.swing.joptionpane;
class calendarbean { //定义一个日历类
string day[]; //天数数组
int year = 2011, month = 9; //给定一个初始年月
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 日历 = calendar.getinstance();
日历.set(year, month - 1, 1);
int 星期几 = 日历.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 = 星期几, n = 1; i < 星期几 + day; i++) {
a[i] = string.valueof(n);
n++;
}
return a;
}
}
class calendarframe extends frame implements actionlistener {
label labelday[] = new label[42]; //用来输出日历的天的数组
button titlename[] = new button[7]; //周一到周天的按键
string name[] = { "日", "一", "二", "三", "四", "五", "六" };
textfield text1, text2; //定义输入的年份和月份
button nextmonth, previousmonth, enter; //下个月,上个月,确定
label lab1, lab2, lab3; //几个文本类型的字符串
int year = 2012, month = 9;
calendarbean calendar; //顶一个量
label showmessage = new label("", label.center); // 定义一个用于显示 当前年月的label 表明,标签上应为中心。
public calendarframe() { //窗体类
panel pcenter = new panel(); //镶嵌
pcenter.setlayout(new gridlayout(7, 7));
for (int i = 0; i < 7; i++) { //给周日到周六的button添加显示文本
titlename[i] = new button(name[i]);
pcenter.add(titlename[i]);
}
for (int i = 0; i < 42; i++) {
labelday[i] = new label("", label.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]);
}
lab1 = new label("请输入日期"); //调用一个方法 new一个对象
lab2 = new label("年份");
lab3 = new label("月份");
enter = new button("确定");
text1 = new textfield(10);
text2 = new textfield(5);
nextmonth = new button("下月");
previousmonth = new button("上月");
enter.addactionlistener(this); //添加指定的动作侦听器
nextmonth.addactionlistener(this);
previousmonth.addactionlistener(this);
panel pnorth = new panel(), psouth = new panel();
pnorth.add(lab1);
pnorth.add(lab2);
pnorth.add(text1);
pnorth.add(lab3);
pnorth.add(text2);
pnorth.add(enter);
pnorth.add(previousmonth);
pnorth.add(nextmonth);
psouth.add(showmessage);
showmessage.settext("日历:" + calendar.getyear() + "年" + calendar.getmonth() + "月");
scrollpane scrollpane = new scrollpane(); //实现单个子组件的自动水平和/或垂直滚动的容器类
scrollpane.add(pcenter);
add(scrollpane, borderlayout.center);
add(pnorth, borderlayout.north);
add(psouth, borderlayout.south);
}
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]);
}
} else {
string yea = text1.gettext();
string mon = text2.gettext();
try {
year = integer.parseint(yea); //把string转成int类型
month = integer.parseint(mon);
if (month > 12 || month < 1 || year < 1) { //错误输入的处理
joptionpane.showmessagedialog(null, "请输入正确月份或月份");
return;
} else {
calendar.setyear(year);
calendar.setmonth(month);
}
string day[] = calendar.getcalendar();
for (int i = 0; i < 42; i++) {
labelday[i].settext(day[i]);
}
} catch (numberformatexception ee) {
joptionpane.showmessagedialog(null, "请输入正确的年份及月份");
}
}
showmessage.settext("日历:" + calendar.getyear() + "年" + calendar.getmonth() + "月");
}
}
public class calendarmainclass {
public static void main(string args[]) {
calendarframe frame = new calendarframe();
frame.settitle("日历");
frame.setbounds(300, 200, 500, 300);
frame.setvisible(true); //显示窗口
frame.validate(); //使生效
frame.addwindowlistener(new java.awt.event.windowadapter() { //点击叉号关闭frame程序
public void windowclosing(java.awt.event.windowevent e) {
system.exit(0);
}
});
}
}
推荐阅读