package cn.lidan.mycalendar;
import cn.lidan.utils.Const;
import cn.lidan.vo.DayWeek;
import cn.lidan.vo.YearMonth;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import javax.swing.*;
public class YearMonthDay extends JFrame implements ActionListener{
//面板容器
private JPanel chooseJpl = new JPanel();
private JPanel weekJpl = new JPanel();
private JPanel dateJpl = new JPanel();
//年月日的选择框
private JComboBox cboYear = new JComboBox();
private JComboBox cboMonth = new JComboBox();
//年月日标签
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
public void init(){
// 年的label
jLabel3.setText("年");
// 月的label
jLabel2.setText("月");
/**
* 如果一容器使用了布局管理器
* 则该容器内的组件或容器(即该容器的下一级)
* 应该使用setPreferredSize(new Dimension(w,h))来设置尺寸
*/
/**
* 如果一容器是绝对布局,即setLayout(null)
* 则该容器内的组件或容器(即该容器的下一级)
* 应该使用setSize(w,h))来设置尺寸
*/
cboYear.setPreferredSize(new Dimension(100, 30));
cboMonth.setPreferredSize(new Dimension(100, 30));
chooseJpl.add(cboYear);
chooseJpl.add(jLabel3);
chooseJpl.add(cboMonth);
chooseJpl.add(jLabel2);
this.setTitle("万年历");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(Const.FRAME_X, Const.FRAME_Y,Const.FRAME_WIDTH, Const.FRAME_HEIGHT);
this.setLayout(null);
chooseJpl.setBounds(12, 10, 7*Const.SIZE, Const.SIZE);
weekJpl.setBounds(12, 65, 7*Const.SIZE, Const.SIZE);
dateJpl.setLocation(12, 115);
this.add(chooseJpl);
this.add(weekJpl);
this.add(dateJpl);
this.addYear();
this.addMonth();
this.addWeeks();
this.addDays();
this.setVisible(true);
}
public void addYear(){
for(int i = Const.FIRST_YEAR; i < Const.LAST_YEAR; i++){
this.cboYear.addItem(i);
}
this.cboYear.setSelectedItem(Calendar.getInstance().get(Calendar.YEAR));
}
public void addMonth(){
for(int i=1; i<=12; i++){
this.cboMonth.addItem(i);
}
this.cboMonth.setSelectedItem(Calendar.getInstance().get(Calendar.MONTH)+1);
}
public void addWeeks(){
weekJpl.setLayout(new GridLayout(1,7));
for(int i = 0; i < Const.WEEKS.length; i++){
JLabel label = new JLabel();
label.setText(Const.WEEKS[i]);
label.setSize(Const.SIZE, Const.SIZE);
label.setHorizontalAlignment(SwingConstants.CENTER);
weekJpl.add(label);
}
}
public void addDays(){
//清空中间面板
clearCenterPanel();
DayWeek dayWeek = getWeekAndDay();
dateJpl.setLayout(null);
dateJpl.setSize(7*Const.SIZE, ((dayWeek.getDay()+dayWeek.getWeek())/7+1)*Const.SIZE);
for(int i=0; i<dayWeek.getDay(); i++){
JButton btn = new JButton();
btn.setSize(Const.SIZE, Const.SIZE);
btn.setText((i+1)+"");
btn.setBackground(Color.yellow);
btn.setLocation(((i+dayWeek.getWeek())%7)*Const.SIZE, (i+dayWeek.getWeek())/7*Const.SIZE);
btn.addActionListener(this);
dateJpl.add(btn);
}
}
public DayWeek getWeekAndDay(){
//获取年月
YearMonth ym = getSelectYearAndMonth();
Calendar cl = Calendar.getInstance();
cl.set(Calendar.YEAR, ym.getYear());//设置当前年
cl.set(Calendar.MONTH, ym.getMonth()-1);//设置当前月
cl.set(Calendar.DATE, 1);//设置当前年、当前月的第一天 因为需要计算一号是星期几
int week = cl.get(Calendar.DAY_OF_WEEK)-1;//当前年、当前月的第一天是星期几
cl.roll(Calendar.DATE, -1);
int days = cl.get(Calendar.DATE);
DayWeek dayWeek = new DayWeek(week,days);
return dayWeek;
}
public YearMonth getSelectYearAndMonth(){
//获取年月
int year = Integer.parseInt(cboYear.getSelectedItem().toString());
int month = Integer.parseInt(cboMonth.getSelectedItem().toString());
YearMonth yearMonth = new YearMonth(year,month);
return yearMonth;
}
public void clearCenterPanel(){
/**
* 先用removeAll()移除了所有组件,但是并未刷新
* 所以单使用这个方法虽然内部组件确实改变了,但是界面显示可能会不变
* repaint()相当于刷新了整个面板
*/
dateJpl.removeAll();
dateJpl.repaint();
}
public void changeMonthHandler(){
cboMonth.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange() == ItemEvent.SELECTED){
addDays();
}
}
});
}
public void changeYearHandler(){
cboYear.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent arg0) {
//getStateChange()
//返回状态更改的类型(已选定或已取消选定)
if(arg0.getStateChange() == ItemEvent.SELECTED){
addDays();
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
YearMonth ym = getSelectYearAndMonth();
String day = e.getActionCommand().toString();
JOptionPane.showMessageDialog(null,
ym.getYear()+"年" + ym.getMonth() + "月" + day + "日",
"日期",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* 程序入口
*/
public static void main(String[] args) {
YearMonthDay ymd = new YearMonthDay();
ymd.init();
ymd.changeMonthHandler();
ymd.changeYearHandler();
}
}