package cn.edu.csu;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
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.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ClockView extends JFrame{
private JPanel mainPanel = null;
private JPanel childPanel1 = null;
private JPanel childPanel2 = null;
private JPanel childPanel3 = null;
private JComboBox jComboBox = null;
private JTextField jTextField = null;
private JTextField text_Show_Time = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
private JLabel jLabel6 = null;
private JLabel jLabel7 = null;
private JLabel[] jLabel_date = new JLabel[42];
private JLabel Label_clock = null;
private JButton Button_Up = null;
private JButton Button_Down = null;
private JButton refresh_Button = null;
public ClockView(){
super();
initialize();
}
/**
* 添加下拉框事件
* @return
*/
private JComboBox getJComboBox(){
if(jComboBox == null){
jComboBox = new JComboBox();
jComboBox.setBounds(new java.awt.Rectangle(10, 8, 90, 25));
for(int i = 1; i <= 12; i++){
jComboBox.addItem(String.valueOf(i));
}
Calendar cl = Calendar.getInstance();
// int curr_month = cl.get(cl.MONTH) + 1;
jComboBox.setSelectedIndex(cl.get(cl.MONTH));
jComboBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
repainTheClock();
}
});
}
return jComboBox;
}
/**
* 添加回车事件
* @return
*/
private JTextField getJTextField(){
if(jTextField == null){
jTextField = new JTextField();
jTextField.setBounds(new java.awt.Rectangle(100, 8, 85, 25));
jTextField.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText().trim());
repainTheClock();
}
});
}
return jTextField;
}
private JTextField getTextTime(){
if(text_Show_Time == null){
text_Show_Time = new JTextField();
text_Show_Time.setBounds(new java.awt.Rectangle(241,201,153,24));
}
return text_Show_Time;
}
/**
* 添加上下钮事件
* @return
*/
private JButton getButton_Up() {
if (Button_Up == null) {
Button_Up = new JButton();
Button_Up.setBounds(new java.awt.Rectangle(184,8,23,13));
Button_Up.setText("");
ImageIcon up = new ImageIcon("icon/up.jpg", "year up");
Button_Up.setIcon(up);
Button_Up.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int text = Integer.parseInt(jTextField.getText().trim());
jTextField.setText(String.valueOf(text + 1));
repainTheClock();
}
});
}
return Button_Up;
}
private JButton getButton_Down() {
if (Button_Down == null) {
Button_Down = new JButton();
Button_Down.setBounds(new java.awt.Rectangle(184,20,23,13));
ImageIcon down = new ImageIcon("icon/down.jpg", "year down");
Button_Down.setIcon(down);
Button_Down.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int text = Integer.parseInt(jTextField.getText().trim());
jTextField.setText(String.valueOf(text - 1));
repainTheClock();
}
});
}
return Button_Down;
}
private JButton getRefresh_Button() {
if (refresh_Button == null) {
refresh_Button = new JButton();
refresh_Button.setBounds(new java.awt.Rectangle(270,230,85,26));
refresh_Button.setText("刷新");
refresh_Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
repainTheClock();
}
});
}
return refresh_Button;
}
private JPanel getChildPanel1(){
if(childPanel1 == null){
childPanel1 = new JPanel();
childPanel1.setBounds(new java.awt.Rectangle(10,40,206,20));
jLabel1 = new JLabel(" 日");
jLabel2 = new JLabel(" 一");
jLabel3 = new JLabel(" 二");
jLabel4 = new JLabel(" 三");
jLabel5 = new JLabel(" 四");
jLabel6 = new JLabel(" 五");
jLabel7 = new JLabel(" 六");
GridLayout cl_cp1 = new GridLayout(1, 7);
childPanel1.setLayout(cl_cp1);
childPanel1.add(jLabel1, null);
childPanel1.add(jLabel2, null);
childPanel1.add(jLabel3, null);
childPanel1.add(jLabel4, null);
childPanel1.add(jLabel5, null);
childPanel1.add(jLabel6, null);
childPanel1.add(jLabel7, null);
}
return childPanel1;
}
private JPanel getChildPanel2(){
if(childPanel2 == null){
childPanel2 = new JPanel();
childPanel2.setBounds(new java.awt.Rectangle(0,0,240,260));
}
// childPanel2.setName("日期");
childPanel2.setLayout(null);
childPanel2.add(getJComboBox(), null);
childPanel2.add(getJTextField(), null);
// childPanel2.add(getJScrollBar(), null);
childPanel2.add(getChildPanel1(),null);
childPanel2.add(getChildPanel3(), null);
childPanel2.add(getButton_Up(), null);
childPanel2.add(getButton_Down(), null);
return childPanel2;
}
private JPanel getMainPanel(){
if(mainPanel == null){
Label_clock = new JLabel();
Label_clock.setBounds(new java.awt.Rectangle(241,10,152,181));
Label_clock.setText("");
mainPanel = new JPanel();
mainPanel.setBounds(new java.awt.Rectangle(0,0,400,200));
// FlowLayout fl_mp = new FlowLayout();
// mainPanel.setLayout(fl_mp);
mainPanel.setLayout(null);
mainPanel.add(getChildPanel1(), null);
mainPanel.add(getChildPanel2(), null);
ImageIcon clockIcon = new ImageIcon("icon/clock1.jpg", "about clock");
Label_clock.setIcon(clockIcon);
mainPanel.add(Label_clock, null);
mainPanel.add(getTextTime(), null);
mainPanel.add(getRefresh_Button(), null);
}
return mainPanel;
}
private JPanel getChildPanel3(){
if(childPanel3 == null){
childPanel3 = new JPanel();
childPanel3.setBounds(new java.awt.Rectangle(14,63,204,177));
GridLayout gl = new GridLayout(6, 7);
childPanel3.setLayout(gl);
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++){
int k = i * 7 + j;
jLabel_date[k] = new JLabel();
childPanel3.add(jLabel_date[k]);
}
}
}
return childPanel3;
}
public void repainTheClock(){
text_Show_Time.setText("");
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++){
jLabel_date[i * 7 + j].setText("");
jLabel_date[i* 7 + j].setForeground(new Color(0, 255, 255));
jLabel_date[i * 7 + j].setBackground(new java.awt.Color(255, 255, 255));
}
}
int year = Integer.parseInt(jTextField.getText().trim());
int month = jComboBox.getSelectedIndex();
Calendar cl = Calendar.getInstance();
int the_day = cl.get(cl.DAY_OF_MONTH);
int whole_day = cl.getActualMaximum(cl.DAY_OF_MONTH);
cl.set(year, month, 1);
int day_of_week = cl.get(cl.DAY_OF_WEEK);
int m = 1;
for(int i = 0; i < 42; i++){
if(i >= day_of_week - 1 && m <= whole_day){
jLabel_date[i].setText(String.valueOf(m));
if(m == the_day){
jLabel_date[i].setForeground(new java.awt.Color(153, 204, 0));
jLabel_date[i].setBackground(new java.awt.Color(51, 51, 255));
}
m++;
}
}
String text = year + "年" + (month + 1) + "月" + the_day + "日" + " " +
cl.get(cl.HOUR_OF_DAY) + ":" + cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND) ;
text_Show_Time.setText(text);
}
private void centerWindow(Window w) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
w.setBounds((int)(screenSize.getWidth() - windowSize.getWidth()) / 2,
(int)(screenSize.getHeight() - windowSize.getHeight()) / 2,
(int)windowSize.getWidth(), (int)windowSize.getHeight());
}
private void initialize(){
this.setSize(400, 300);
this.setContentPane(getMainPanel());
this.setTitle("日历控件");
this.centerWindow(this);
jTextField.setText(String.valueOf(2007));
this.repainTheClock();
}
public static void main(String[] args){
new ClockView().setVisible(true);
}
/**
* This method initializes refresh_Button
*
* @return javax.swing.JButton
*/
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
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.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ClockView extends JFrame{
private JPanel mainPanel = null;
private JPanel childPanel1 = null;
private JPanel childPanel2 = null;
private JPanel childPanel3 = null;
private JComboBox jComboBox = null;
private JTextField jTextField = null;
private JTextField text_Show_Time = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
private JLabel jLabel4 = null;
private JLabel jLabel5 = null;
private JLabel jLabel6 = null;
private JLabel jLabel7 = null;
private JLabel[] jLabel_date = new JLabel[42];
private JLabel Label_clock = null;
private JButton Button_Up = null;
private JButton Button_Down = null;
private JButton refresh_Button = null;
public ClockView(){
super();
initialize();
}
/**
* 添加下拉框事件
* @return
*/
private JComboBox getJComboBox(){
if(jComboBox == null){
jComboBox = new JComboBox();
jComboBox.setBounds(new java.awt.Rectangle(10, 8, 90, 25));
for(int i = 1; i <= 12; i++){
jComboBox.addItem(String.valueOf(i));
}
Calendar cl = Calendar.getInstance();
// int curr_month = cl.get(cl.MONTH) + 1;
jComboBox.setSelectedIndex(cl.get(cl.MONTH));
jComboBox.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
repainTheClock();
}
});
}
return jComboBox;
}
/**
* 添加回车事件
* @return
*/
private JTextField getJTextField(){
if(jTextField == null){
jTextField = new JTextField();
jTextField.setBounds(new java.awt.Rectangle(100, 8, 85, 25));
jTextField.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextField.setText(jTextField.getText().trim());
repainTheClock();
}
});
}
return jTextField;
}
private JTextField getTextTime(){
if(text_Show_Time == null){
text_Show_Time = new JTextField();
text_Show_Time.setBounds(new java.awt.Rectangle(241,201,153,24));
}
return text_Show_Time;
}
/**
* 添加上下钮事件
* @return
*/
private JButton getButton_Up() {
if (Button_Up == null) {
Button_Up = new JButton();
Button_Up.setBounds(new java.awt.Rectangle(184,8,23,13));
Button_Up.setText("");
ImageIcon up = new ImageIcon("icon/up.jpg", "year up");
Button_Up.setIcon(up);
Button_Up.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int text = Integer.parseInt(jTextField.getText().trim());
jTextField.setText(String.valueOf(text + 1));
repainTheClock();
}
});
}
return Button_Up;
}
private JButton getButton_Down() {
if (Button_Down == null) {
Button_Down = new JButton();
Button_Down.setBounds(new java.awt.Rectangle(184,20,23,13));
ImageIcon down = new ImageIcon("icon/down.jpg", "year down");
Button_Down.setIcon(down);
Button_Down.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int text = Integer.parseInt(jTextField.getText().trim());
jTextField.setText(String.valueOf(text - 1));
repainTheClock();
}
});
}
return Button_Down;
}
private JButton getRefresh_Button() {
if (refresh_Button == null) {
refresh_Button = new JButton();
refresh_Button.setBounds(new java.awt.Rectangle(270,230,85,26));
refresh_Button.setText("刷新");
refresh_Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
repainTheClock();
}
});
}
return refresh_Button;
}
private JPanel getChildPanel1(){
if(childPanel1 == null){
childPanel1 = new JPanel();
childPanel1.setBounds(new java.awt.Rectangle(10,40,206,20));
jLabel1 = new JLabel(" 日");
jLabel2 = new JLabel(" 一");
jLabel3 = new JLabel(" 二");
jLabel4 = new JLabel(" 三");
jLabel5 = new JLabel(" 四");
jLabel6 = new JLabel(" 五");
jLabel7 = new JLabel(" 六");
GridLayout cl_cp1 = new GridLayout(1, 7);
childPanel1.setLayout(cl_cp1);
childPanel1.add(jLabel1, null);
childPanel1.add(jLabel2, null);
childPanel1.add(jLabel3, null);
childPanel1.add(jLabel4, null);
childPanel1.add(jLabel5, null);
childPanel1.add(jLabel6, null);
childPanel1.add(jLabel7, null);
}
return childPanel1;
}
private JPanel getChildPanel2(){
if(childPanel2 == null){
childPanel2 = new JPanel();
childPanel2.setBounds(new java.awt.Rectangle(0,0,240,260));
}
// childPanel2.setName("日期");
childPanel2.setLayout(null);
childPanel2.add(getJComboBox(), null);
childPanel2.add(getJTextField(), null);
// childPanel2.add(getJScrollBar(), null);
childPanel2.add(getChildPanel1(),null);
childPanel2.add(getChildPanel3(), null);
childPanel2.add(getButton_Up(), null);
childPanel2.add(getButton_Down(), null);
return childPanel2;
}
private JPanel getMainPanel(){
if(mainPanel == null){
Label_clock = new JLabel();
Label_clock.setBounds(new java.awt.Rectangle(241,10,152,181));
Label_clock.setText("");
mainPanel = new JPanel();
mainPanel.setBounds(new java.awt.Rectangle(0,0,400,200));
// FlowLayout fl_mp = new FlowLayout();
// mainPanel.setLayout(fl_mp);
mainPanel.setLayout(null);
mainPanel.add(getChildPanel1(), null);
mainPanel.add(getChildPanel2(), null);
ImageIcon clockIcon = new ImageIcon("icon/clock1.jpg", "about clock");
Label_clock.setIcon(clockIcon);
mainPanel.add(Label_clock, null);
mainPanel.add(getTextTime(), null);
mainPanel.add(getRefresh_Button(), null);
}
return mainPanel;
}
private JPanel getChildPanel3(){
if(childPanel3 == null){
childPanel3 = new JPanel();
childPanel3.setBounds(new java.awt.Rectangle(14,63,204,177));
GridLayout gl = new GridLayout(6, 7);
childPanel3.setLayout(gl);
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++){
int k = i * 7 + j;
jLabel_date[k] = new JLabel();
childPanel3.add(jLabel_date[k]);
}
}
}
return childPanel3;
}
public void repainTheClock(){
text_Show_Time.setText("");
for(int i = 0; i < 6; i++){
for(int j = 0; j < 7; j++){
jLabel_date[i * 7 + j].setText("");
jLabel_date[i* 7 + j].setForeground(new Color(0, 255, 255));
jLabel_date[i * 7 + j].setBackground(new java.awt.Color(255, 255, 255));
}
}
int year = Integer.parseInt(jTextField.getText().trim());
int month = jComboBox.getSelectedIndex();
Calendar cl = Calendar.getInstance();
int the_day = cl.get(cl.DAY_OF_MONTH);
int whole_day = cl.getActualMaximum(cl.DAY_OF_MONTH);
cl.set(year, month, 1);
int day_of_week = cl.get(cl.DAY_OF_WEEK);
int m = 1;
for(int i = 0; i < 42; i++){
if(i >= day_of_week - 1 && m <= whole_day){
jLabel_date[i].setText(String.valueOf(m));
if(m == the_day){
jLabel_date[i].setForeground(new java.awt.Color(153, 204, 0));
jLabel_date[i].setBackground(new java.awt.Color(51, 51, 255));
}
m++;
}
}
String text = year + "年" + (month + 1) + "月" + the_day + "日" + " " +
cl.get(cl.HOUR_OF_DAY) + ":" + cl.get(cl.MINUTE) + ":" + cl.get(cl.SECOND) ;
text_Show_Time.setText(text);
}
private void centerWindow(Window w) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
w.setBounds((int)(screenSize.getWidth() - windowSize.getWidth()) / 2,
(int)(screenSize.getHeight() - windowSize.getHeight()) / 2,
(int)windowSize.getWidth(), (int)windowSize.getHeight());
}
private void initialize(){
this.setSize(400, 300);
this.setContentPane(getMainPanel());
this.setTitle("日历控件");
this.centerWindow(this);
jTextField.setText(String.valueOf(2007));
this.repainTheClock();
}
public static void main(String[] args){
new ClockView().setVisible(true);
}
/**
* This method initializes refresh_Button
*
* @return javax.swing.JButton
*/
}