package hao;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
//import java.awt.font.*;
import javax.swing.*;
//import java.text.*;
public class DateQuery extends JFrame {
/**
*
*/
private static final long serialVersionUID = 0L;
private Calendar temp = Calendar.getInstance();
private JLabel Lyear;
private JLabel Lmonth;
private JLabel Lcalen;
private JLabel Ltime;
private JComboBox CBmonth = null;
private JComboBox CByear = null;
private String month[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12" };
private String year[] = new String[9999];
private JLabel[] dayPan = new JLabel[42];
private int width = 500;
private int height = 400;
private JPanel queryJPanel;
private JPanel dayAndWeekJPanel;
private JPanel weekDateJPanel;
private JPanel JPButtons;
private JPanel showTime1;
private JPanel north;
private JPanel echoArea;
private JButton yearUp;
private JButton yearDown;
private JButton monthUp;
private JButton monthDown;
private JButton currentDate;
public DateQuery(String s) {
super(s);
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
private void init() throws Exception {
this.getContentPane().setPreferredSize(new Dimension(700, 700));
queryJPanel = new JPanel();
weekDateJPanel = new JPanel();
dayAndWeekJPanel = new JPanel();
JPButtons = new JPanel();
showTime1 = new JPanel();
north = new JPanel();
echoArea = new JPanel();
Ltime = new JLabel();
showYearAndMonth();
weekDatePane();
showDayAndWeekPan();
createButtonPane();
int delay = 1000;
ActionListener drawClock = new ActionListener() {
public void actionPerformed(ActionEvent ee) {
// TODO 自动生成方法存根
paint();
}
};
new Timer(delay, drawClock).start();
echoArea.setLayout(new GridLayout(2, 1));
north.setLayout(new GridLayout(1, 2));
showTime1.add(Ltime);
north.add(queryJPanel);
north.add(showTime1);
showTime1.setBackground(Color.PINK);
echoArea.add(north);
echoArea.add(weekDateJPanel);
this.getContentPane().add(dayAndWeekJPanel, BorderLayout.CENTER);
this.getContentPane().add(echoArea, BorderLayout.NORTH);
this.getContentPane().add(JPButtons, BorderLayout.SOUTH);
this.getContentPane().validate();
// 建立框架
this.setLocation(100, 100);
this.setResizable(false);
this.setVisible(true);
this.setSize(width, height);
this.validate();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
addListener();
}
private void addListener() {
// TODO 自动生成方法存根
CBmonth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CBmonth_actionPerformed();
}
});
CByear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CByear_actionPerformed();
}
});
yearUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
yearUp_actionPerformed();
}
});
yearDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
yearDown_actionPerformed();
}
});
monthUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monthUp_actionPerformed();
}
});
monthDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
monthDown_actionPerformed();
}
});
currentDate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentDate_actionPerformed();
}
});
}
public void CBmonth_actionPerformed() {
updateJPanel(CBmonth.getSelectedIndex());
}
public void CByear_actionPerformed() {
temp.set(Calendar.YEAR, CByear.getSelectedIndex() + 1);
updateJPanel(CBmonth.getSelectedIndex());
}
public void yearUp_actionPerformed() {
temp.set(Calendar.YEAR, CByear.getSelectedIndex());
CByear.setSelectedIndex(CByear.getSelectedIndex() - 1);
updateJPanel(CBmonth.getSelectedIndex());
}
public void yearDown_actionPerformed() {
temp.set(Calendar.YEAR, CByear.getSelectedIndex() + 2);
CByear.setSelectedIndex(CByear.getSelectedIndex() + 1);
updateJPanel(CBmonth.getSelectedIndex());
}
public void monthUp_actionPerformed() {
int tempMonth;
tempMonth = CBmonth.getSelectedIndex() - 1;
if (tempMonth < 0) {
tempMonth = 11;
if (CByear.getSelectedIndex() == 0) {
JOptionPane.showMessageDialog(this, "超出查找范围", "警告对话框",
JOptionPane.WARNING_MESSAGE);
return;
}
temp.set(Calendar.YEAR, CByear.getSelectedIndex());
CByear.setSelectedIndex(CByear.getSelectedIndex() - 1);
CBmonth.setSelectedIndex(tempMonth);
updateJPanel(tempMonth);
} else {
CBmonth.setSelectedIndex(CBmonth.getSelectedIndex() - 1);
updateJPanel(tempMonth);
}
}
public void monthDown_actionPerformed() {
int tempMonth;
tempMonth = 1 + CBmonth.getSelectedIndex();
if (tempMonth == 12) {
tempMonth = 0;
if (CByear.getSelectedIndex() == 9998) {
JOptionPane.showMessageDialog(this, "超出查找范围", "警告对话框",
JOptionPane.WARNING_MESSAGE);
return;
}
temp.set(Calendar.YEAR, CByear.getSelectedIndex() + 2);
CByear.setSelectedIndex(CByear.getSelectedIndex() + 1);
CBmonth.setSelectedIndex(tempMonth);
updateJPanel(tempMonth);
} else {
CBmonth.setSelectedIndex(CBmonth.getSelectedIndex() + 1);
updateJPanel(tempMonth);
}
}
public void currentDate_actionPerformed() {
updateJPanel(Calendar.getInstance().get(Calendar.MONTH));
CByear.setSelectedIndex(Calendar.getInstance().get(Calendar.YEAR) - 1);
CBmonth.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
}
public void paint() {
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st = "时间:";
if (nowh < 10)
st += "0" + nowh;
else
st += "" + nowh;
if (nowm < 10)
st += ":0" + nowm;
else
st += ":" + nowm;
if (nows < 10)
st += ":0" + nows;
else
st += ":" + nows;
Ltime.setFont(new Font("黑体", Font.PLAIN, 20));
Ltime.setForeground(Color.BLUE);
Ltime.setText(st);
}
private void showYearAndMonth() {
Lyear = new JLabel("年");
Lmonth = new JLabel("月");
Lcalen = new JLabel("公历");
Lyear.setForeground(Color.WHITE);
Lmonth.setForeground(Color.WHITE);
Lcalen.setForeground(Color.WHITE);
Lyear.setHorizontalAlignment(SwingConstants.CENTER);
Lmonth.setHorizontalAlignment(SwingConstants.CENTER);
Lcalen.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 1; i <= 9999; i++)
year[i - 1] = Integer.toString(i);
CByear = new JComboBox(year);
CByear.setSelectedIndex(Calendar.getInstance().get(Calendar.YEAR) - 1);
CBmonth = new JComboBox(month);
CBmonth.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
queryJPanel.setBackground(Color.PINK);
queryJPanel.setLayout(new FlowLayout());
queryJPanel.add(Lcalen);
queryJPanel.add(CByear);
queryJPanel.add(Lyear);
queryJPanel.add(CBmonth);
queryJPanel.add(Lmonth);
}
private void weekDatePane() {
weekDateJPanel.setLayout(new GridLayout(1, 7));
weekDateJPanel.setBackground(Color.LIGHT_GRAY);
String dayOfWeek[] = { "日", "一", "二", "三", "四", "五", "六" };
JLabel day;
for (int i = 0; i < 7; i++) {
day = new JLabel(dayOfWeek[i]);
day.setHorizontalAlignment(JLabel.CENTER);
day.setFont(new Font("黑体", Font.PLAIN, 20));
if (i == 0)
day.setForeground(Color.RED);
else if (i == 6)
day.setForeground(Color.GREEN);
else
day.setForeground(Color