/****************************************************************************************
* Java日历程序
* 作者:张亚航
*
* 完成时间:2008-5-22
*
* 程序功能:
* 使用Swing组件编写界面,能够正确显示月历,并且拥有四个按钮分别进行显示控制
*
* 设计思路:
* 本程序总体使用MVC设计模式,其中viewer包中的CalWin程序是主界面显示程序,
* 主要用于界面的生成和数据的刷新;
*
* Model包中的MyDate类是程序日历的载体,本程序没有使用JDK的类库java.util.Date和
* java.util.Calendar类,而是重新构造了MyDate类,该类只能保存精度到日的时间
*
* Control包中的ComputeDate类主要是用来计算每个月的第一天是星期几以及每个月的总天数
*
* Viewer类中包含了一个内部类Cell,Cell是界面日期表格中显示中的基本元素,继承与JLabel
* ,每个Cell对应一个MyDate对象。
*
* 程序主要流程是根据当前baseDate利用ComputeDate计算当前月份的数据,然后将所有数据
* 通过setCell方法同Cell[]数组关联起来,初始化所有Cell,然后将数据在界面显示。
* 每次日期发生改变,都要调用一次setCell方法,将所有界面数据刷新。
*
******************************************************************************************/
package viewer;
import control.*;
import model.MyDate;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.event.*;
public class CalWin extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
/*******************所有界面元素******************************/
private JPanel jContentPane = null;
private JPanel controlPanel = null;
private JPanel showPanel = null;
private JPanel buttonPanel = null;
private JButton OkButton = null;
private JButton cancelButton = null;
private JTextField dateTextField = null;
private JTextField yearTextField = null;
private JTextField monthTextField = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private BasicArrowButton addButton = null;//上箭头增加按钮
private BasicArrowButton delButton = null;//下箭头递减按钮
private BasicArrowButton delMonthButton = null;//左箭头增加月份按钮
private BasicArrowButton addMonthButton = null;//右箭头减少月份按钮
private JPanel tablePanel = null;//添加日期版面
private JPanel headerPanel = null;//存放星期的头版
//private ButtonGroup group = null;
//private SetCell setCell = null;
/*******************所有逻辑计算相关元素******************************/
private JLabel[] headerCell = null;
private Cell[] cell = null;
private MyDate baseDate = null; //关键变量,记录当前日期,很多计算都基于这个变量!!!
private ComputeDate computeDate = null;
private MyDate cellDate1 = null; //对每个cell元素进行设置的时候所要设置对应的日期
private int changeFlag = 1; //标记递增或递减哪个元素,1的时候修改日,2的时候修改月,三的时候修改年
/**
* 主函数
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CalWin calendar = new CalWin();
calendar.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calendar.setVisible(true);
}
public CalWin() {
super();
baseDate = new MyDate();
this.setSize(400, 300);
this.setTitle("JCalendar 1.2");
this.setResizable(false);
this.initialize();
}
/**初始化界面函数
* 在这里注意:由于要求上的界面很难实现所有组件
* 按照图中所示对其,因此本界面没有采用任何布局管理器
* 而是通过对每个组件设置边界位置的方式进行布局管理
* */
private void initialize() {
System.out.println(baseDate);
jContentPane = new JPanel();
dateTextField = new JTextField();
dateTextField.setBounds(new Rectangle(10, 10, 20, 30));
dateTextField.setText((new Integer(baseDate.getDate()).toString()));
dateTextField.setBorder(null);
dateTextField.setEditable(false);
dateTextField.addActionListener(this);
dateTextField.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
changeFlag = 1;
System.out.print("aaaaaaaaaa"+changeFlag);
}
});
jLabel1 = new JLabel();
jLabel1.setText("-");
jLabel1.setBounds(new Rectangle(30, 10, 10, 30));
monthTextField = new JTextField();
monthTextField.setBounds(new Rectangle(45, 10, 20, 30));
monthTextField.setText((new Integer(baseDate.getMonth()).toString()));
monthTextField.setBorder(null);
monthTextField.setEditable(false);
//monthTextField.addActionListener(this);
monthTextField.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
changeFlag = 2;
System.out.print("aaaaaaaaaa"+changeFlag);
}
});
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(65, 10, 10, 30));
jLabel2.setText("-");
yearTextField = new JTextField();
yearTextField.setBounds(new Rectangle(80, 10, 30, 30));
yearTextField.setText((new Integer(baseDate.getYear()).toString()));
yearTextField.setBorder(null);
yearTextField.setEditable(false);
yearTextField.addActionListener(this);
yearTextField.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
changeFlag = 3;
System.out.print("aaaaaaaaaa"+changeFlag);
}
});
addButton = new BasicArrowButton(SwingConstants.NORTH);
addButton.setBounds(new Rectangle(300, 10, 50, 15));
addButton.addActionListener(this);
addButton.setBorder(null);
delButton = new BasicArrowButton(SwingConstants.SOUTH);
delButton.setBounds(new Rectangle(300, 25, 50, 15));
delButton.addActionListener(this);
controlPanel = new JPanel();
controlPanel.setLayout(null);
controlPanel.setBounds(new Rectangle(20, 5, 360, 45));
controlPanel.add(dateTextField);
controlPanel.add(jLabel1);
controlPanel.add(monthTextField);
controlPanel.add(jLabel2);
controlPanel.add(yearTextField);
controlPanel.add(addButton);
controlPanel.add(delButton);
controlPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
jContentPane.setLayout(null);
jContentPane.add(controlPanel);
showPanel = new JPanel();
delMonthButton = new BasicArrowButton(SwingConstants.WEST);
delMonthButton.setBounds(new Rectangle(10, 10, 50, 20));
delMonthButton.setBorder(null);
addMonthButton = new BasicArrowButton(SwingConstants.EAST);
addMonthButton.setBounds(new Rectangle(300, 10, 50, 20));
addMonthButton.setBorder(null);
addMonthButton.setBackground(showPanel.getBackground());
addMonthButton.setOpaque(true);
addMonthButton.addActionListener(this);
delMonthButton.addActionListener(this);
headerPanel = new JPanel();
headerPanel.setLayout(new GridLayout(1,7));
headerPanel.setBounds(new Rectangle(70, 10, 220, 20));
headerCell = new JLabel[7];
//在日期头上加入星期的标志
for(int i=0;i<7;i++)
{
String weekFlag = "";
headerCell[i] = new JLabel();
switch (i)
{
case 0:weekFlag = "Mon";break;
case 1:weekFlag = "Tue";break;
case 2:weekFlag = "Wed";break;
case 3:weekFlag = "Thu";break;
case 4:weekFlag = "Fri";break;
case 5:weekFlag = "Sat";break;
case 6:weekFlag = "Sun";break;
}
headerCell[i].setText(weekFlag);
headerPanel.add(headerCell[i]);
}
headerPanel.setBackground(Color.blue);
tablePanel = new JPanel();
tablePanel.setLayout(new GridLayout(6,7));
tablePanel.setBounds(new Rectangle(70, 35, 220, 110));
cell = new Cell[42];
//setCell = new SetCell();
//初始化cell[42]数组里面的cell对象
baseDate = new MyDate();
for(int i=0;i<42;i++)
{
cell[i] = new Cell();
//group.add(cell[i]);
}
// 为cell[42]数组里面的cell对象置入初值
cell = SetCell(cell, baseDate);
//group.add(cell[i]);
for(int i=0;i<42;i++)
{
ta