發新話題

[JAVA] 最新的月曆

[JAVA] 最新的月曆

引用:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class b7 extends JFrame implements ActionListener//畫圖物件是JFrame的擴充
{
    Container c;
    Color bgcolor=Color.white;
    Color textcolor=Color.red;
    JLabel lab1=new JLabel();
    JButton rot0,rot1;
    int year,month;
    JTable dsp;
    int[] temp_month={31,28,31,30,31,30,31,31,30,31,30,31,};                //月份的天數
    String[] weekname = {"週日","週一","週二","週三","週四","週五","週六"};
    public void gen_cal()
    {
        int temp_year,days,i,last;
        int xpt,ypt;
        int row=6;
        //取得輸入
        String syear = JOptionPane.showInputDialog("請輸入年");
        String smonth = JOptionPane.showInputDialog("請輸入月");
        year=Integer.parseInt(syear);
        month=Integer.parseInt(smonth);
        lab1.setText(year+" 年 "+month+" 月份日曆");
        //---------判斷是否為閏年及計算總天數----------------------------
        temp_year=year-1;
        days=365*temp_year+temp_year/4-temp_year/100+temp_year/400;
        if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 ==0)))// 判斷該年是否為閏年,若是閏年則將該年2月的日期改為29天
                temp_month[1]=29;
        else temp_month[1]=28;
        for (i=0;i<(month-1);i++)                //算總天數
                days=days+temp_month;
        last=((days % 7)+1) % 7 ; //算第一行空白的格數
        if ((temp_month[month-1]+last-7)>28) row=6;         //算算要幾列
        else row=5;
        Object[][] data =new String[row][7];
        dsp=new JTable(data,weekname); //設定JTable
        //for (i=0;i<7;i=i+1) //設定標題列
        //        dsp.setValueAt(weekname,0,i);
        xpt=last;
        ypt=0;
        for (i=1;i<=temp_month[month-1];i=i+1)//排每一天
        {
                dsp.setValueAt(""+i,ypt,xpt);
                xpt=xpt+1;
                if (xpt%7 ==0 )
                {
                        xpt=0;
                        ypt=ypt+1;
                }
        }
                        
    }

    public b7()   //建構元
    {
        super("月曆");
        gen_cal();
        c=getContentPane();
        rot0=new JButton("離開");
        setSize(600,300);
        c.setLayout(new FlowLayout(FlowLayout.CENTER));
        c.add(lab1);
        c.add(dsp.getTableHeader());//排進標題
        c.add(dsp);
        c.add(rot0);
        //安裝listener
        rot0.addActionListener(this);
        show();
    }
    public void paint(Graphics g)  //真正的畫圖設定
    {
        super.paint(g);
    }

     public void actionPerformed(ActionEvent e)
     {
         if (e.getSource()==rot0)//結束
          {
                 System.exit(0);
          }
        
     } //actionPerformed

    public static void main(String args[]) //程式起點
    {
        b7 app=new b7(); //啟動物件
        app.addWindowListener(new WindowAdapter(){ //匿名內部類別
        public void windowClosing(WindowEvent e)
                {
                        System.exit(0);
                }
        }); //處理視窗關閉要求
    }
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。