📄 calenderdialog.java
字号:
package mypack;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalenderDialog extends JFrame implements ItemListener{
/**
*
*/
private static final long serialVersionUID = 2658632819629485313L;
JComboBox list=null;
CalenderPane calenderPane=null;
Container con=null;
CalenderDialog()
{
list=new JComboBox();
list.addItem(2007);
for(int i=2000;i<=2010;i++)
{
String temp=""+i;
list.addItem(temp);
}
list.addItemListener(this);
con=this.getContentPane();
con.add(list,BorderLayout.NORTH);
calenderPane=new CalenderPane(2007);
con.add(calenderPane,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100,100,400,240);
setVisible(true);
validate();
}
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成方法存根
con.remove(calenderPane);
String str=(String)list.getSelectedItem();
int year=Integer.parseInt(str);
calenderPane=new CalenderPane(year);
con.add(calenderPane,BorderLayout.CENTER);
con.validate();
validate();
}
}
class CalenderPane extends JPanel implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = -4099676573160654052L;
JTable table;
CalendarBean calendar;
Object a[][]=new Object[6][7];
Object name[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JButton nextMonth,previousMonth;
int year=2007,month=1;
JLabel showMessage=new JLabel("",JLabel.CENTER);
public CalenderPane(int year){
setLayout(new BorderLayout());
calendar=new CalendarBean();
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar(); //返回一个自定义的日历主面板
table=new JTable(a,name);
table.setRowSelectionAllowed(false);
setTable(day);
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("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月");
add(new JScrollPane(table),BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
add(pSouth,BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if(e.getSource()==nextMonth)
{
month=month+1;
if(month>12) month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
setTable(day);
table.repaint();
}
else if(e.getSource()==previousMonth)
{
month=month-1;
if(month<1) month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
setTable(day);
table.repaint();
}
showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月");
}
public void setTable(String day[])
{
int n=0;
for(int i=0;i<6;i++){
for(int j=0;j<7;j++){
a[i][j]=day[n];
n++;
}
}
}
}
class CalendarBean //自定义日历主面板
{
String day[];
int year=2007,month=0;
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
this.month=month;
}
public int getYear(){
return year;
}
public int getMonth(){
return month;
}
public String[] getCalendar(){ //返回一个日期面板
String a[]=new String[42];
Calendar calendar=Calendar.getInstance();
calendar.set(year, month-1,1);
int weekday=calendar.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
day=31;
}
if(month==4||month==6||month==7||month==8||month==9||month==11){
day=30;
}
if(month==2){
if(((year%4==0)&&(year%100!=0))||(year%400==0)){
day=29;
}
else {
day=28;
}
}
for(int i=weekday,n=1;i<weekday+day;i++)
{
a[i]=String.valueOf(n);
n++;
}
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -