JavaSwing万年历+时间的程序(详细过程)

来源:互联网 发布:不负如来不负卿网络剧 编辑:程序博客网 时间:2024/05/21 19:41

import java.awt.Color;   
import java.awt.Font;   
import java.awt.GridLayout;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import java.awt.event.ItemEvent;   
import java.awt.event.ItemListener;   
import java.util.Date;   
import java.util.GregorianCalendar;   
import javax.swing.JButton;   
import javax.swing.JComboBox;   
import javax.swing.JFrame;   
import javax.swing.JLabel;
import java.awt.Dimension;  
public class Calender2 extends JFrame implements ActionListener, ItemListener
{   

    private static final long serialVersionUID = 1L;   
 
    public static void main(String args[])
 { 
  
    try {   
            Calender2 frame = new Calender2();   
            frame.setVisible(true);   
        }
         
    catch (Exception e)
     {   
        e.printStackTrace();   
     }   
}
   
private Date date = new Date();   
 
private GregorianCalendar gregorianCalendar = new GregorianCalendar();   
 
private String[] stringWeek = new String[] { "SUN", "MON", "TUE", "WED","THU", "FRI", "SAT" };    
    
private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };   
   
private String[] stringMonth = new String[] { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };   
                                               
private String[] strSysTime = new String[6];   
  
private String[] strSysNowTime = new String[6];   
   
private JButton[] buttonDay = new JButton[42];   
private JButton[] buttonWeek = new JButton[7];   
private JLabel labelMonth = new JLabel();   
private JButton buttonToday = new JButton();   
private JButton buttonLastMonth = new JButton();   
private JButton buttonNextMonth = new JButton();   
private JComboBox comboYear = new JComboBox();   
private JComboBox comboMonth = new JComboBox(); 

 
public Calender2()
{
 
    super("SONG");   
    getContentPane().setLayout(new GridLayout(9,7, 3, 5));   
    setBounds(300, 200,620, 430);   
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    comboYear.setForeground(new Color(200, 50, 255));   
    comboYear.setFont(new Font("", Font.PLAIN, 18));   
    for (int y = 1900; y < 3101; y++)
     {   
        comboYear.addItem("  " + new Integer(y).toString());   
     }   
getContentPane().add(comboYear);   
comboYear.addItemListener(this);

final JLabel labelYear = new JLabel();   
labelYear.setForeground(Color.BLUE);   
labelYear.setFont(new Font(" ", Font.BOLD, 18));   
getContentPane().add(labelYear);   
labelYear.setText("    YEAR");   
 
comboMonth.setForeground(new Color(150, 20, 255));   
comboMonth.setFont(new Font(" ", Font.BOLD, 18));   
for (int m =1; m <=12; m++)
{
   
    comboMonth.addItem(" " + new Integer(m).toString());
  
}   
getContentPane().add(comboMonth);   
comboMonth.addItemListener(this);   
 
getContentPane().add(labelMonth);   
labelMonth.setForeground(Color.BLUE);   
labelMonth.setFont(new Font(" ", Font.BOLD, 18));   
labelMonth.setText("  MONTH");   
 
getContentPane().add(buttonLastMonth);   
buttonLastMonth.setForeground(Color.BLUE);   
buttonLastMonth.setFont(new Font(" ", Font.BOLD, 16));   
buttonLastMonth.setText("上月");   
buttonLastMonth.addActionListener(this);   
 
getContentPane().add(buttonToday);   
buttonToday.setForeground(Color.BLUE);   
buttonToday.setFont(new Font(" ", Font.BOLD, 16));   
buttonToday.setText("今日");   
buttonToday.addActionListener(this);   
 
getContentPane().add(buttonNextMonth);   
buttonNextMonth.setForeground(Color.BLUE);   
buttonNextMonth.setFont(new Font(" ", Font.BOLD, 16));   
buttonNextMonth.setText("下月");   
buttonNextMonth.addActionListener(this);   
 
for (int i = 0; i < 7; i++)
 {   
    buttonWeek[i] = new JButton();
    buttonWeek[i].setBackground(Color.YELLOW);
  
    if (i == 0 || i == 6)
    {   
        buttonWeek[i].setForeground(Color.RED); 
       
    }
     else {   
            buttonWeek[i].setForeground(Color.BLUE);   
          }   
    buttonWeek[i].setFont(new Font(" ", Font.BOLD, 16));   
    buttonWeek[i].setText(stringWeekCn[i]);   
    getContentPane().add(buttonWeek[i]);   
}   
 
    for (int i = 0; i < 42; i++)
{   
    buttonDay[i] = new JButton();   
    buttonDay[i].setText(" ");
    buttonDay[i].setFont(new Font(" ", Font.BOLD, 28));    
    getContentPane().add(buttonDay[i]);   
}   
    this.setResizable(false);   
    getSysNowTimeInfo();   
    setNowDate();  
    setNowDate();
//---------------------------------------------------------------------------------------
/* CLASS  TIME  */  
final JLabel time = new JLabel();

time.add(new TIME(this));

getContentPane().add(time);

//----------------------------------------------------------------------------------------
  
}   
 
 
 
    public void setSysDate(int year, int month)
 {   
 
    gregorianCalendar.set(year, month, 1);   
 }   
 
    public void actionPerformed(ActionEvent ae)
{   
    if (ae.getSource() == buttonToday)
     {   
        setNowDate();   
        setNowDate();   
     }
      else if (ae.getSource() == buttonLastMonth)
      {   
           setDate(-1);   
      }
      else { setDate(1);}
                
   
}   
 
    public void itemStateChanged(ItemEvent arg0)
{   
    setDate(0);   
}   
 
    public void getSysNowTimeInfo()
{   
   
    date = gregorianCalendar.getTime();   
    strSysNowTime = (date + " ").split(" ");   
}   
 
    public void getSysTimeInfo()
{   
   
    date = gregorianCalendar.getTime();   
    strSysTime = (date + " ").split(" ");   
}   
 
    public int getNowMonth()
{   
    int month = 0;   
    for (int i = 0; i < 12; i++)
 {   
    if (strSysNowTime[1].equalsIgnoreCase(stringMonth[i]))
    {   
        month = i;    break;    
      
    }   
}
  
return month;
   
}   
 
    public int weekStrat(String strWeek)
{   
 
    int strat = 0;   
    for (int i = 0; i < 7; i++)
    {   
        if (strWeek.equalsIgnoreCase(stringWeek[i]))
     {   
        strat = i;break;   
   
     }   
    } 
     
return strat;
  
}   
 
    public void setNowDate()
{   
  
    setSysTime(getNowYear(), getNowMonth());   
    getSysTimeInfo();   
    setDayNull();   
    getDay(getMonthDays(getNowYear(), getNowMonth() - 1), getMonthDays(getNowYear(), getNowMonth()), weekStrat(strSysTime[0]),getNowDay());    
    comboYear.setSelectedIndex(getNowYear() - 1900);   
    comboMonth.setSelectedIndex(getNowMonth());   
}   
 
    public void setDate(int move)
{   
   
    setSysTime(getYear(), getMonth() + move);   
    getSysTimeInfo();   
    setDayNull();   
    getDay(getMonthDays(getYear(), getMonth() + move - 1), getMonthDays(getYear(), getMonth() + move), weekStrat(strSysTime[0]), -1);           
    if (move != 0)
    {   
        if (getMonth() == 0 && move < 0)
        {   
            move = 11;   
            comboYear.setSelectedIndex(getYear() - 1901);   
        }
         else if (getMonth() == 11 && move > 0)
        {   
            move = -11;   
            comboYear.setSelectedIndex(getYear() - 1899);   
        }
        else
        {   
            comboYear.setSelectedIndex(getYear() - 1900);   
        }
          
        comboMonth.setSelectedIndex(getMonth() + move);   
    }   
}   
 
 
 
    public void setSysTime(int year, int month)
{   
    gregorianCalendar.set(year, month, 1);   
}   
 
    public int getNowYear()
{   
    return Integer.parseInt(strSysNowTime[5]);   
}   
 
    public int getNowDay()
{   
    return Integer.parseInt(strSysNowTime[2]);   
}   
 
    public int getYear()
{   
    return comboYear.getSelectedIndex() + 1900;   
}   
 
    public int getMonth()
{   
    return comboMonth.getSelectedIndex();   
}   
 
    public void setDayNull()
{   
    for (int d = 0; d < 42; d++)
    {   
        buttonDay[d].setText(" ");   
    }   
}   
 
 
    public void getDay(int lastMonDays, int monthDays, int startWeek, int day)
{   
  
    for (int d = 0; d < startWeek + 1; d++)
     {   
        buttonDay[d].setForeground(Color.GRAY);   
        buttonDay[d].setText((lastMonDays - startWeek) + d + 1 + "    ");   
    }   
    for (int d = startWeek; d < startWeek + monthDays; d++)
    {   
        if ((d - startWeek + 1) == day)
         {   
            buttonDay[d].setForeground(Color.blue);
            buttonDay[d].setBackground(Color.GREEN);
            buttonDay[d].setFont(new Font(" ", Font.BOLD, 36));
   
         }
         else if (d % 7 == 0 || d % 7 == 6)
         {   
            buttonDay[d].setForeground(Color.RED);   
         }
         else
         {   
            buttonDay[d].setForeground(Color.BLACK);   
         }   
 
        buttonDay[d].setText(d - startWeek + 1 + " ");   
    }
      
    for (int d = monthDays + startWeek; d < 42; d++)
    {   
        buttonDay[d].setForeground(Color.GRAY);   
        buttonDay[d].setText(d - (monthDays + startWeek) + 1 + " ");   
    } 
     
}   
 
    public int getMonthDays(int year, int month)
{   
   
        switch (month)
     {   
        case 3:   
        case 5:   
        case 8:   
        case 10:   
        return 30;   
        case 1:   
        if (gregorianCalendar.isLeapYear(year))
        {   
            return 29;   
        }
        else
         {   
            return 28;   
         }   
        default:   
        return 31;   
    }   
}

   

 

----------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.text.SimpleDateFormat;

import java.util.Calendar;

class TIME extends Canvas implements Runnable
 {


    private static final long serialVersionUID = 3660124045489727166L;

    Calender2 mf;

    Thread t;

    String time;

    public TIME(Calender2 mf)
   {

        this.mf=mf;

        setSize(620,60);

        setBackground(Color.white);

        t=new Thread(this);               

        t.start();                       

    }

   public void run()
  {

    while(true)
    {

    try {Thread.sleep(1000);}
   
    catch(InterruptedException e)
    {

        System.out.println("error");

    }

    this.repaint(100);

    }

   }

    public void paint(Graphics g)
{

            Font f=new Font(" ",Font.BOLD,26);

        SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");

        Calendar now=Calendar.getInstance();

        time=SDF.format(now.getTime());       

        g.setFont(f);

        g.setColor(Color.BLUE);

        g.drawString(time,45,25);

 }