爪哇农历太阳节

成套练习;

导入Java . awt . *;

导入Java . awt . event . *;

导入Java . lang . string buffer;

导入javax . swing . *;

导入Java . util . *;

导入javax . swing . timer;

导入javax . swing . border . *;

公共类AidyCalender扩展JFrame实现ActionListener,

ItemListener {

Date Date = new Date();

private Gregorian calendar Gregorian calendar = new Gregorian calendar();

//定义中英文字符数组存储星期信息进行转换显示。

private String[]String weeken = new String[]{ " SUN "," MON "," TUE "," WED ",

“THU”、“星期五”、“星期六”};

private string[]string weekcn = new string[]{ "星期日","星期一","星期二","星期三",

“周四”、“周五”、“周六”;

//定义存储月份的信息数组,用于转换显示。

private String[]String monthen = new String[]{ " Jan "," Feb "," Mar "," Apr ",

“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};

Private String[] stringMonthCn = { "一月","二月","三月","四月","五月","六月",

七月、八月、九月、十月、十一月和十二月;

private String[]sysNowTime = new String[6];// sysNowTime是用于存储系统时间的变量。

private String[]sys runtime = new String[6];

private JLabel[]label week = new JLabel[7];

private JLabel[]label day = new JLabel[42];

private JLabel label time = new JLabel();

私有JPanel panel 1 = new JPanel();

private JPanel panel 2 = new JPanel();

private JPanel panel 3 = new JPanel();

private JComboBox combo 1 = new JComboBox();

private JComboBox combo 2 = new JComboBox();

private JButton button today = new JButton();

private Border Border = Border factory . createraisedbevelborder();

私有边框Border 1 = Border factory . create line Border(color . cyan,3);

公共AidyCalender(字符串标题){

超级(标题);

for(int y = 1900;y & lt2101;y++) {

Combo1。additem("+新整数(y)。tostring()+" Year ");

}

for(int m = 0;m & lt12;m++) {

combo 2 . additem("+stringMonthCn[m]);

}

button today . settext(" today ");

set layout(new flow layout());

add(panel 1);

添加(panel 2);

添加(panel 3);

panel1.setLayout(新GridLayout(1,3,10,0));

panel 1 . add(combo 1);

combo 1 . additem listener(this);

panel 1 . add(combo 2);

combo 2 . additem listener(this);

panel 1 . add(button today);

button today . addactionlistener(this);

LabelTime.setFont(新字体("ì",字体。平原,16));

labelTime.setForeground(Color。洋红色);

panel 1 . add(label time);

Timer time = new Timer(1000,new Timer listener());

time.addActionListener(新timer listener());

//time . set repeats(true);

time . start();

//label time . addancestorlistener(new timer listener());

panel 2 . set layout(new GridLayout(7,7,0,10));

panel 2 . set background(color . white);

for(int I = 0;我& lt7;i++) {

label week[I]= new JLabel();

labelWeek[i]。sethorizontal alignment(0);

if (i == 0 || i == 6) {

labelWeek[i]。set background(color . blue);

labelWeek[i]。setForeground(颜色。红色);

LabelWeek[i]。setFont(新字体(“粗体”,字体。粗体,14));

}

否则{

labelWeek[i]。setForeground(颜色。黑色);

LabelWeek[i]。setFont(新字体(“新歌体”,字体。平原,14));

}

labelWeek[i]。setText(string weekcn[I]);

panel 2 . add(label week[I]);

}

for(int I = 0;我& lt42;i++) {

label day[I]= new JLabel();

标签日[i]。sethorizontal alignment(0);

标签日[i]。setText(" ");

panel 2 . add(label day[I]);

}

addWindowListener(new window adapter(){

public void window closing(window event e){

system . exit(0);

}

});

setSize(300,300);

setBounds(250,200,400,360);

set visible(true);

setResizable(false);

setDefaultCloseOperation(JFrame。EXIT _ ON _ CLOSE);

get sysdate();

setNowDate();

}

public void action performed(action event AE){

if(AE . getsource()= = button today){

setNowDate();

}

}

public void itemStateChanged(item event aa){

setChangeDate();

}

public int turnWeek(字符串周){

int I;

for(I = 0;我& lt7;i++)

if(week . equalsignorecase(string weeken[I]))

打破;

返回I;

}

public int turn month(String month){

/**

* int turnMonth(字符串月)

*

* @month系统日期中的月份,如Jan\Feb

* @return int返回一个整数值,用于在stringMonthCn[]数组中查找对应的中国月份。

*/

int I;

for(I = 0;我& lt12;i++)

if(month . equalsignorecase(string monthen[I]))

打破;

返回I;

}

/**

* setNowDate()设置当前系统日期。

*/

public void setNowDate() {

setSysDate(getnoweyear()、getnowmount());

getSysRunDate();

setdate null();

combo 1 . setselectedindex(getShowYear()-1900);

combo 2 . setselectedindex(getShowMonth());

setDays(getMonthDays(getnowhear()、getNowMonth()),

getInitWeek(sysRunTime[0]),getNowDay();

//label time . settext(sysNowTime[3]);

//label time . sethorizontal alignment(0);

}

/**

* Integer getShowYear()获取应在组合框中显示的年份。

*/

public void setSysDate(int year,int month) {

gregorianCalendar.set(年,月,1);

}

public void setDateNull() {

for(int I = 0;我& lt42;i++) {

标签日[i]。setText(" ");

}

}

public void setChangeDate() {

setSysDate(getComboYear()、getComboMonth());

getSysRunDate();

setdate null();

setDays(getMonthDays(getComboYear()、getComboMonth()),

getInitWeek(sysRunTime[0]),-1);

}

public int getMonthDays(int year,int month) {

/**

*返回所选年份和月份的天数。因为数组中的值从0开始,3\5\8\10分别代表4\6\9\11个月。

* 1代表二月。判断是不是闰年后,选择回到28或29日。其他月份都是大月份,回到31天。

**/

开关(月){

案例三:

案例5:

案例8:

案例10:

返回30;//流产返回30天

案例1:

if(Gregorian calendar . isleapyear(year)){

// isLeapYear(year)确定当前纪元中的指定年份是否为闰年。

返回29;

}否则{

返回28;

}//二月返回闰年29天,平年28天。

默认值:

返回31;

//大岳返回31天。

}

}

/**

* int getComboYear()获取组合框中的年份。

*/

public void getSysDate() {

date = Gregorian calendar . gettime();

sysNowTime = (date.toString())。拆分(" ");

}

public void getSysRunDate() {

date = Gregorian calendar . gettime();

sysRunTime = (date.toString())。拆分(" ");

}

public int getComboYear() {

return combo 1 . getselectedindex()+1900;

}

/**

* int getComboMonth()获取月份组合框中的整数值。

*/

public int getComboMonth() {

返回combo 2 . getselectedindex();

}

public int getInitWeek(String init week){

/**

* getWeekNow(字符串initWeek)

*

* @para nowWeek,系统日期中的星期返回到数字1开始的月份中的某一天。

*/

int now week = 0;

for(int I = 0;我& lt7;i++) {

if(init week . equalsignorecase(string weeken[I]){

now week = I;

打破;

}

}

下周立即返回;

}

public int getnoweyear(){

返回integer . parse int(sysNowTime[5]);

}

public int getNowMonth() {

int now month = 0;

for(int I = 0;我& lt12;i++) {

if (sysNowTime[1]。equalsIgnoreCase(string monthen[I]))

now month = I;

打破;

}

立即返回每月;

}

public int getNowDay() {

返回integer . parse int(sysNowTime[2]);

}

公共整数getShowYear() {

返回integer . parse int(sysNowTime[5]);

}

公共整数getShowMonth() {

/**

* Integer getShowMonth()获取分组框中显示的中文格式的月份,如July \ August等。

*/

return turn month(sysNowTime[1]);

}

public void setDays(int monthDays,int initWeek,int day) {

/**

* void setDays(int monthDays,int initWeek,int day)

*

* @para monthDays本月天数

* @para initWeek第一周

* @para day设定今天的日历。

*/

setdate null();

for(int I = init week;我& ltinit week+month days+1;i++) {

if ((i - initWeek + 1) == day) {

标签日[i]。set border(border 1);

标签日[i]。setForeground(颜色。蓝色);

标签日[i]。setFont(新字体(“粗体”,字体。粗体,20));

} else if ((i % 7 == 0) || (i % 7 == 6))

标签日[i]。setForeground(颜色。红色);

否则{

标签日[i]。setForeground(颜色。黑色);

}

标签日[i]。setText(string . value of(I-init week+1));

}

for(int I = init week+month days;我& lt42;i++)

标签日[i]。setText(" ");

}

类TimerListener实现ActionListener {

//adapter demovar = new adapter demo("万年历程序-Aidy ");

public void action performed(action event e){

Gregorian calendar g = new Gregorian calendar();

String clock = new String((g . gettime()。toString()。split(" ")[3]);

labelTime.setText(时钟);

}

}

公共静态void main(String args[]) {

尝试{

ui manager . setlookandfeel(ui manager . getsystemlookandfeelclassname());

} catch(异常e) {

抛出新的runtime exception(e);

}

Aidy日历var =新aidy日历(“万年历程序-在线”);

}

}