package com.lian.swing;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class CalebdarFrame extends JFrame implements ActionListener{
/**
* 日历
* Author: miluo
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CalebdarFrame();
}
JTable table;
JButton nextmonth,previousmonth;
int year=2012, month=5;
CalendarBean calenbean;
Object[] name={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
String rili[][];
JLabel showMessage=new JLabel("", JLabel.CENTER);
JScrollPane scroll;
public CalebdarFrame(){
calenbean=new CalendarBean();
calenbean.setYear(year);
calenbean.setMonth(month);
rili=calenbean.getCalendar();
// for(int i=0;i<name.length;i++){
// rili[0][i]=(String) name[i];
// }
DefaultTableModel model=new DefaultTableModel(rili,name);
table=new JTable(model);
table.setRowSelectionAllowed(false);
nextmonth=new JButton("下个月");
previousmonth=new JButton("上个月");
nextmonth.addActionListener(this);
previousmonth.addActionListener(this);
JPanel pNorth=new JPanel(),
pSouth=new JPanel();
pNorth.add(previousmonth);
pNorth.add(nextmonth);
pSouth.add(showMessage);
showMessage.setText("日历:"+calenbean.getYear()+"年"+calenbean.getMonth()+"月");
scroll=new JScrollPane(table);
add(scroll,BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
add(pSouth,BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,280,220);
setVisible(true);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==nextmonth){
month=month+1;
if(month>12){
month=1;
year+=1;
}
calenbean.setYear(year);
calenbean.setMonth(month);
int firstday=1;
String pTime=year+"-"+month+"-"+firstday;
try {
int week=dayForWeek(pTime);
rili=getCaldar(week);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// rili=calenbean.getCalendar();
remove(scroll);
DefaultTableModel model=new DefaultTableModel(rili,name);
table=new JTable(model);
table.setRowSelectionAllowed(false);
scroll=new JScrollPane(table);
add(scroll,BorderLayout.CENTER);
}
else{
month=month-1;
if(month<1){
month=12;
year-=1;
}
calenbean.setYear(year);
calenbean.setMonth(month);
int firstday=1;
String pTime=year+"-"+month+"-"+firstday;
try {
int week=dayForWeek(pTime);
rili=getCaldar(week);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// rili=calenbean.getCalendar();
remove(scroll);
DefaultTableModel model=new DefaultTableModel(rili,name);
table=new JTable(model);
table.setRowSelectionAllowed(false);
scroll=new JScrollPane(table);
add(scroll,BorderLayout.CENTER);
}
showMessage.setText("日历:"+calenbean.getYear()+"年"+calenbean.getMonth()+"月");
}
public static int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 6;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 2;
}
return dayForWeek;
}
public String[][] getCaldar(int weeks){
String[][] a=new String[6][7];
int day=0;
int nextday=1;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
day=31;
if(month==4||month==6||month==9||month==11)
day=30;
if(month==2){
if((year%4==0)&&(year%400!=0)||(year%400==0))
day=29;
else
day=28;
}
for(int k=0;k<6;k++){
if(k==0){
for(int j=weeks;j<7;j++){
a[k][j]=""+nextday;
nextday++;
}
}
else{
for(int j=0;j<7&&nextday<=day;j++){
a[k][j]=""+nextday;
nextday++;
}
}
}
return a;
}
}
![]()
![]()