MyDateChooserButton

package com.java2016.joy;
/*这是弹出日历面板,以供选择日期。
* */
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class myDateDialog extends JDialog implements ActionListener
, MouseListener , WindowListener {//继承JDialog类 实现ActionListener,MouseListener,WindowListener接口
JButton[] myDateButton = new JButton[42];//初始化日期按钮
String toOutString = null;//初始化对外输出字符,为日期转换文本字符
JButton lastMonth = new JButton("<");//上一月按钮,单击翻到上一月
JButton nextMonth = new JButton(">");//下一月按钮,单击翻到下一月
JLabel jlDate = new JLabel("riqi ");//日期显示标签
Date nowDate = new Date();//初始化一个时间
Calendar cald = Calendar.getInstance();//初始化一个日期
String[] weekDay = { "日", "一", "二", "三", "四", "五", "六" };//星期表头文本数组
private String stringToFather = null;//文本传递
private boolean isOpening = false;//标识是否打开
private MyDateChooserButton father1 = null;//引用MyDateChooserButton

public boolean isOpening() {//isOpening标识的set get
return isOpening;
}

public void setOpening(boolean isOpening) {
this.isOpening = isOpening;
}

public myDateDialog(MyDateChooserButton mdb) {//构造函数

this.father1 = mdb;//引用MyDateChooserButton
this.initialComponent();//初始化控件函数
this.initialDialog();//初始化窗口函数
this.addListener();//添加监听函数

}

private void initialComponent() {//初始化控件方法

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//引用一个日期转换文本格式
this.jlDate.setText(sdf.format(this.nowDate).trim());//为日期显示标签初始化数据

Calendar cald1 = Calendar.getInstance();//获得一个日期

try {
cald1.setTime(sdf.parse(this.jlDate.getText().trim()));//获得日期显示标签的内容为工作日期
this.initialDatePanel(cald1);//初始化日历日期面板
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private void initialDatePanel(Calendar cald1) {//初始化日历日期面板
for (int i = 0; i < 42; i++) {//42个日期显示的按钮,设置为空
this.myDateButton[i] = null;
}

// TODO Auto-generated method stub
for (int i = 0; i < 42; i++) {//为42个日期按钮重新初始化,

int m = 7;//设置一周常量7天
JButton dateB = new JButton();//初始化按钮
this.myDateButton[i] = dateB;//将初始化的按钮添加到数组当中
this.myDateButton[i].setBounds(5 + (i % 7) * 41,//设置按钮显示的位置,和大小
35 + (1 + i / 7) * 30, 40, 30);
this.myDateButton[i].setMargin(new Insets(0, 0, 0, 0));//设置按钮的边际大小为0
this.add(myDateButton[i]);

}

cald1.set(Calendar.DATE, 1);//将工作日期设置为当月1日

SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy");//获得一个新的日期显示文本格式,为日历年
Calendar cald2 = Calendar.getInstance();//新获得一个日期

int dateOfFirstDay = 0;//引用一个整数显示1日之前的星期天数
int year = 0;//引用一个整数显示为年份
String strY = sdf3.format(cald1.getTime());//获得cald1的年份
year = Integer.parseInt(strY);//将年份转换为整数

int dateOfThisMonth = 0;//引用一个整数显示本月天数
int dateOfLastMonth = 0;//引用一个整数显示上月天数

switch (this.monthToInt(cald1.getTime())) {//根据现在工作日期获得本月和上一月天数
case 1:
dateOfThisMonth = 31;
dateOfLastMonth = 31;
break;

case 2:

{
dateOfLastMonth = 31;
if (year % 100 == 0 && year % 4 != 0 || year % 400 == 0) {
dateOfThisMonth = 29;

break ;
} else {
dateOfThisMonth = 28;
break ;
}
}

case 3: {
dateOfThisMonth = 31;

if (year % 100 == 0 && year % 4 != 0 || year % 400 == 0) {
dateOfLastMonth = 29;
break ;
} else {
dateOfLastMonth = 28;
break ;
}

}

case 4:
dateOfLastMonth = 31;
dateOfThisMonth = 30;
break;
case 5:
dateOfLastMonth = 30;
dateOfThisMonth = 31;

break;
case 6:
dateOfLastMonth = 31;
dateOfThisMonth = 30;

break;
case 7:
dateOfLastMonth = 30;
dateOfThisMonth = 31;

break;
case 8:
dateOfLastMonth = 31;
dateOfThisMonth = 31;
break;
case 9:
dateOfLastMonth = 31;
dateOfThisMonth = 30;
break;
case 10:
dateOfLastMonth = 30;
dateOfThisMonth = 31;

break;
case 11:
dateOfLastMonth = 31;
dateOfThisMonth = 30;

break;
case 12:
dateOfLastMonth = 30;
dateOfThisMonth = 31;
break;
}

switch (this.weekToInt(cald1.getTime())) {//根据工作时间获得本月一日是星期几,并获得本月1日在日历中的排序位置
case 7:
dateOfFirstDay = 0;
break;
case 1:
dateOfFirstDay = 1;
break;
case 2:
dateOfFirstDay = 2;
break;
case 3:
dateOfFirstDay = 3;
break;
case 4:
dateOfFirstDay = 4;
break;
case 5:
dateOfFirstDay = 5;
break;
case 6:
dateOfFirstDay = 6;
break;
}

for (int i =dateOfFirstDay-1 ; i >=0; i--) {//本月1日之前的上月日期按钮的标题的设置
this.myDateButton[i].setText(Integer.toString(dateOfLastMonth
+i-dateOfFirstDay+1));
this.myDateButton[i].setEnabled(false);//设置上月按钮不可用
}
for (int i = 0; i < 7 - dateOfFirstDay; i++) {//第一行本月日期按钮标题的设置
this.myDateButton[i + dateOfFirstDay].setText(Integer
.toString(i + 1));
}
for (int i = 7; i < dateOfThisMonth + dateOfFirstDay; i++) {//之后的本月日期按钮标题的设置

this.myDateButton[i].setText(Integer.toString(i - dateOfFirstDay
+ 1));

}
for (int i = dateOfThisMonth + dateOfFirstDay; i < 42; i++) {//本月之后的下一月日期按钮标题的设置

this.myDateButton[i].setText(Integer.toString(1 - (dateOfFirstDay
+ dateOfThisMonth - i)));
this.myDateButton[i].setEnabled(false);//设置下月标题按钮不可用
}

}
public int monthToInt(Date date){//根据工作时间获得月份并转换为整数形式
SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
String str = null ;
int i = 0 ;
str = sdf2.format(date);
if(str.equals("01")){
i = 1 ;

}else if
(str.equals("02")){
i = 2 ;

} else if
(str.equals("03")){
i = 3 ;

}

else if
(str.equals("04")){
i = 4 ;

}

else if
(str.equals("05")){
i = 5;

}
else if
(str.equals("06")){
i = 6 ;

}

else if
(str.equals("07")){
i = 7 ;

}else if
(str.equals("08")){
i = 8;

}
else if
(str.equals("09")){
i = 9 ;

}

else if
(str.equals("10")){
i = 10 ;

}else if
(str.equals("11")){
i = 11;

}
else if
(str.equals("12")){
i = 12 ;

}
return i;}

public int weekToInt(Date date){//根据工作时间获得星期并转换成整数形式
SimpleDateFormat sdf1 = new SimpleDateFormat("E");
String str = null ;
int i = 0 ;
str = sdf1.format(date);
if(str.equals("星期日")){
i = 7 ;

}else if
(str.equals("星期一")){
i = 1 ;

} else if
(str.equals("星期二")){
i = 2 ;

}

else if
(str.equals("星期三")){
i = 3 ;

}

else if
(str.equals("星期四")){
i = 4;

}
else if
(str.equals("星期五")){
i = 5 ;

}

else if
(str.equals("星期六")){
i = 6 ;

}

return i;}
private void addListener() {//添加监听方法
// TODO Auto-generated method stub
this.lastMonth.addActionListener(this);//为上月按钮添加监听
this.nextMonth.addActionListener(this);//为下月按钮添加监听
for (int i = 0; i < 42; i++) {//为日期按钮添加监听
this.myDateButton[i].addMouseListener(this);
}
}

private void initialDialog() {//初始化窗口函数
// TODO Auto-generated method stub
this.setLayout(null);//设置窗口为空布局
this.jlDate.setBounds(100, 5, 150, 20);//设置日期显示标签的位置和大小
this.lastMonth.setBounds(5, 5, 47, 30);//设置上月按钮的位置和大小
this.nextMonth.setBounds(245, 5, 47, 30);//设置下月按钮的位置和大小
this.add(lastMonth);//添加上月按钮
this.add(nextMonth);//添加下月按钮
this.add(this.jlDate);//添加日期显示标签
this.setSize(305, 300);//设置面板大小
this.setTitle("日期选择框");//设置面板标题
for (int i = 0; i < weekDay.length; i++) {//初始化星期文本显示标题
JLabel week1 = new JLabel(weekDay[i], JLabel.CENTER);//添加标签内容在标签中间显示
week1.setOpaque(false);//防锯齿显示
week1.setBackground(Color.getHSBColor(101, 102, 204));//设置标签的背景色
week1.setForeground(Color.RED);//设置标签的前景色
week1.setBounds(5 + i * 41, 35, 40, 30);//设置大小和位置
this.add(week1);//添加到面板
}

this.addWindowListener(this);//为窗口添加窗口监听

}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
for (int i = 0; i < 42; i++) {//监听日期按钮鼠标点击事件
if (e.getSource().equals(this.myDateButton[i])) {
toOutString = this.myDateButton[i].getText().trim();//获得输出文本的内容
}

}
if (toOutString == null) {//如果没有获得文本 设置文本为“1”
toOutString = "1";
}
Calendar caldOut = Calendar.getInstance();//引用一个新的日期

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//引用一个日期字符格式

try {
caldOut.setTime(sdf.parse(this.jlDate.getText().trim()));//设置新的日期为标签显示的日期

caldOut.set(Calendar.DATE, Integer.parseInt(toOutString));//设置日期为点击获得新的日期

this.jlDate.setText(sdf.format(caldOut.getTime()));//设置日期显示标签为刚获得日期
stringToFather = sdf.format(caldOut.getTime());//对外输出的文版为刚获得日期
this.windowClosing(null);//关闭窗口
this.setVisible(false);//窗口设置为不可显示
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {//窗口监听函数
// TODO Auto-generated method stub
if (e.getSource().equals(this.lastMonth)) {//上月按钮按钮点击
this.lastMonth_event();

} else if (e.getSource().equals(this.nextMonth)) {//下月按钮点击
this.nextMonth_event();
}
}

private void nextMonth_event() {//下月按钮点击时间
for (int i = 0; i < 42; i++) {
this.remove(myDateButton[i]);//移除日期按钮
}
// TODO Auto-generated method stub
this.repaint();//重绘

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//获得一个引用时间转换字符
try {
Date date1 = sdf.parse(this.jlDate.getText().trim());//获得日期显示标签的时间
this.cald.setTime(date1);//
this.cald.add(Calendar.MONTH, 1);
String strDate = sdf.format(this.cald.getTime()).trim();
this.jlDate.setText(strDate);
this.initialDatePanel(cald);
for (int i = 0; i < 42; i++) {
this.myDateButton[i].addMouseListener(this);
}
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

private void lastMonth_event() {
// TODO Auto-generated method stub

for (int i = 0; i < 42; i++) {

this.remove(myDateButton[i]);

}
this.repaint();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
Calendar cald1 = Calendar.getInstance();
try {
Date date1 = sdf.parse(this.jlDate.getText().trim());
cald1.setTime(date1);
cald1.add(Calendar.MONTH, -1);
String strDate = sdf.format(cald1.getTime()).trim();
this.jlDate.setText(strDate);
this.initialDatePanel(cald1);
for (int i = 0; i < 42; i++) {
this.myDateButton[i].addMouseListener(this);
}
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub

this.father1.isOpening = false;
if (this.stringToFather != null) {
this.father1.setText(this.stringToFather);
}
}

@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public String getStringToFather() {
return stringToFather;
}

public void setStringToFather(String stringToFather) {
this.stringToFather = stringToFather;
}

}

----------------

package com.java2016.joy;
/*这个项目是实现一个日期选择按钮
* 1,点击按钮可以弹出一个月历面板的Dialog,可以选择要使用的日期
* 2,点击月历面板上的日期,月历面板退出,按钮上显示选择后的日期
* */
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class MyDateChooserButton extends JButton implements MouseListener{
//private Management father;

boolean isOpening = false;

public MyDateChooserButton() {

this.initial_button();
this.addListener();
}

private void addListener() {
// TODO Auto-generated method stub
this.getAction();
}

private void initial_button() {
// TODO Auto-generated method stub
Date nowDay = new Date();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
this.setText(sdf2.format(nowDay));
this.addMouseListener(this);

}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (isOpening == false && e.getSource().equals(this)) {
myDateDialog mdd = new myDateDialog(this);
mdd.setLocation(e.getX() + this.getX() + 280,
e.getY() + this.getY() + 120);
isOpening = true;
mdd.setVisible(true);
mdd.setAlwaysOnTop(true);

}
}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

}

//-------

package com.java2016.joy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test extends JFrame {
JPanel jp ;
MyDateChooserButton mdcb ;
public Test(){

jp = new JPanel() ;
mdcb = new MyDateChooserButton() ;
jp.add(mdcb);
this.add(jp);

this.setTitle("测试日期选择按钮");
this.setSize(300,200);
this.setLocation(60,50);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
public static void main(String[] args) {
Test test = new Test() ;

}
}

时间: 2024-10-25 14:52:02

MyDateChooserButton的相关文章