闲的无聊·写的小日历。

参考了一位大神的代码:http://liangruijun.blog.51cto.com/3061169/625909/

ClendarUI.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalendarUI{
CalendarBO bo;
JFrame cj = null;
JPanel ct=null;
JPanel up=null;
JLabel month;
JLabel year;
JButton weekday[]=new JButton[7];
JLabel daynum[]=new JLabel[42];
JButton lastmonth,nextmonth,nextyear,lastyear;
public static void main(String[] args){
CalendarUI calendar = new CalendarUI();
calendar.calendarframe();
}

public void calendarframe(){
bo = new CalendarBO();
cj = new JFrame("小日历");
ct = new JPanel();
JPanel riup = new JPanel();
JPanel ceup = new JPanel();
JLabel leup=new JLabel("",JLabel.CENTER);
month=new JLabel("",JLabel.CENTER);
year = new JLabel("",JLabel.CENTER);
ct.setLayout(new GridLayout(7,7));
String name[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

for(int i=0;i<7;i++){
weekday[i] = new JButton(name[i]);
ct.add(weekday[i]);
}
for(int i=0;i<42;i++)
{
daynum[i] = new JLabel("",JLabel.CENTER);
ct.add(daynum[i]);
}






//当前年月
month.setText(bo.getMonth());
year.setText(bo.getYear());
//打印日历
String[] print=bo.day(Integer.valueOf(year.getText()),Integer.valueOf(month.getText()));
for(int i=0;i<42;i++)
daynum[i].setText(print[i]);
//查找年月的按钮
lastmonth = new JButton("<<");
nextmonth = new JButton(">>");
lastyear = new JButton("<<");
nextyear = new JButton(">>");
ceup.add(lastyear);
//组件拼装
ceup.add(year);
ceup.add(nextyear);
riup.add(lastmonth);
riup.add(month);
riup.add(nextmonth);
up=new JPanel();
leup.setText(bo.now());
up.add(leup);
up.add(ceup);
up.add(riup);
ScrollPane ctp = new ScrollPane();
ctp.add(ct);
//布局
cj.add(ctp,BorderLayout.CENTER);
cj.add(up,BorderLayout.NORTH);

lastmonth.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int j=Integer.valueOf(month.getText())-1;
if(j<1)
{
j=12;
int k=Integer.valueOf(year.getText())-1;
year.setText(String.valueOf(k));
}
month.setText(String.valueOf(j));
String[] x=bo.day(Integer.valueOf(year.getText()),Integer.valueOf(month.getText()));
for(int i=0;i<42;i++)
daynum[i].setText(x[i]);

}
});
nextmonth.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int j=Integer.valueOf(month.getText())+1;
if(j>12)
{
j=1;
int k=Integer.valueOf(year.getText())+1;
year.setText(String.valueOf(k));
}
month.setText(String.valueOf(j));
String[] x=bo.day(Integer.valueOf(year.getText()),Integer.valueOf(month.getText()));
for(int i=0;i<42;i++)
daynum[i].setText(x[i]);

}
});

nextyear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int j=Integer.valueOf(year.getText())+1;
year.setText(String.valueOf(j));
String[] x=bo.day(Integer.valueOf(year.getText()),Integer.valueOf(month.getText()));
for(int i=0;i<42;i++)
daynum[i].setText(x[i]);

}
});
lastyear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
int j=Integer.valueOf(year.getText())-1;
year.setText(String.valueOf(j));
String[] x=bo.day(Integer.valueOf(year.getText()),Integer.valueOf(month.getText()));
for(int i=0;i<42;i++)
daynum[i].setText(x[i]);

}
});


//窗口初始化
cj.setBounds(100,100,520,370);
cj.setVisible(true);
cj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


}
}

ClendarBO.java

import java.util.*;
public class CalendarBO {

public String now(){
Calendar a = Calendar.getInstance();
int year=a.get(Calendar.YEAR);
int month=a.get(Calendar.MONTH)+1;
int day=a.get(Calendar.DAY_OF_MONTH);
String date ="今天是:"+year+"年"+month+"月"+day+"日";
return date;
}

public String getMonth(){
Calendar a = Calendar.getInstance();
int month=a.get(Calendar.MONTH)+1;
String mon=month+"";
return mon;
}
public String getYear(){
Calendar a = Calendar.getInstance();
int year=a.get(Calendar.YEAR);
String y=year+"";
return y;
}

public String[] day(int year,int month)
{
String[] s=new String[42];
int day=0,week;
Calendar a = Calendar.getInstance();
a.set(year,month-1,1);
week=a.get(Calendar.DAY_OF_WEEK)-1;

if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
if(month==4||month==6||month==11||month==9)
day=30;
if(month==2)
{
if((year%4==0&&year%100!=0)||year%400==0)
day=29;
else
day=28;
}
for(int i=week,n=1;n<=day;i++)
{
s[i]=n+"";
n++;
}
return s;
}


}

猜你喜欢

转载自903180552.iteye.com/blog/2311001
今日推荐