JAVA万年历.docx

上传人:b****6 文档编号:7970538 上传时间:2023-01-27 格式:DOCX 页数:17 大小:20.01KB
下载 相关 举报
JAVA万年历.docx_第1页
第1页 / 共17页
JAVA万年历.docx_第2页
第2页 / 共17页
JAVA万年历.docx_第3页
第3页 / 共17页
JAVA万年历.docx_第4页
第4页 / 共17页
JAVA万年历.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

JAVA万年历.docx

《JAVA万年历.docx》由会员分享,可在线阅读,更多相关《JAVA万年历.docx(17页珍藏版)》请在冰豆网上搜索。

JAVA万年历.docx

JAVA万年历

importjava.io.*;//引入相關物件

importjava.util.*;

importjava.lang.Thread;

importjava.awt.BorderLayout;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjavax.swing.BorderFactory;

importjavax.swing.ComboBoxModel;

importjavax.swing.DefaultComboBoxModel;

importjavax.swing.JButton;

importjavax.swing.JComboBox;

importjavax.swing.JDesktopPane;

importjavax.swing.JLabel;

importjavax.swing.JTextArea;

importjavax.swing.JTextField;

importjavax.swing.JScrollPane;

importjavax.swing.WindowConstants;

importjavax.swing.border.BevelBorder;

importjavax.swing.border.LineBorder;

importjavax.swing.border.SoftBevelBorder;

publicclassNewJFrameextendsjavax.swing.JFrame//建立一視窗物件

{

privateJDesktopPanejDesktopPane1,jDesktopPane2;//相關物件宣告

privateJButtonjButton1,jButton2,jButton3;

privateJLabeljLabel1,jLabel2,jLabel3,jLabel4,jLabel5,jLabel6,jLabel7,jLabel8;

privateJComboBoxjComboBox1;//222

privateJTextFieldjTextField1;//214

privateJTextAreajTextArea1;//214

publicstaticvoidmain(String[]args)throwsIOException//主程式開始

{

NewJFrameinst=newNewJFrame();

inst.setVisible(true);

}//主程式結束

publicNewJFrame()//建立視窗開始

{

super();

initGUI();//呼叫GUI函數

}//建立視窗結束

publicint[]getdate()//取得系統日期函數開始

{

int[]date_array=newint[3];

Calendarca=newGregorianCalendar();

date_array[0]=ca.get(Calendar.YEAR);//年

date_array[1]=ca.get(Calendar.MONTH)+1;//月

date_array[2]=ca.get(Calendar.DAY_OF_MONTH);//日

returndate_array;//回傳自訂日期陣列

}//取得系統日期函數結束

publicvoidnew_btn()//重新產生日期按鈕函數開始

{

jTextArea1.setText("");//清空記事

intyear,month;

year=Integer.parseInt(jLabel5.getText().substring(0,4));//設定為已選擇的年

month=Integer.parseInt(jLabel5.getText().substring(7,9));//設定為已選擇的月

date_btn_create(year,month);//呼叫產生日期按鈕函數

}//重新產生日期按鈕函數結束

publicvoiddate_btn_create(intyear,intmonth)//產生日期按鈕

{

inty=0,x=0,x_add=0,y_add=0,week_add=0,date_acc=0,week_of_day=0;

Stringsyear,smonth,sday,filename;

syear=String.valueOf(year);

smonth=String.valueOf(month);

if(smonth.length()==1)//若小於10月(一位數)就在前面加0

smonth="0"+smonth;

jDesktopPane1.remove(jDesktopPane2);//移除桌面2(日期按鈕附著,也就是把日期按鈕移除)

jDesktopPane2=newJDesktopPane();//產生一個新的桌面

jDesktopPane1.add(jDesktopPane2);

jDesktopPane2.setBounds(0,30,252,196);//設定大小及位置

jDesktopPane2.setBackground(newjava.awt.Color(148,205,176));//設定顏色

switch(month)//設定月份天數

{

case1:

//大月31天

case3:

case5:

case7:

case8:

case10:

case12:

date_acc=31;

break;

case4:

//小月30天

case6:

case9:

case11:

date_acc=30;

break;

case2:

if(leap_year(year))

date_acc=29;

else

date_acc=28;

}

week_of_day=dow(year,month,1);//呼叫星期函數(取得當月第一天為星期幾)

switch(week_of_day)//設定當月1日位置(像素)

{

case0:

week_add=0;

break;

case1:

week_add=34;

break;

case2:

week_add=68;

break;

case3:

week_add=102;

break;

case4:

week_add=136;

break;

case5:

week_add=170;

break;

case6:

week_add=204;

break;

}

JButtonbtn[]=newJButton[date_acc];//建立日期按鈕陣列

for(inti=0;i

{

btn[i]=newJButton();//建立對應日期按鈕

jDesktopPane2.add(btn[i]);//加到桌面2上

btn[i].setText(String.valueOf(i+1));//設定按鈕文字為日期

if((i-week_of_day>0&&(i+week_of_day)%7==0)||((i+week_of_day)%7==0&&i!

=0))

{//設定當月第一天日期按鈕位置

x=0;//X軸座標

x_add=0;//下一個按鈕座標(X軸)加值

y++;//Y軸座標

y_add+=10;//換行座標(Y軸)加值

week_add=0;//當月第一日按鈕座標加值

}//下面設定按鈕大小及加值(X為起始10+第幾個按鈕*橫向寬度25+間隔+當月第一天星期幾加值)

btn[i].setBounds(10+x*25+x_add+week_add,y*20+y_add,25,20);//(Y為第幾個按鈕*高度20+換行加值)按鈕寬為25高為20

btn[i].setFont(newjava.awt.Font("Arial",1,12));//設定字體大小及樣式

btn[i].setBorder(BorderFactory.createTitledBorder(""));//設定按鈕文字不自動調整大小

int[]now=newint[3];

now=getdate();//取得當天日期

if(year==now[0]&&month==now[1]&&i+1==now[2])

btn[i].setBackground(newjava.awt.Color(233,238,164));//若為當天則設定按鈕為黃色

else

btn[i].setBackground(newjava.awt.Color(148,205,176));//若不是當天則設定按鈕為綠色

sday=String.valueOf(i+1);

filename=syear+smonth+sday;//記事檔案名稱(年+月+日.txt)

Filefile=newFile(filename+".txt");//建立檔案物件

if(file.exists())//若當天有記事檔案則設定按鈕字體顏色為藍色

btn[i].setForeground(newjava.awt.Color(0,0,255));

btn[i].addActionListener(newActionListener(){//設定按鈕監聽(觸發事件函數)

publicvoidactionPerformed(ActionEventevt){

btnActionPerformed(evt);

}

});

x++;//下一個按鈕X座標加權

x_add+=9;//下一個按鈕間隔X座標加權

}

}

publicbooleanleap_year(intyear)//判斷閏年函數開始

{

booleanleap_year;//4的倍數,若為100的倍數則必須為4的倍數才是閏年

if(year%4==0)

{

if(year%100==0)

{

if(year%400==0)

leap_year=true;

else

leap_year=false;

}

else

leap_year=true;

}

else

leap_year=false;

returnleap_year;

}//判斷閏年函數結束

publicintdow(inty,intm,intd)//判斷星期幾

{

int[]ww={6,2,2,5,0,3,5,1,4,6,2,4};//天文星體運行值

intw;

w=ww[m-1]+y+(y/4)-(y/100)+(y/400);//閏年設定

if(((y%4)==0)&&(m<3))//公式

{

w--;

if((y%100)==0)w++;

if((y%400)==0)w--;

}

return(w+d)%7;//回傳星期幾(0為星期日,1為星期一以此類推)

}

privatevoidinitGUI()//產生視覺化物件函數(GraphUserInterface,圖形化使用者介面)

{

try

{

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//設定外框視窗主要功能列為標準(縮到最小,放到最大,關閉)

{

this.setTitle("万年行事历");//設定視窗抬頭

jDesktopPane1=newJDesktopPane();//建立一桌面

getContentPane().add(jDesktopPane1,BorderLayout.CENTER);

jDesktopPane1.setPreferredSize(newjava.awt.Dimension(464,267));//設定桌面大小

jDesktopPane1.setBackground(newjava.awt.Color(148,205,176));//設定桌面顏色

{//建立桌面2開始(放日期按鈕的桌面)

jDesktopPane2=newJDesktopPane();

jDesktopPane1.add(jDesktopPane2);

jDesktopPane2.setBounds(0,30,252,196);

jDesktopPane2.setBackground(newjava.awt.Color(148,205,176));

}//建立桌面2結束

{//建立星期日標籤開始

jLabel1=newJLabel();

jDesktopPane1.add(jLabel1);

jLabel1.setText("Sun");

jLabel1.setBounds(14,7,21,21);

jLabel1.setFont(newjava.awt.Font("Arial",0,11));

jLabel1.setForeground(newjava.awt.Color(255,0,0));

}//建立星期日標籤結束

{//建立記事框開始

jTextArea1=newJTextArea(252,35);

jDesktopPane1.add(jTextArea1);

jTextArea1.setText("");//預設內容清空

jTextArea1.setBounds(252,42,175,182);//設定大小

jTextArea1.setFont(newjava.awt.Font("新細明體",0,11));//設定字體樣式大小

jTextArea1.setLineWrap(true);//文字過長自動換行

jTextArea1.setWrapStyleWord(true);//文字過長自動換行

JScrollPanescrollPane=newJScrollPane(jTextArea1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

jDesktopPane1.add(scrollPane);//上面為建立scorollpane(視窗旁邊的移動棒);將記事框建立在scrollpane上

scrollPane.setBounds(252,42,175,182);//設定大小及位置

}//建立記事框結束

{//建立清除按鈕開始

jButton1=newJButton();

jDesktopPane1.add(jButton1);

jButton1.setText("清除");

jButton1.setBounds(252,231,63,21);

jButton1.setFont(newjava.awt.Font("新細明體",0,11));

jButton1.setBorder(BorderFactory.createTitledBorder(""));

jButton1.addActionListener(newActionListener(){//設定清除按鈕監聽函數

publicvoidactionPerformed(ActionEventevt){

jButton1ActionPerformed(evt);

}

});

}//建立清除按鈕結束

{//建立儲存按鈕開始

jButton2=newJButton();

jDesktopPane1.add(jButton2);

jButton2.setText("储存");

jButton2.setBounds(364,231,63,21);

jButton2.setFont(newjava.awt.Font("新細明體",0,11));

jButton2.setBorder(BorderFactory.createTitledBorder(""));

jButton2.addActionListener(newActionListener(){//設定儲存按鈕監聽函數

publicvoidactionPerformed(ActionEventevt){

jButton2ActionPerformed(evt);

}

});

}//建立儲存按鈕結束

{//建立星期一~星期六標籤開始

jLabel2=newJLabel();

jDesktopPane1.add(jLabel2);

jLabel2.setText("MonTueWedThuFriSat");

jLabel2.setBounds(49,7,189,21);

jLabel2.setFont(newjava.awt.Font("Arial",0,11));

}//建立星期一~星期六標籤結束

{//建立查詢年份輸入框開始

jTextField1=newJTextField();

jDesktopPane1.add(jTextField1);

jTextField1.setText("");

jTextField1.setBounds(14,231,63,21);

jTextField1.setFont(newjava.awt.Font("Arial",0,12));

}//建立查詢年份輸入框結束

{//建立狀態標籤開始

jLabel6=newJLabel();

jDesktopPane1.add(jLabel6);

jLabel6.setText("TEST");

jLabel6.setBounds(357,7,70,21);

jLabel6.setFont(newjava.awt.Font("新細明體",0,11));

jLabel6.setForeground(newjava.awt.Color(0,0,255));

}//建立狀態標籤開始

{//建立隱藏日期按鈕暫存標籤開始

jLabel7=newJLabel();

jDesktopPane1.add(jLabel7);

jLabel7.setText("");

jLabel7.setBounds(0,0,0,0);//設定大小為0

}//建立隱藏日期按鈕暫存標籤結束

{//建立目前選擇日期標籤開始

jLabel8=newJLabel();

jDesktopPane1.add(jLabel8);

jLabel8.setText("目前无选择日期");

jLabel8.setBounds(252,28,175,14);

jLabel8.setFont(newjava.awt.Font("新細明體",0,11));

}//建立目前選擇日期標籤結束

{//建立月份下拉選單開始

ComboBoxModeljComboBox1Model=newDefaultComboBoxModel

(newString[]{"1","2","3","4","5","6","7","8","9","10","11","12"});//內容設定1~12

jComboBox1=newJComboBox();

jDesktopPane1.add(jComboBox1);

jComboBox1.setModel(jComboBox1Model);

jComboBox1.setBounds(105,231,40,21);

jComboBox1.setFont(newjava.awt.Font("Arial",0,11));

}//建立月份下拉選單結束

{//建立"月"標籤開始

jLabel3=newJLabel();

jDesktopPane1.add(jLabel3);

jLabel3.setText("月");

jLabel3.setBounds(155,231,14,21);

jLabel3.setFont(newjava.awt.Font("新細明體",0,12));

}//建立"月"標籤結束

{//建立"年"標籤開始

jLabel4=newJLabel();

jDesktopPane1.add(jLabel4);

jLabel4.setText("年");

jLabel4.setFont(newjava.awt.Font("新細明體",0,12));

jLabel4.setBounds(84,231,14,21);

}//建立"年"標籤結束

{//建立查詢按鈕開始

jButton3=newJButton();

jDesktopPane1.add(jButton3);

jButton3.setText("查询");

jButton3.setBounds(180,231,35,21);

jButton3.setBorder(BorderFactory.createTitledBorder(""));

jButton3.setFont(newjava.awt.Font("新細明體",0,11));

jButton3.addActionListener(newActionListener(){//查詢按鈕監聽函數

publicvoidactionPerformed(ActionEventevt){

jButton3ActionPerformed(evt);

}

});

}//建立查詢按鈕結束

{//建立目前年月位置標籤開始

int[]now=newint[3];

now=getdate();//預設為當年當月

Stringyear5,smonth;

year5=String.valueOf(now[0]);

smonth=String.valueOf(now[1]);

if(smonth.length()==1)

s

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 学科竞赛

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1