Java 日历程序

2017-01-13 15:06:57来源:csdn作者:Google_huchun人点击

最近写了一个日历的小程序,分享给大家.

界面:

package com.example.calendar;import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class CalendarMain {/**
* @param args
*/
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 WindowAdapter() {// 点击叉号关闭frame程序
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}package com.example.calendar;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JOptionPane;public class CalendarFrame extends Frame implements ActionListener{ Label labelDay[] =new Label[42]; // 用来输出日历的天的数组
Button titleName[] = new Button[7]; // 周一到周日的按键
String name[] = new String[]{"日", "一", "二", "三", "四", "五", "六" };
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 center = new Panel(); //镶嵌
center.setLayout(new GridLayout(7,7));
for (int i = 0; i < 7; i++) { //给周日到周六的button添加显示文本
titleName[i] = new Button(name[i]);
center.add(titleName[i]);
}
for (int i = 0; i < 42; i++) {
labelDay[i] = new Label("", Label.CENTER);
center.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();
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(center);
add(scrollPane, BorderLayout.CENTER);
add(pNorth, BorderLayout.NORTH);
add(pSouth, BorderLayout.SOUTH);
}@Override
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 e2) {
JOptionPane.showMessageDialog(null, "请输入正确的年份及月份");
}
}
showMessage.setText("日历:" + calendar.getYear() + "年" + calendar.getMonth() + "月");}
}package com.example.calendar;import java.util.Calendar;//定义一个日历类
public class CalendarBean {String day[];//天数数组
int year = 2011, month = 9; //给定一个初始年月public void setYear(int year) {
this.year = year;
}public void setMonth(int month) {
this.month= month;
}public int getYear() {
return year;
}public int getMonth() {
return month;
}public String[] getCalendar() {//获得日历
String a[] = new String[42]; //定义一个以字符串数组
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, 1);
int week = calendar.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;
}
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台