⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testjcalendar.java

📁 java简单日历(万年历)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        jPanelButton.add(YearUp); 
        jPanelButton.add(YearDown); 

        jPanelDay.add(weekPanel,BorderLayout.NORTH); 
        jPanelDay.add(Days, BorderLayout.CENTER); 

        jPanelMonth.add(Month, BorderLayout.CENTER); 
        jPanelMonth.add(MonthDown, BorderLayout.WEST); 
        jPanelMonth.add(MonthUp, BorderLayout.EAST); 

        showMonth(); 
        showYear(); 
        showDate(); 
        showDays(); 
    } 

    //自定义重画年选择面板 
    void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt)
    { 
        YearUp.setLocation(0,0); 
        YearDown.setLocation(0,YearUp.getHeight()); 
        jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2); 
        jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2)); 
        jPanelButton.updateUI(); 
    } 

    //测试用 
    public static void main(String[] args)
    { 
        JFrame f=new JFrame(); 
        f.setContentPane(new JCalendar()); 
        f.pack(); 
        //f.setResizable(false); 
        f.show(); 
    } 

    //增加年份 
    void YearUp_actionPerformed(ActionEvent e)
    { 
        year++; 
        showYear(); 
        showDate(); 
        showDays(); 
    } 

    //减少年份 
    void YearDown_actionPerformed(ActionEvent e)
    { 
        year--; 
        showYear(); 
        showDate(); 
        showDays(); 
    } 

    //减少月份 
    void MonthDown_actionPerformed(ActionEvent e) 
    { 
        month--; 
        if(month<0) 
        { 
            month = 11; 
            year--; 
            showYear(); 
        } 
        showMonth(); 
        showDate(); 
        showDays(); 
    } 

    //增加月份 
    void MonthUp_actionPerformed(ActionEvent e) 
    { 
        month++; 
        if(month==12) 
        { 
            month=0; 
            year++; 
            showYear(); 
        } 
        showMonth(); 
        showDate(); 
        showDays(); 
    } 

    //初始化年月日 
    void iniCalender()
    { 
        year=cal.get(Calendar.YEAR); 
        month=cal.get(Calendar.MONTH); 
        day=cal.get(Calendar.DAY_OF_MONTH); 
    } 

    //刷新月份 
    void showMonth()
    { 
        Month.setText(Integer.toString(month+1)+"月"); 
    } 

    //刷新年份 
    void showYear()
    { 
        Year.setText(Integer.toString(year)+"年"); 
    } 

    //刷新日期 
    void showDate()
    { 
        Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day)); 
    } 

    //重画天数选择面板 
    void showDays() 
    { 
        cal.set(year,month,1); 
        int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 
        int n=mm[month]; 
        if(cal.isLeapYear(year)&&month==1) n++; //当闰年且为二月时,天数加一,即为29天
        int i=0; 
        for(;i<firstDayOfWeek-1;i++)
        { 
            days[i].setEnabled(false); 
            days[i].setSelected(false); 
            days[i].setText(""); 
        } 
        int d=1; 
        for(;d<=n;d++)
        { 
            days[i].setText(Integer.toString(d)); 
            days[i].setEnabled(true); 
            if(d==day)                     
                days[i].setSelected(true); //当前日期显示为选中状态
            else 
                days[i].setSelected(false); //非当前日期显示为未选中状态
            i++; 
        }
        // 
        for(;i<42;i++)
        { 
            days[i].setEnabled(false); 
            days[i].setSelected(false); 
            days[i].setText(""); 
        } 
    } 

    //单击年份面板选择整个年份字符串 
    void SelectionYear()
    { 
        Year.setSelectionStart(0); 
        Year.setSelectionEnd(Year.getText().length()); 
    } 

    //单击月份面板选择整个月份字符串 
    void SelectionMonth()
    { 
        Month.setSelectionStart(0); 
        Month.setSelectionEnd(Month.getText().length()); 
    } 

    //月份面板响应鼠标单击事件 
    void Month_mouseClicked(MouseEvent e) 
    { 
        //SelectionMonth(); 
        inputMonth(); 
    } 

    //检验输入的月份 
    void inputMonth()
    { 
        String s; 
        if(Month.getText().endsWith("月")) 
        { 
            s=Month.getText().substring(0,Month.getText().length()-1); 
        } 
        else s=Month.getText(); 
        month=Integer.parseInt(s)-1; 
        this.showMe(); 
    } 

    //月份面板键盘敲击事件响应 
    void Month_keyPressed(KeyEvent e) 
    { 
        if(e.getKeyChar()==10) 
        inputMonth(); 
    } 

    //年份面板响应鼠标单击事件 
    void Year_mouseClicked(MouseEvent e) 
    { 
        //SelectionYear(); 
        inputYear(); 
    } 

    //年份键盘敲击事件响应 
    void Year_keyPressed(KeyEvent e) 
    { 
        //System.out.print(new Integer(e.getKeyChar()).byteValue()); 
        if(e.getKeyChar()==10) 
        inputYear(); 
    } 

    //检验输入的年份字符串 
    void inputYear() 
    { 
        String s; 
        if(Year.getText().endsWith("年")) 
        { 
            s=Year.getText().substring(0,Year.getText().length()-1); 
        } 
        else 
            s=Year.getText(); 
        year=Integer.parseInt(s); 
        this.showMe(); 
    } 

    //以字符串形式返回日期,yyyy-mm-dd 
    public String getDate(){return Out.getText();} 

    //以字符串形式输入日期,yyyy-mm-dd 
    public void setDate(String date)
    { 
        if(date!=null)
        { 
            StringTokenizer f = new StringTokenizer(date, "-"); 
            if(f.hasMoreTokens()) 
                year = Integer.parseInt(f.nextToken()); 
            if(f.hasMoreTokens()) 
                month = Integer.parseInt(f.nextToken()); 
            if(f.hasMoreTokens()) 
                day = Integer.parseInt(f.nextToken()); 
            cal.set(year,month,day); 
        } 
        this.showMe(); 
    } 

    //以日期对象形式输入日期 
    public void setTime(Date date)
    { 
        cal.setTime(date); 
        this.iniCalender(); 
        this.showMe(); 
    } 

    //返回日期对象 
    public Date getTime(){return cal.getTime();} 

    //返回当前的日 
    public int getDay(){return day;} 

    //设置当前的日 
    public void setDay(int day) 
    { 
        this.day = day; 
        cal.set(this.year,this.month,this.day); 
        this.showMe(); 
    } 

    //设置当前的年 
    public void setYear(int year) 
    { 
        this.year = year; 
        cal.set(this.year,this.month,this.day); 
        this.showMe(); 
    } 

    //返回当前的年 
    public int getYear() {return year;} 

    //返回当前的月 
    public int getMonth() {return month;} 

    //设置当前的月 
    public void setMonth(int month) 
    { 
        this.month = month; 
        cal.set(this.year,this.month,this.day); 
        this.showMe(); 
    } 

    //刷新 
    public void showMe()
    { 
        this.showDays(); 
        this.showMonth(); 
        this.showYear(); 
        this.showDate(); 
    } 

} 

public class TestJCalendar 
{ 
    public static void main(String[] args) 
    { 
        JFrame f=new JFrame("JCalendar"); 
        f.setContentPane(new JCalendar()); 
        f.pack(); 
        //f.setResizable(false); 
        f.show(); 
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -