简单日历

package GUI;
import java.awt.BorderLayout;
public class CalenderTrain extends JFrame implements ActionListener{
private String year_int;
private int month_int=0;
private boolean todayflag=false;
private JComboBox monthbox=new JComboBox();
private JComboBox yearbox=new JComboBox();
private JLabel year=new JLabel("年份:");
private JLabel month=new JLabel("月份:");
private JPanel top=new JPanel();
private JPanel main=new JPanel();
private JButton ok=new JButton("查看");
private JButton today=new JButton("今天");
private Date date=new Date();
private int now_year=date.getYear()+1900;
private int now_month=date.getMonth();
private JButton button[]=new JButton[7];
private final String b[]=new String[]{
"日","一","二","三","四","五","六"};
private JButton bu[]=new JButton[42];
public CalenderTrain(){
for(int i=now_year-10;i<=now_year+20;i++){
yearbox.addItem(i);
}
yearbox.setSelectedItem(now_year);
for(int i=1;i<13;i++){
monthbox.addItem(i);
}
monthbox.setSelectedItem(now_month+1);
ok.addActionListener(this);
today.addActionListener(this);
top.add(year);
top.add(yearbox);
top.add(month);
top.add(monthbox);
top.add(ok);
top.add(today);
this.add(top,BorderLayout.NORTH);
for(int i=0;i<button.length;i++){
button[i]=new JButton(b[i]);
main.add(button[i]);
}
for(int i=0;i<bu.length;i++){
bu[i]=new JButton(i+"");
main.add(bu[i]);
}
this.pain();
main.setLayout(new GridLayout(7,7));
this.add(main,BorderLayout.CENTER);
this.setBounds(200, 200, 400, 400);
this.setTitle("日历");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void pain(){
int day_week=0;
int days=0;
if(todayflag==true){
year_int=now_year+"";
month_int=now_month;
}else{
year_int=yearbox.getSelectedItem().toString();
month_int=monthbox.getSelectedIndex();
}
int years=Integer.parseInt(year_int.trim())-1900;
Date firstday=new Date(years,month_int,1);
GregorianCalendar cal=new GregorianCalendar();
cal.setTime(firstday);
if(month_int==0||month_int==2||month_int==4||month_int==6||month_int==7||month_int==9||month_int==11){
days=31;
}else if(month_int==3||month_int==5||month_int==8||month_int==10){
days=30;
}else if(cal.isLeapYear(years)){//判断该年是否为闰年,是闰年,2月份为29天,不是闰年,2月份为28天
days=29;
}else{
days=28;
}
int count=1;
day_week=firstday.getDay();
// System.out.println(month_int);
// System.out.println(new Date().getMonth());
for(int i=day_week;i<day_week+days;i++,count++){
if((years==new Date().getYear())&&(month_int==new Date().getMonth())&&(new Date().getDate()+day_week-1==i)){
bu[i].setForeground(Color.red);
}else{
bu[i].setForeground(Color.black);
}
bu[i].setText(count+"");
}
if(day_week!=0){
for(int i=0;i<day_week;i++){
bu[i].setText(" ");
}
for(int j=day_week+days;j<42;j++){
bu[j].setText(" ");
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==today){
todayflag=true;
monthbox.setSelectedItem(now_month+1);
yearbox.setSelectedItem(now_year);
this.pain();
}
if(e.getSource()==ok){
todayflag=false;
this.pain();
}
}
public static void main(String[] args) {
CalenderTrain calendertrain=new CalenderTrain();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值