`
xander
  • 浏览: 15360 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

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();
 }
}

  • 大小: 42 KB
0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics