用java编程实现 2016年12月份的日历怎么写?

用java编程实现 2016年12月份的日历怎么写?,第1张

用Java实现的日历程序如下(图形界面程序)

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Font

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

public class CCI extends JFrame implements ActionListener{

 JButton jb1=new JButton("<<")

 JButton jb2=new JButton("<")

 JButton jb3=new JButton(">")

 JButton jb4=new JButton(">>")

 JPanel jp1=new JPanel()

 JPanel jp2=new JPanel()

 JPanel jp3=new JPanel()

 JPanel jp4=new JPanel()

 JLabel jl1=new JLabel()

 JLabel jl2=new JLabel()

 JLabel[]jl=new JLabel[49]

 String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}

 Calendar c=Calendar.getInstance()

 int year,month,day

 int nowyear,nowmonth,nowday

 CCI(){

  super("简单日历")

  nowyear=c.get(Calendar.YEAR)

  nowmonth=c.get(Calendar.MONTH)+1

  nowday=c.get(Calendar.DAY_OF_MONTH)

  year=nowyear

  month=nowmonth

  day=nowday

  String s=year+"年"+month+"月"

  jl1.setForeground(Color.RED)

 困陵 jl1.setFont(new Font(null,Font.BOLD,20))

  jl1.setText(s)

  jb1.addActionListener(this)

  jb2.addActionListener(this)

  jb3.addActionListener(this)

  jb4.addActionListener(this)

  jp1.add(jb1)jp1.add(jb2)jp1.add(jl1)jp1.add(jb3)jp1.add(jb4)

  jp2.setLayout(null)

  createMonthPanel()

  jp2.add(jp3)

  jl2.setFont(new Font(null,Font.BOLD,20))

  jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日")

  jp4.add(jl2)

  add(jp1,BorderLayout.NORTH)

  add(jp2,BorderLayout.CENTER)

  add(jp4,BorderLayout.SOUTH)

  setSize(500,500)

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

  setLocationRelativeTo(null)

  setVisible(true)

 }

 @Override

 public void actionPerformed(ActionEvent ae) {

  if(ae.getSource()==jb1){

   year=year-1

   String s=year+"年"+month+"月"

   jl1.setText(s)

   jp3.removeAll()

   createMonthPanel()

   jp3.validate()

  }

 汪蔽戚 if(ae.getSource()==jb2){

   if(month==1){

    year=year-1

    month=12

   }else{

 并型   month=month-1

   }

   String s=year+"年"+month+"月"

   jl1.setText(s)

   jp3.removeAll()

   createMonthPanel()

   jp3.validate()

  }

  if(ae.getSource()==jb3){

   if(month==12){

    year=year+1

    month=1

   }else{

    month=month+1

   }

   String s=year+"年"+month+"月"

   jl1.setText(s)

   jp3.removeAll()

   createMonthPanel()

   jp3.validate()

  }

  if(ae.getSource()==jb4){

   year=year+1

   String s=year+"年"+month+"月"

   jl1.setText(s)

   jp3.removeAll()

   createMonthPanel()

   jp3.validate()

  }

 }

 public static void main(String[] args) {

  new CCI()

 }

 public int getMonthDays(int year, int month) { 

  switch (month) {

   case 1: 

   case 3: 

   case 5: 

   case 7:

   case 8: 

   case 10: 

   case 12:

    return 31 

   case 2: 

    if ((year%4==0&&year%100!=0)||year%400==0) { 

     return 29 

    } else { 

     return 28 

    } 

   default: 

    return 30 

  } 

 } 

 public void createMonthPanel(){

  c.set(year, month-1, getMonthDays(year,month))

  int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH)

  if(weekOfMonth==6){

   jp3.setLayout(new GridLayout(7,7))

   jp3.setBounds(50, 20, 420, 350)

  }else{

   jp3.setLayout(new GridLayout(6,7))

   jp3.setBounds(50, 20, 420, 300)

  }

  jp3.setBorder(BorderFactory.createEtchedBorder())

  for(int i=0i<7i++){

   jl[i]=new JLabel(week[i],JLabel.CENTER)

   jl[i].setFont(new Font(null,Font.BOLD,20))

   jl[i].setBorder(BorderFactory.createEtchedBorder())

   jp3.add(jl[i])

  }

  c.set(year, month-1, 1)

  int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1

  int daysOfMonth=getMonthDays(year,month)

  for(int i=6+emptyFirsti>=7i--){

   int intyear=year

   int intmonth=month

   if(intmonth==1){

    intyear=intyear-1

    intmonth=12

   }else{

    intmonth=intmonth-1

   }

   int intdays=getMonthDays(intyear,intmonth)

   jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER)

   jl[i].setFont(new Font(null,Font.BOLD,20))

   jl[i].setForeground(Color.GRAY)

   jl[i].setBorder(BorderFactory.createEtchedBorder())

   jp3.add(jl[i])

  }

  for(int i=7+emptyFirsti<daysOfMonth+7+emptyFirsti++){

   jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER)

   jl[i].setFont(new Font(null,Font.BOLD,20))

   if((i+1)%7==0 || (i+1)%7==1){

    jl[i].setForeground(Color.RED)

   }else if((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)

    jl[i].setForeground(Color.BLUE)

   else

    jl[i].setForeground(Color.BLACK)

   

   jl[i].setBorder(BorderFactory.createEtchedBorder())

   jp3.add(jl[i])

  }

  if(weekOfMonth==6)

   for(int i=48i>=daysOfMonth+emptyFirst+7i--){

    jl[i]=new JLabel((49-i)+"",JLabel.CENTER)

    jl[i].setFont(new Font(null,Font.BOLD,20))

    jl[i].setForeground(Color.GRAY)

    jl[i].setBorder(BorderFactory.createEtchedBorder())

    jp3.add(jl[i])

   }

  else

   for(int i=41i>=daysOfMonth+emptyFirst+7i--){

    jl[i]=new JLabel((42-i)+"",JLabel.CENTER)

    jl[i].setFont(new Font(null,Font.BOLD,20))

    jl[i].setForeground(Color.GRAY)

    jl[i].setBorder(BorderFactory.createEtchedBorder())

    jp3.add(jl[i])

   }

 }

}

