Calendar日历小程序
//有待完善,有點(diǎn)bug
package com.sunshine.framework.calendar.model;
import java.util.Calendar;
/**
?*
?* <p>
?* 描述該類情況 {@link 代表跟誰(shuí)有關(guān)系}
?* </p>
?*
?* @author 王超
?* @since 1.0
?* @date 2016年10月20日 下午8:19:15
?* @see 新建|修改|放棄
?* @see com.sunshine.framework.calendar.model.CalendarBean
?*/
public class CalendarBean {
?? ?String day[];
?? ?int year = 2005, month = 0;
?? ?public String[] getCalendar() {
?? ??? ?String a[] = new String[42];
?? ??? ?// 獲取日歷的實(shí)例對(duì)象
?? ??? ?Calendar date = Calendar.getInstance();
?? ??? ?// 設(shè)置日歷日期
?? ??? ?date.set(this.year, this.month - 1, 1);
?? ??? ?// 獲取周數(shù)
?? ??? ?int week = date.get(Calendar.DAY_OF_WEEK);
?? ??? ?int day = 0;
?? ??? ?// 判斷大月份
?? ??? ?if (this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8
?? ??? ??? ??? ?|| this.month == 10 || this.month == 12) {
?? ??? ??? ?day = 31;
?? ??? ?}
?? ??? ?// 判斷小月
?? ??? ?if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) {
?? ??? ??? ?day = 30;
?? ??? ?}
?? ??? ?// 單獨(dú)判斷2月
?? ??? ?if (this.month == 2) {
?? ??? ??? ?// 判斷是閏年 能被4或400整除 但不能被100整除
?? ??? ??? ?if ((this.year % 4 == 0) || (this.year % 100 != 0) || (this.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;
?? ?}
?? ?public int getMonth() {
?? ??? ?return this.month;
?? ?}
?? ?public int getYear() {
?? ??? ?return this.year;
?? ?}
?? ?public void setMonth(int month) {
?? ??? ?this.month = month;
?? ?}
?? ?public void setYear(int year) {
?? ??? ?this.year = year;
?? ?}
}
?
package com.sunshine.framework.calendar;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.sunshine.framework.calendar.model.CalendarBean;
/**
?*
?* <p>
?* 描述該類情況 {@link 代表跟誰(shuí)有關(guān)系}
?* </p>
?*
?* @author 王超
?* @since 1.0
?* @date 2016年10月20日 下午8:41:58
?* @see 新建|修改|放棄
?* @see com.sunshine.framework.calendar.CalendarFrame
?*/
public class CalendarFrame extends JFrame implements ActionListener {
?? ?/***/
?? ?private static final long serialVersionUID = 1L;
?? ?JButton button = new JButton();
?? ?CalendarBean calendar;
?? ?JLabel labelDay[] = new JLabel[42];
?? ?JLabel lbl1 = new JLabel("請(qǐng)輸入年份:");
?? ?JLabel lbl2 = new JLabel("");
?? ?String name[] = { "日", "一", "二", "三", "四", "五", "六" };
?? ?JButton nextMonth, previousMonth;
?? ?JLabel showMessage = new JLabel("", JLabel.CENTER);
?? ?JTextField text = new JTextField(10);
?? ?JButton titleName[] = new JButton[7];
?? ?int year = 1996, month = 1;
?? ?public CalendarFrame() {
?? ??? ?JPanel pCenter = new JPanel();
?? ??? ?// 將pCenter的布局設(shè)置為7行7列的GridLayout布局
?? ??? ?pCenter.setLayout(new GridLayout(7, 7));
?? ??? ?// pCenter添加組件titleName[i]
?? ??? ?for (int i = 0; i < 7; i++) {
?? ??? ??? ?// 把星期值存入到titleName數(shù)組里
?? ??? ??? ?this.titleName[i] = new JButton(this.name[i]);
?? ??? ?}
?? ??? ?// pCenter添加組件labelDay[i]
?? ??? ?for (int i = 0; i < 42; i++) {
?? ??? ??? ?this.labelDay[i] = new JLabel("", JLabel.CENTER);
?? ??? ??? ?pCenter.add(this.labelDay[i]);
?? ??? ?}
?? ??? ?this.text.addActionListener(this);
?? ??? ?this.calendar = new CalendarBean();
?? ??? ?this.calendar.setYear(this.year);
?? ??? ?this.calendar.setMonth(this.month);
?? ??? ?String day[] = this.calendar.getCalendar();
?? ??? ?for (int i = 0; i < 42; i++) {
?? ??? ??? ?this.labelDay[i].setText(day[i]);
?? ??? ?}
?? ??? ?this.nextMonth = new JButton("下月");
?? ??? ?this.previousMonth = new JButton("上月");
?? ??? ?this.button = new JButton("確定");
?? ??? ?// 注冊(cè)監(jiān)聽(tīng)器
?? ??? ?this.nextMonth.addActionListener(this);
?? ??? ?this.previousMonth.addActionListener(this);
?? ??? ?this.button.addActionListener(this);
?? ??? ?JPanel pNorth = new JPanel(), pSouth = new JPanel();
?? ??? ?pNorth.add(this.showMessage);
?? ??? ?pNorth.add(this.lbl2);
?? ??? ?pNorth.add(this.previousMonth);
?? ??? ?pNorth.add(this.nextMonth);
?? ??? ?pNorth.add(this.lbl1);
?? ??? ?pNorth.add(this.text);
?? ??? ?pNorth.add(this.button);
?? ??? ?this.showMessage.setText("日歷:" + this.calendar.getYear() + "年" + this.calendar.getMonth() + "月");
?? ??? ?ScrollPane scrollPane = new ScrollPane();
?? ??? ?scrollPane.add(pCenter);
?? ??? ?// 窗口添加scrollPane在中心區(qū)域
?? ??? ?add(scrollPane, BorderLayout.CENTER);
?? ??? ?// 窗口添加pNorth 在北面區(qū)域
?? ??? ?add(pNorth, BorderLayout.NORTH);
?? ??? ?// 窗口添加pSouth 在南區(qū)域。
?? ??? ?add(pSouth, BorderLayout.SOUTH);
?? ?}
?? ?/*
?? ? * (方法重寫(xiě))
?? ? *
?? ? * @see
?? ? * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
?? ? */
?? ?@Override
?? ?public void actionPerformed(ActionEvent e) {
?? ??? ?if (e.getSource() == this.nextMonth) {
?? ??? ??? ?this.month = this.month + 1;
?? ??? ??? ?if (this.month > 12) {
?? ??? ??? ??? ?this.month = 1;
?? ??? ??? ?}
?? ??? ??? ?this.calendar.setMonth(this.month);
?? ??? ??? ?String day[] = this.calendar.getCalendar();
?? ??? ??? ?for (int i = 0; i < 42; i++) {
?? ??? ??? ??? ?this.labelDay[i].setText(day[i]);
?? ??? ??? ?}
?? ??? ?} else if (e.getSource() == this.previousMonth) {
?? ??? ??? ?this.month = this.month - 1;
?? ??? ??? ?if (this.month < 1) {
?? ??? ??? ??? ?this.month = 12;
?? ??? ??? ?}
?? ??? ??? ?this.calendar.setMonth(this.month);
?? ??? ??? ?String day[] = this.calendar.getCalendar();
?? ??? ??? ?for (int i = 0; i < 42; i++) {
?? ??? ??? ??? ?this.labelDay[i].setText(day[i]);
?? ??? ??? ?}
?? ??? ?} else {
?? ??? ??? ?this.month = this.month + 1;
?? ??? ??? ?if (this.month > 12) {
?? ??? ??? ??? ?this.month = 1;
?? ??? ??? ?}
?? ??? ??? ?this.calendar.setYear(Integer.parseInt(this.text.getText()));
?? ??? ??? ?String day[] = this.calendar.getCalendar();
?? ??? ??? ?for (int i = 0; i < 42; i++) {
?? ??? ??? ??? ?this.labelDay[i].setText(day[i]);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?this.showMessage.setText("日歷:" + this.calendar.getYear() + "年" + this.calendar.getMonth() + "月");
?? ?}
}
?
package com.sunshine.framework.calendar;
import javax.swing.JFrame;
import javax.swing.UIManager;
/**
?*
?* <p>
?* 描述該類情況 {@link 代表跟誰(shuí)有關(guān)系}
?* </p>
?*
?* @author 王超
?* @since 1.0
?* @date 2016年10月20日 下午9:34:15
?* @see 新建|修改|放棄
?* @see com.sunshine.framework.calendar.CalendarMain
?*/
public class CalendarMain {
?? ?public static void main(String[] args) {
?? ??? ?try {
?? ??? ??? ?UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?CalendarFrame frame = new CalendarFrame();
?? ??? ?frame.setBounds(100, 100, 360, 300);
?? ??? ?frame.setTitle("日歷小程序");
?? ??? ?// 窗體居中顯示
?? ??? ?frame.setLocationRelativeTo(null);
?? ??? ?frame.setVisible(true);
?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
?? ?}
}
轉(zhuǎn)載于:https://www.cnblogs.com/qingtianBKY/p/5988783.html
總結(jié)
以上是生活随笔為你收集整理的Calendar日历小程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用Kibana和logstash快速搭建
- 下一篇: h5移动端知识要点