java万年历

最近在学习java窗体开发,自己做了一个万年历,还请大家多多指点

 package awt;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class MyCalendar extends JFrame implements ActionListener
{
 
 //private JTextField text;
 TextField text=new TextField("2012");
 int year;
 int month=0,date=1;
 int week=0;
 int max=1;
 JTextArea trea =new JTextArea();//创建一个文本域对象
 
 public MyCalendar()
 {
  super("万年历");
  this.setBounds(300, 300, 400, 400);
  this.setResizable(true);
  this.setBackground(Color.BLUE);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  this.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
  this.add(text);  
  JScrollPane scr = new JScrollPane(trea);//创建一个滚动条对象
  //JScrollPane scr=new JScrollPane(trea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  scr.setBounds(120, 10, 300, 300);
     Font font=new Font("宋体",Font.PLAIN,15);
  this.add(scr);   
  text.setEditable(true); 
  text.addActionListener(this);
  this.actionPerformed(null);
  this.setVisible(true);
     trea.setFont(font);
     trea.setBackground(Color.cyan);
     trea.setForeground(Color.red);
  trea.setColumns(33);
  trea.setRows(20);
  
 }
 public void actionPerformed(ActionEvent e)
 {
  try
  {
   int years=Integer.parseInt(text.getText());
   year=years;
   Calendar c=Calendar.getInstance();
   c.clear();
   c.set(Calendar.YEAR,year);
   c.set(Calendar.MONTH,month);
   c.set(Calendar.DATE,date);
   int weeks=c.get(Calendar.DAY_OF_WEEK);
   week=weeks;
   //trea.append(year+"年日历"+"\n");//在JTextArea中显示日历
   trea.setText(year+"年日历"+"\n");
   //do
   while(month<=11)
   {
    switch(month+1)
    {
    case 1 :
    case 3 :
    case 5 :
    case 7 :
    case 8 :
    case 10 :
    case 12 :
    max=31;
    break;
    
    case 2:
     if(year%400==0||year%100!=0&&year%4==0)
      max=29;
     else
      max=28;
     break;
     
    case 4 :
    case 6 :
    case 9 :
    case 11:
     max=30;
     break;
    }
    trea.append("\n");
    trea.append(month+1+"月份"+"\n");
    trea.append("   日  一  二  三  四  五  六"+"\n");
    for(int i=1;i<week;i++)
    {
     trea.append(String.format("%4c",' '));
    }
    for(int day=1;day<=max;day++)
    {
     trea.append(String.format("%4d",day));
     week++;
                 week%=7;
                 if (week==1)
              trea.append("\n");
    }
    trea.append("\n");
    month++;
   }
   //while(month<=11);
   month=0;
  }
  catch(NumberFormatException nfe)
  {
   JOptionPane.showMessageDialog(this,"\""+text.getText()+"\"必须是整数,请重新输入");
  }
  finally{}
 }
 public static void main(String args[])
 {
  new MyCalendar();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值