java日历如何让combox实现事件
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class RL
{
public RL()
{
Date date=new Date();//获取日期
int date_year=date.getYear()+1900;//获取年份
int date_month=date.getMonth()+1;//获取月份
CalendaBean cb = new CalendaBean();
JPanel pSouth = new JPanel();
final boolean[] todayFlag = {false};
String year_int = null;
int month_int;
cb.setYear(date_year);
cb.setMonth(date_month);
JComboBox MonthBox = new JComboBox();
JComboBox YearBox = new JComboBox();
JFrame f=new JFrame();
FlowLayout F=new FlowLayout(FlowLayout.CENTER);
JPanel p=new JPanel(F);
JLabel j1=new JLabel("年份:");
JLabel j2=new JLabel("月份:");
JLabel j3=new JLabel();
JButton button=new JButton("确定");
JButton button_today = new JButton("今天");
JButton u=new JButton("上个月");
JButton h=new JButton("下个月");
u.setActionCommand("lastmonth");
h.setActionCommand("nextmonth");
u.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cb.actionPerformed(e);
}
});
h.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cb.actionPerformed(e);
}
});
p.add(j1);
p.add(YearBox);
p.add(j2);
p.add(MonthBox);
p.add(button);
p.add(button_today);
p.add(u);
p.add(h);
p.add(j3);
this.setTimer(j3);
GridLayout G=new GridLayout(7,7);
JPanel p1=new JPanel(G);
JLabel[] label;
String[] a = cb.getCalendar();
JButton week1 = new JButton("日");
JButton week2 = new JButton("一");
JButton week3 = new JButton("二");
JButton week4 = new JButton("三");
JButton week5 = new JButton("四");
JButton week6 = new JButton("五");
JButton week7 = new JButton("六");
p1.add(week1);
p1.add(week2);
p1.add(week3);
p1.add(week4);
p1.add(week5);
p1.add(week6);
p1.add(week7);
Font font = new Font("Dialog",Font.BOLD,12);
u.setFont(font);
h.setFont(font);
for(int i = date_year - 20;i <= date_year + 100;i++){
YearBox.addItem(i+"");
}
YearBox.setSelectedIndex(20);
for(int i = 1;i < 13;i++){
MonthBox.addItem(i+"");
}
MonthBox.setSelectedIndex(date_month-1);
label = new JLabel[42];
//for循环进行日期排序
for (int i = 0; i < 42; i++) {
label[i] = new JLabel();
p1.add(label[i]);
}
cb.label = label;
for (int i = 0; i < a.length; i++) {
if (i % 7 == 0) {
label[i].setText("");
}
label[i].setText(" "+a[i]);
}
p1.setLayout(G);
f.add(p1,BorderLayout.CENTER);
f.add(p,BorderLayout.NORTH);
f.setSize(700,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
JLabel now = new JLabel();
now.setText(""+cb.year+"年"+""+cb.month+"月");
cb.now = now;
pSouth.add(now);
f.setLocationRelativeTo(null);
f.add(pSouth, BorderLayout.SOUTH);
}
//动态时间线程
private void setTimer(JLabel j3)
{
final JLabel varTime=j3;
Timer timeAction=new Timer(100, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
long timeillis=System.currentTimeMillis();
SimpleDateFormat df=new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
varTime.setText(df.format(new Date(timeillis)));
}
}
);
timeAction.start();
}
//上下月翻页事件处理
class CalendaBean implements ActionListener {
JLabel[] label;
JLabel now;
int year=0, month=0;
public void setYear(int year) {
this.year = year;
}
public void setMonth(int month) {
this.month = month;
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("lastmonth")) {
month--;
if (month == 0) {
month = 12;
year--;
}
}
else if (str.equals("nextmonth")) {
month++;
if (month == 13) {
month = 1;
year++;
}
}
now.setText(""+year+"年"+""+month+"月");
String[] a = getCalendar();
for (int i = 0; i < a.length; i++) {
if (i % 7 == 0) {
label[i].setText("");
}
label[i].setText(" "+a[i]);
}
}
public String[] getCalendar() {
String[] a = new String[42];
Calendar rili = Calendar.getInstance();
rili.set(year, month - 1, 1);
int weekDay = rili.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 == 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 = 0; i < weekDay; i++)
a[i] = "";
for (int i = weekDay, n = 1; i < weekDay + day; i++) {
a[i] = String.valueOf(n);
n++;
}
for (int i = weekDay + day; i < a.length; i++)
a[i] = "";
return a;
}
}
public static void main(String[] args)
{
new RL();
}
}