运行结果

import java.util.Scanner

public class $ {

    private static 配碧int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in)

     渣卖蔽   System.out.print("输入年份:")

        int year = in.nextInt()

        System.out.print("输入月份:")

        int month = in.nextInt()

        String str = year + "-" + month + ":"

        // 闰年二月份

        if (year % 400 == 0 || (year % 4 == 0 &&如州 year % 100 != 0) && month == 2) {

            str += 29

        } else {

            str += DAYS[month - 1]

        }

        System.out.println(str)

    }

}

int y// year可以随便设定

int m//month可以随便设定

int d=0//day设默认

int i=0

for(i <2000i++){

if(y==4i){

switch [m]{

case '2'

return d=29

break

case '1'

case '3'

case '5'

case '7'

case '8'

case '10'

case '12'

return d=31

default: return d=30

}

}

else{

switch [m]{

case '2'

return d=28

break

case '1'

case '3'

case '5'

case '7'

case '8'

case '10'

case '12'

return d=31

default: return d=30

扩展质料:

switch语句执行时会从上到下根据括号中表达式的值作比较,当某个case语句中的表达式与此值相同时,就执行这个case语句或语句序列,直到遇到break为止。 break语句是必须有的,它用来结束switch语句的执行。

如果所有case语句后面的表达式都不等于switch语句的表达式expr1的掘橡值川0执行default后面的默认语句序列。不过,default部分是可选的。如果没有这一部分,并遇到所有case语句都不匹配,那么,就不作任何处理而进入后续程序段的执行。

可见,一个switch语句可以代替多个if-else语句组成的分支结构,而switch语句从思路上显得更清晰。

使用switch语句时,要注意expr1必须是符合byte,判中旁char,short,int类型的常量表达式,而不能用浮点类型或long类型,//(也不能为一个字符串)。

参培雹考资料:switch-百度百科


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12439940.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存