用java写的一个日历程序,因为答辩急着用,有没有大佬帮忙注释一下每个模块的作用!!!
package Calendar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.*;

//图形用户界面设计
public class Calendarshowing {
 static Calendar  c   =  Calendar.getInstance();
 static int year = c.get(Calendar.YEAR), month = c.get(Calendar.MONTH)+1,monthday = 0;
 static String[] weekdaystr = { "日", "一", "二", "三", "四", "五", "六" };//设置星期日.一.二.三.四.五.六
 JFrame frame = new JFrame("日历表");
// JPanel panel = new JPanel();
 static JLabel[]dayLabel = new JLabel[49];//是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历
 static JPanel showPanel;
 //放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth(下一月),可以显示当前月的下一个月的日历;单击previousMonth(上一月)按钮,可以显示当前月的上一个月的日历
 JButton Prebutton = new JButton("上一月");//查看上一月的特定按钮
 JButton Nextbutton = new JButton("下一月");//查看下一月的特定按钮
 JButton Cbutton = new JButton("查询( 请输入正确的年和月 )");//查询的特定按钮
 JLabel showLabel =  new JLabel();
 JLabel YearLabel=new JLabel("年");//查询中输入年的特定按钮
 JLabel MonthLabel=new JLabel("月");//查询中输入月的特定按钮
 JTextArea YearText=new JTextArea("2017");//查询中输入年的初始值
 JTextArea MonthText=new JTextArea("4");//查询中输入月的初始值
 static Border Border01 = BorderFactory.createLineBorder(Color.BLUE, 1);
 public Calendarshowing() {
  //frame.setResizable(false);
  showPanel = new JPanel();
  frame.setBounds(400, 200, 500, 400);//窗体所在位置、大小
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  Container content = frame.getContentPane();
  final JPanel buttonPanel = new JPanel();
  buttonPanel.setLayout(new FlowLayout());

  Cbutton.addActionListener(new PressButton());
  Prebutton.addActionListener(new PressButton());
  Nextbutton.addActionListener(new PressButton());
  buttonPanel.add(Prebutton);
  buttonPanel.add(Nextbutton);
  buttonPanel.add(YearText);
  buttonPanel.add(YearLabel);
  buttonPanel.add(MonthText);
  buttonPanel.add(MonthLabel);
  buttonPanel.add(Cbutton);
  content.add(buttonPanel, BorderLayout.NORTH);

  final GridLayout gridLayout = new GridLayout(0, 7);
  gridLayout.setHgap(10);
  gridLayout.setVgap(10);
  showPanel.setLayout(gridLayout);
  showLabel.setHorizontalAlignment(JLabel.CENTER);
  showLabel.setText("<html><Font size = '6',color = 'RED'>"+"日历: "+"<html><Font size='4',color='Green'>"+year+"年"+month+"月"+"</Font></html>");
  showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
  content.add(showLabel,BorderLayout.SOUTH);
  content.add(showPanel, BorderLayout.CENTER);
  frame.setVisible(true);
 }

 public class PressButton implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
   // TODO Auto-generated method stub
   JButton button = (JButton)e.getSource();
   if (button == Prebutton)
   {
    month--;
    if (month <= 0){
     year--;
     month+=12;
    }
   }
   else{
    if(button == Nextbutton){
    month++;
    if (month > 12){
     year++;
     month-=12;
    }
    }
    else{
       // System.out.println(Double.valueOf(CText.getText()));
       // System.out.println(c.get(Calendar.YEAR));
         while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText())<year*12+month){
              month--;
              if (month <= 0){
              year--;
              month+=12;
            }
         }
         while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText())>year*12+month){
              month++;
              if (month >12){
              year++;
              month-=12;
            }
         }
        }
   }
   showPanel.removeAll();
   showPanel.updateUI();
   new showing();
   showLabel.setHorizontalAlignment(JLabel.CENTER);
   showLabel.setText("<html><Font size = '6',color = 'RED'>"+"日历: "+"<html><Font size='4',color='Green'>"+year+"年"+month+"月"+"</Font></html>");
   showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
  }
 }

 static class showing{

  public boolean isLeapYear(int year1) // 判断该年是否为闰年
  {
   return ((year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0));
  }

  public  int firstDay() // 用来计算并判断该年第一天是星期几
  {
   long n = year * 365;
   for (int i = 1; i < year; i++){
    if (isLeapYear(i)){
     n++;
    }
   }
   return (int)n%7;
  }

  public void printWeek() // 设置图形界面的表头
  {
   for (int i = 0; i < 7; i++) {
    dayLabel[i] = new JLabel(weekdaystr[i],JLabel.CENTER);
    dayLabel[i].setBackground(Color.BLACK);
    dayLabel[i].setForeground(Color.RED);
    showPanel.add(dayLabel[i]);
   }
  }

  public int getMonthDay(int m) // 获取每个月的天数
  {
   switch (m) {
  //月份为1、3、5、7、8、10、12,天数为31;
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
    return 31;
   //月份为4、6、9、11,天数为30;
   case 4:
   case 6:
   case 9:
   case 11:
    return 30;
   //闰年2月份,天数为29,非闰年,天数为28
   case 2:
    if (isLeapYear(year))
     return 29;
    else
     return 28;
   default:
    return 0;
   }
  }

  public  int getDays(){
   monthday = 0;
   for (int i = 1;i<month;i++)
    monthday+=getMonthDay(i);
   return (firstDay()+(monthday%7))%7;
  }

  public showing(){
   int n =1 ;
   printWeek();
   int k = getDays();
   int   Year   =   c.get(Calendar.YEAR); 
   int   Month   =   c.get(Calendar.MONTH);
   Month += 1;
   int   Date   =   c.get(Calendar.DATE);
   for (int j = 7;n<=getMonthDay(month);j++){
    if (j < k+7){
    dayLabel[j] = new JLabel();
    showPanel.add(dayLabel[j]);
    }
    else{
    dayLabel[j] = new JLabel(""+n,JLabel.CENTER);
    dayLabel[j].setToolTipText("星期"+weekdaystr[(j)%7]);
    if (Year == year&&Month==month&&Date == n){
     dayLabel[j].setText("<html><Font size = '6',color = 'BLUE'>"+n+"</Font></html>");
     dayLabel[(Date-1)%7].setForeground(Color.BLUE);
    }
    showPanel.add(dayLabel[j]);
    n++;
    }
   }
   showPanel.setBorder(Border01);
  }
 }

 public static void main(String[] args) {
  Calendarshowing c = new Calendarshowing();
  showing s = new Calendarshowing.showing();
 }
}



1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问