import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JDatePanel extends JPanel implements ActionListener {
protected int curYear;
protected int curMonth;
protected int curDate;
final Font font14 = new Font("Monospaced", 0, 14);
final Font font12 = new Font("Monospaced", 0, 12);
JLabel lblYear;
JLabel lblMonth;
JLabel lblSun;
JLabel lblMon;
JLabel lblTues;
JLabel lblWed;
JLabel lblThurs;
JLabel lblFri;
JLabel lblSat;
JButton btDay[] = new JButton[43];
JButton btYearL;
JButton btYearR;
JButton btMonthL;
JButton btMonthR;
JTextField txtYear;
JTextField txtMonth;
Container ob;
Container parent;
public JDatePanel(Container parent, Container ob) {
this.parent = parent;
this.ob = ob;
init();
}
public void setContainer(Container ob){
this.ob = ob;
}
private void init() {
setFont(font14);
lblSun = new JLabel(" 日");
lblMon = new JLabel(" 一");
lblTues = new JLabel(" 二");
lblWed = new JLabel(" 三");
lblThurs = new JLabel(" 四");
lblFri = new JLabel(" 五");
lblSat = new JLabel(" 六");
// btDay = new JButton[32];
btYearL = new JButton("<<");
btYearR = new JButton(">>");
btMonthL = new JButton("<");
btMonthR = new JButton(">");
txtYear = new JTextField("年");
txtMonth = new JTextField("月");
for (int i = 1;
i < btDay.length;
i++) {
btDay[i] = new JButton(String.valueOf(i));
btDay[i].setActionCommand("day");
btDay[i].addActionListener(this);
}
btYearL.addActionListener(this);
btYearR.addActionListener(this);
btMonthL.addActionListener(this);
btMonthR.addActionListener(this);
lblSun.setFont(font14);
lblMon.setFont(font14);
lblTues.setFont(font14);
lblWed.setFont(font14);
lblThurs.setFont(font14);
lblFri.setFont(font14);
lblSat.setFont(font14);
btYearL.setFont(font14);
btYearR.setFont(font14);
btMonthL.setFont(font14);
btMonthR.setFont(font14);
txtYear.setFont(font14);
txtMonth.setFont(font14);
setLayout(null);
btYearL.setBounds(5, 5, 50, 25);
txtYear.setBounds(60, 5, 50, 25);
btYearR.setBounds(115, 5, 50, 25);
btMonthL.setBounds(170, 5, 50, 25);
txtMonth.setBounds(225, 5, 50, 25);
btMonthR.setBounds(280, 5, 50, 25);
lblSun.setBounds(0, 35, 50, 25);
lblMon.setBounds(50, 35, 50, 25);
lblTues.setBounds(100, 35, 50, 25);
lblWed.setBounds(150, 35, 50, 25);
lblThurs.setBounds(200, 35, 50, 25);
lblFri.setBounds(250, 35, 50, 25);
lblSat.setBounds(300, 35, 50, 25);
lblSun.setForeground(Color.red);
lblSat.setForeground(Color.red);
int x = 0;
int y = 65;
for (int i = 1;
i < btDay.length;
i++) {
btDay[i].setBounds(x, y, 50, 25);
btDay[i].setFont(font12);
if (i % 7 == 0) {
x = 0;
y += 30;
}
else
{
x += 50;
}
add(btDay[i]);
}
paintDay(0, 0, 0);
add(btYearL);
add(txtYear);
add(btYearR);
add(btMonthL);
add(txtMonth);
add(btMonthR);
add(lblSun);
add(lblMon);
add(lblTues);
add(lblWed);
add(lblThurs);
add(lblFri);
add(lblSat);
}
protected void paintDay(int year, int month, int date) {
Calendar today = Calendar.getInstance();
Calendar pointer = (Calendar) today.clone();
Color btFColor = btYearL.getForeground();
Color btBColor = btYearL.getBackground();
if (year != 0)
pointer.set(Calendar.YEAR, year);
if (month != 0)
pointer.set(Calendar.MONTH, month - 1);
if (date != 0)
pointer.set(Calendar.DAY_OF_MONTH, date);
curYear = pointer.get(Calendar.YEAR);
curMonth = pointer.get(Calendar.MONTH) + 1;
curDate = pointer.get(Calendar.DATE);
txtYear.setText(curYear + "年");
txtMonth.setText(curMonth + "月");
pointer.set(Calendar.DATE, 1);
int dayOfWeek = pointer.get(Calendar.DAY_OF_WEEK);
int maxDay = pointer.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 0;
i < dayOfWeek - 1;
i++) {
btDay[i + 1].setText("");
}
for (int i = 1;
i <= maxDay;
i++) {
btDay[dayOfWeek + i - 1].setText(String.valueOf(i));
pointer.set(Calendar.DATE, i);
if(today.equals(pointer)){
btDay[dayOfWeek + i - 1].setForeground(Color.white);
btDay[dayOfWeek + i - 1].setBackground(Color.blue);
}
else
{
btDay[dayOfWeek + i - 1].setForeground(btFColor);
btDay[dayOfWeek + i - 1].setBackground(btBColor);
}
}
for (int i = maxDay + dayOfWeek;
i < btDay.length;
i++) {
btDay[i].setText("");
}
}
public void actionPerformed(ActionEvent e) {
int preMonth = curMonth - 1;
int nextMonth = curMonth + 1;
if (preMonth < 1)
preMonth = 12;
if (nextMonth > 12)
nextMonth = 1;
if (e.getSource() == btYearL)
// JOptionPane.showMessageDialog(this, "");
paintDay(curYear - 1, curMonth, 1);
if (e.getSource() == btYearR)
paintDay(curYear + 1, curMonth, 1);
if (e.getSource() == btMonthL)
paintDay(curYear, preMonth, 1);
if (e.getSource() == btMonthR)
paintDay(curYear, nextMonth, 1);
if (e.getActionCommand().equals("day")) {
String date = ((JButton) e.getSource()).getText().trim();
String day = "";
if(date.length()>0)
day += curYear + "/" + curMonth + "/" + date;
JTextField b = (JTextField) ob;
// JButton b = (JButton) ob;
b.setText(day);
// JComponent dialog = (JComponent) parent;
parent.setVisible(false);
// dialog.setVisible(false);
}
}
}