我的用java做的日历,图如上,
当改变年时间的时候,不能使下面的日历改变,具体要调用jlist的哪几个方法,才能使日历改变;
就像电脑自带的日历一样
class DatePanel extends JPanel implements ActionListener
{
MenologyPanel mpanel=null;//我自己定义的一个panel可以打印日历的panel
JPanel panel=null;
int year=2011,month=1;
JComboBox jcb1;
JList jlist;
JScrollPane jsp;
String str2[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String str1[];
public DatePanel()
{
str1=new String[120];
for(int i=0;i<120;i++){
str1[i]=new String((1980+i)+"");
}
this.setLayout(new BorderLayout());
panel=new JPanel();
panel.setLayout(new GridLayout(1,2,50,0));
jcb1=new JComboBox(str2);
jlist=new JList(str1);
jsp=new JScrollPane(jlist);
jlist.setVisibleRowCount(1);
jlist.setSelectedIndex(31);
// jlist.addAncestorListener(this);
jcb1.setBorder(BorderFactory.createMatteBorder(0, 10, 0, 10,jcb1.getBackground()));
//jsp.setBorder(BorderFactory.createMatteBorder(0, 10,0, 10, jsp.getBackground()));
jcb1.addActionListener(this);
panel.add(jcb1);
panel.add(jsp);
this.add(panel,BorderLayout.NORTH);
mpanel=new MenologyPanel(year,month);
mpanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(10, 10, 10, 10, mpanel.getBackground()),
BorderFactory.createBevelBorder(BevelBorder.LOWERED)));
add(mpanel,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int i=jcb1.getSelectedIndex();
month=i+1;
try {
this.remove(mpanel);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mpanel=new MenologyPanel(year,month);
mpanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(10, 10, 10, 10, mpanel.getBackground()),
BorderFactory.createBevelBorder(BevelBorder.LOWERED)));
add(mpanel,BorderLayout.CENTER);
this.validate();//重新设置定位
}
}