Java JFrame空間,日曆

 

 

package calendarTest;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * 啓動入口
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/06/30/9:34
 * @Description:
 */
public class CalendarMain {

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面風格
        }catch (Exception e) {
            e.printStackTrace();
        }
        //構建日期frame
        CalendarFrame frame=new CalendarFrame();
        //左,上,寬,高
        frame.setBounds(100,100,360,300);
        //窗體標題
        frame.setTitle("日曆");
        //窗體居中顯示
        frame.setLocationRelativeTo(null);
        //需要顯示則使用true,需要隱藏則顯示false。
        frame.setVisible(true);
        //窗體右上角退出
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
package calendarTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * 監聽事件
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/06/30/9:33
 * @Description:
 */
public class CalendarFrame extends JFrame implements ActionListener {

    JLabel labelDay[]=new JLabel[42];  //天數
    JButton titleName[]=new JButton[7]; // 週數

    String name[]={"日","一","二","三", "四","五","六"};   //一周天數
    JButton nextMonth,previousMonth;

    //啓動程序顯示的日期信息
    Calendar cal = Calendar.getInstance();
    int year=cal.get(Calendar.YEAR);   //當前年
    int month=cal.get(Calendar.MONTH)+ 1;; //當前月
    int day = cal.get(Calendar.DATE); //當前日
    CalendarBean calendar;
    JLabel showMessage=new JLabel("",JLabel.CENTER);  //初始化左上角,當前時間文本展示

    public CalendarFrame() {
        //設置窗體顏色
        setBackground(new Color(110, 123, 128));
        JPanel pCenter=new JPanel();
        pCenter.setBackground(new Color(110, 123, 128));

        //將pCenter的佈局設置爲7行7列的GridLayout 佈局。
        pCenter.setLayout(new GridLayout(7,7));

        //pCenter添加組件titleName[i]
        for(int i=0;i<7;i++) {
            titleName[i]=new JButton(name[i]);
            pCenter.add(titleName[i]);
        }

        //pCenter添加組件labelDay[i]
        for(int i=0;i<42;i++) {
            labelDay[i]=new JLabel("",JLabel.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]);
        }

        nextMonth=new JButton("下月"); //設置主題下月按鈕
        previousMonth=new JButton("上月"); //設置主題上月按鈕


        //註冊監聽器
        nextMonth.addActionListener(this); //下月按鈕點擊觸發事件
        previousMonth.addActionListener(this); //上月按鈕點擊觸發事件


        //創建主題面板,添加綁定,當前時間,上月、下月按鈕
        JPanel pNorth=new JPanel(),
                pSouth=new JPanel();
        pNorth.add(showMessage);
        pNorth.add(previousMonth);
        pNorth.add(nextMonth);

        //展示
        showMessage.setText("當前日期:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" + this.day +"日" );
        ScrollPane scrollPane=new ScrollPane();
        scrollPane.add(pCenter);
        getContentPane().add(scrollPane,BorderLayout.CENTER);// 窗口添加scrollPane在中心區域
        getContentPane().add(pNorth,BorderLayout.NORTH);// 窗口添加pNorth 在北面區域
        getContentPane().add(pSouth,BorderLayout.SOUTH);// 窗口添加pSouth 在南區域。

    }

    /**
     * 監聽事件
     * @param e
     */
    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]);
            }
        }

    }


}
package calendarTest;

import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * 創建日曆實體類
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/06/30/9:32
 * @Description:
 */
public class CalendarBean {

    String day[];   //天數數組
    int year=0,month=0; // 年月
    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 date=Calendar.getInstance();
        date.set(year,month,1);
        int week=date.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;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章