package com.ueky;
|
|
|
import javax.swing.*;
|
import javax.swing.border.Border;
|
import javax.swing.event.CaretEvent;
|
import javax.swing.event.CaretListener;
|
import javax.swing.event.DocumentEvent;
|
import javax.swing.event.DocumentListener;
|
import javax.swing.text.*;
|
import java.awt.*;
|
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseListener;
|
import java.text.DecimalFormat;
|
import java.text.NumberFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.List;
|
import java.util.*;
|
import java.util.function.Consumer;
|
|
/**
|
* @ClassName DateTimePicker
|
* @Author guokai2012
|
* @Date 2021/8/1 17:53
|
* @Description ʱ¼äÑ¡Ôñ²å¼þ
|
*/
|
|
public class Test2 {
|
/**
|
* ʱ¼äÑ¡Ôñ×é¼þ£¬¼´ÔÚÄĸö×é¼þÉÏ ²Ù×÷ ʱ¼äÑ¡Ôñ
|
*/
|
private JComponent component;
|
|
/**
|
* ʱ¼ä¸ñʽ£¬Ä¬ÈÏʱ 2021-08-01 17:20£¬Ã»ÓÐÃë
|
*/
|
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
private SimpleDateFormat sdfYm = new SimpleDateFormat("yyyyÄêMMÔÂ");
|
|
/**
|
* ÈÕÆÚ£¬Ä¬ÈÏÊǵ±Ç°Ê±¼ä
|
*/
|
private Date select = new Date();
|
|
/**
|
* ʱ¼ä Ñ¡Ôñ²å¼þ´óС
|
*/
|
private int width = 200;
|
|
private int height = 200;
|
|
/**
|
* µ×²ã ²Ëµ¥£¬Ôڸò˵¥ÉÏ »æÖƹ¦ÄÜ
|
*/
|
private JPopupMenu popup;
|
/**
|
* µ¯³ö¿ò£¬ÀûÓõ¯³ö¿ò£¬ÊµÏÖµã»÷ÆäËûλÖ㬹رÕʱ¼äÑ¡Ôñ
|
*/
|
private JDialog dialog;
|
/**
|
* ÊÇ·ñÆôÓÃ
|
*/
|
private boolean isEnable = true;
|
/**
|
* ÏÔʾ ÄêÔÂÃæ°å
|
*/
|
private TopPanel topPanel;
|
/**
|
* ÈÕÀú
|
*/
|
private final Calendar calendar = Calendar.getInstance();
|
/**
|
* ÓÃÓÚ¶Ô±ÈʹÓÃʱµÄÈÕÀú
|
*/
|
private final Calendar nowCalendar = Calendar.getInstance();
|
/**
|
* ÏÔʾ ÈÕÆÚ Ãæ°å
|
*/
|
private CenterPanel centerPanel;
|
/**
|
* ÏÔʾ ʱ¼äÃæ°å
|
*/
|
private BottomPanel bottomPanel;
|
/**
|
* Êó±ê ʼþ£¨±»°ó¶¨µÄ×é¼þ£©
|
*/
|
private MouseAdapter componentMostListener;
|
/**
|
* »Øµ÷º¯Êý
|
*/
|
private Consumer<Test2> callFun;
|
private boolean isTimeEnable = true;
|
|
/**
|
* ¹¹Ôì·½·¨
|
*/
|
public Test2() {
|
}
|
|
public Test2(String sdf) {
|
this.sdf = new SimpleDateFormat(sdf);
|
}
|
|
public Test2(String sdf, int w, int h) {
|
this.sdf = new SimpleDateFormat(sdf);
|
this.width = w;
|
this.height = h;
|
}
|
|
/**
|
* textField ×¢²á²å¼þ
|
*/
|
public void register(JTextField textField) {
|
this.component = textField;
|
initDateTimePicker();
|
}
|
|
/**
|
* Label ×¢²á²å¼þ
|
*/
|
public void register(JLabel label) {
|
this.component = label;
|
initDateTimePicker();
|
}
|
|
/**
|
* »ñȡѡÖеÄʱ¼ä
|
*/
|
public String getSelect() {
|
return this.sdf.format(select);
|
}
|
|
/**
|
* ÉèÖà ĬÈÏÑ¡ÖеÄʱ¼ä
|
*/
|
public Test2 setSelect(Date select) {
|
this.select = select;
|
this.calendar.setTime(this.select);
|
return this;
|
}
|
|
/**
|
* »ñÈ¡ÊÇ·ñÆôÓÃ
|
*/
|
public boolean isEnable() {
|
return isEnable;
|
}
|
|
/**
|
* ÉèÖÃÆôÓ㬲»ÆôÓÃ
|
*/
|
public Test2 setEnable(boolean enable) {
|
isEnable = enable;
|
reInitDateTimePicker();
|
return this;
|
}
|
|
/**
|
* »ñÈ¡ ÊÇ·ñÆôÓà ʱ¼äÊäÈë¿ò
|
*/
|
public boolean isTimeEnable() {
|
return isTimeEnable;
|
}
|
|
/**
|
* ÉèÖà ÊÇ·ñÆôÓÃʱ¼ä ÊäÈë¿ò
|
*/
|
public Test2 setTimeEnable(boolean timeEnable) {
|
isTimeEnable = timeEnable;
|
reInitDateTimePicker();
|
return this;
|
}
|
|
/**
|
* Ñ¡ÖкóµÄ »Øµ÷º¯Êý
|
*/
|
public Test2 changeDateEvent(Consumer<Test2> consumer) {
|
this.callFun = consumer;
|
return this;
|
}
|
|
/**
|
* ³õʼ»¯ ×é¼þ
|
*/
|
private void initDateTimePicker() {
|
if (isEnable) {
|
this.componentMostListener = new MouseAdapter() {
|
// Êó±ê°´ÏÂ̧Æð
|
@Override
|
public void mouseReleased(MouseEvent e) {
|
showBasePop();
|
}
|
|
// Êó±ê»®Èë
|
@Override
|
public void mouseEntered(MouseEvent e) {
|
component.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
}
|
|
// Êó±ê»®³ö
|
@Override
|
public void mouseExited(MouseEvent e) {
|
component.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
}
|
};
|
this.component.addMouseListener(this.componentMostListener);
|
// ³õʼ»¯Ê±£¬½«Öµ¸³Öµµ½ °ó¶¨µÄ×é¼þ
|
commit();
|
initBasePop();
|
initTopPanel();
|
initCenterPanel();
|
if (isTimeEnable) {
|
initBottomPanel();
|
}
|
}
|
}
|
|
/**
|
* ÖØÐ³õʼ»¯×é¼þ
|
*/
|
private void reInitDateTimePicker() {
|
if (null != this.component) {
|
this.component.removeMouseListener(this.componentMostListener);
|
initDateTimePicker();
|
}
|
}
|
|
/**
|
* ³õʼ»¯ ÄêÔÂÏÔÊ¾Ãæ°å
|
*/
|
private void initTopPanel() {
|
// TODO ÕâÀï Òª×öÈë²ÎÅäÖÃ
|
this.topPanel = new TopPanel(true, true);
|
this.topPanel.setBackground(Color.RED);
|
this.popup.add(topPanel, BorderLayout.NORTH);
|
this.topPanel.updateDate();
|
}
|
|
/**
|
* ³õʼ»¯ ÈÕÆÚÏÔÊ¾Ãæ°å
|
*/
|
private void initCenterPanel() {
|
this.centerPanel = new CenterPanel(1);
|
this.centerPanel.setBackground(Color.YELLOW);
|
this.popup.add(centerPanel, BorderLayout.CENTER);
|
}
|
|
/**
|
* ³õʼ»¯ ʱ¼äÃæ°å
|
*/
|
private void initBottomPanel() {
|
this.bottomPanel = new BottomPanel();
|
this.bottomPanel.setBackground(Color.MAGENTA);
|
this.popup.add(bottomPanel, BorderLayout.SOUTH);
|
}
|
|
/**
|
* ÏÔʾ ²å¼þ
|
*/
|
private void showBasePop() {
|
// ÏÈÈ·¶¨ pointµã
|
Point point = new Point(0, this.component.getHeight());
|
// ½« pointµã£¬Ïà¶ÔÓë×é¼þ componentµÄµãת»¯³ÉÆÁÄ»µÄµã
|
SwingUtilities.convertPointToScreen(point, this.component);
|
this.dialog.setLocation(point);
|
this.dialog.setVisible(true);
|
// ½« popup ÏÔʾÔÚ dialogÉÏ¡£ÕâÑù¿ÉÒÔ ÔÚpopupʧȥ½¹µãʱ£¬×Ô¶¯¹Ø±Õ
|
this.popup.show(this.dialog, 0, 0);
|
}
|
|
/**
|
* ³õʼ»¯ µ×²ã ²Ëµ¥
|
*/
|
private void initBasePop() {
|
this.dialog = new JDialog();
|
this.dialog.setSize(0, 0);
|
// ÉèÖÃΪ ûÓÐ ¹Ø±Õ°´Å¥
|
this.dialog.setUndecorated(true);
|
|
// ´´½¨ µ¯³ö²Ëµ¥£¬²¢ÖØÐ´ ¼´½«Òþ²Ø·½·¨¡£
|
this.popup = new JPopupMenu() {
|
@Override
|
protected void firePopupMenuWillBecomeInvisible() {
|
// ½« dialogÒ²ÉèÖÃΪ ²»¿É¼û
|
dialog.setVisible(false);
|
}
|
};
|
this.popup.setPopupSize(new Dimension(this.width, this.height));
|
// TODO ÑÕÉ«
|
this.popup.setBackground(Color.RED);
|
this.popup.setBorder(BorderFactory.createEmptyBorder());
|
this.popup.setLayout(new BorderLayout());
|
}
|
|
/**
|
* ˢРÈÕÆÚ×é¼þ
|
*/
|
private void refresh() {
|
this.topPanel.updateDate();
|
this.centerPanel.updateDate();
|
SwingUtilities.updateComponentTreeUI(this.popup);
|
}
|
|
/**
|
* Ñ¡ÖÐÈÕÆÚ·½·¨
|
*/
|
private void selectDate(int year, int month, int day) {
|
Calendar tmp = Calendar.getInstance();
|
if (isTimeEnable && null != bottomPanel) {
|
int hour = Integer.parseInt(bottomPanel.hourText.getText());
|
int min = Integer.parseInt(bottomPanel.minText.getText());
|
tmp.set(year, month, day, hour, min);
|
} else {
|
tmp.set(year, month, day);
|
}
|
setSelect(tmp.getTime());
|
commit();
|
refresh();
|
}
|
|
/**
|
* È·ÈÏ Ê±¼äºú´¥·¢
|
*/
|
private void commit() {
|
if (this.component instanceof JTextField) {
|
((JTextField) this.component).setText(sdf.format(select));
|
}
|
if (this.component instanceof JLabel) {
|
((JLabel) this.component).setText(sdf.format(select));
|
}
|
if (null != this.callFun) {
|
this.callFun.accept(this);
|
}
|
if (null != this.popup) {
|
this.popup.setVisible(false);
|
}
|
}
|
|
/**
|
* ×îÉÏÃæµÄ Ãæ°å£¬ÏÔʾ ÄêÔÂÈÕ£¬²¢Ö§³ÖÄêÔÂ×óÓÒÒÆ¶¯µ÷Õû
|
*/
|
private class TopPanel extends JPanel {
|
private final JPanel leftP = new JPanel();
|
private final JPanel rightP = new JPanel();
|
// ÄêÑ¡Ôñ°´Å¥
|
private final boolean yearClick;
|
// Ô°´Å¥
|
private final boolean monthClick;
|
private JLabel yearL;
|
private JLabel yearR;
|
private final JLabel center;
|
private JLabel monthL;
|
private JLabel monthR;
|
|
private final MouseListener listener = new MouseAdapter() {
|
@Override
|
public void mouseReleased(MouseEvent e) {
|
// ¸ù¾Ý µã»÷ʼþ£¬ÏÔʾֵ
|
JLabel source = (JLabel) e.getSource();
|
switch (source.getName()) {
|
case "yearL":
|
lastYear();
|
break;
|
case "yearR":
|
nextYear();
|
break;
|
case "monthL":
|
lastMonth();
|
break;
|
case "monthR":
|
nextMonth();
|
break;
|
default:
|
break;
|
}
|
refresh();
|
}
|
|
@Override
|
public void mouseEntered(MouseEvent e) {
|
if (yearClick) {
|
yearL.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
yearR.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
}
|
if (monthClick) {
|
monthL.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
monthR.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
}
|
}
|
|
@Override
|
public void mouseExited(MouseEvent e) {
|
if (yearClick) {
|
yearL.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
yearR.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
}
|
if (monthClick) {
|
monthL.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
monthR.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
}
|
}
|
};
|
|
|
public TopPanel(boolean yearClick, boolean monthClick) {
|
this.yearClick = yearClick;
|
this.monthClick = monthClick;
|
setLayout(new BorderLayout());
|
this.center = new JLabel(sdfYm.format(select), JLabel.CENTER);
|
int hGap = ((width - 100) / 4) - 10;
|
this.leftP.setLayout(new FlowLayout(FlowLayout.LEFT, hGap, 5));
|
this.rightP.setLayout(new FlowLayout(FlowLayout.RIGHT, hGap, 5));
|
intTopPanel();
|
}
|
|
private void intTopPanel() {
|
if (yearClick) {
|
this.yearL = new JLabel("<<", JLabel.CENTER);
|
this.yearL.setToolTipText("ÉÏÒ»Äê");
|
this.yearL.setName("yearL");
|
this.yearL.addMouseListener(this.listener);
|
|
this.yearR = new JLabel(">>", JLabel.CENTER);
|
this.yearR.setToolTipText("ÏÂÒ»Äê");
|
this.yearR.setName("yearR");
|
this.yearR.addMouseListener(this.listener);
|
}
|
|
if (monthClick) {
|
this.monthL = new JLabel("<", JLabel.CENTER);
|
this.monthL.setToolTipText("ÉϸöÔÂ");
|
this.monthL.setName("monthL");
|
this.monthL.addMouseListener(this.listener);
|
|
this.monthR = new JLabel(">", JLabel.CENTER);
|
this.monthR.setToolTipText("ϸöÔÂ");
|
this.monthR.setName("monthR");
|
this.monthR.addMouseListener(this.listener);
|
}
|
|
if (yearClick && monthClick) {
|
this.leftP.add(this.yearL);
|
this.leftP.add(this.monthL);
|
this.rightP.add(this.monthR);
|
this.rightP.add(this.yearR);
|
this.leftP.setVisible(true);
|
this.rightP.setVisible(true);
|
} else if (yearClick) {
|
this.leftP.add(this.yearL);
|
this.rightP.add(this.yearR);
|
this.leftP.setVisible(true);
|
this.rightP.setVisible(true);
|
} else if (monthClick) {
|
this.leftP.add(this.monthL);
|
this.rightP.add(this.monthR);
|
this.leftP.setVisible(true);
|
this.rightP.setVisible(true);
|
} else {
|
this.leftP.setVisible(false);
|
this.rightP.setVisible(false);
|
}
|
|
add(this.leftP, BorderLayout.WEST);
|
add(this.center, BorderLayout.CENTER);
|
add(this.rightP, BorderLayout.EAST);
|
setVisible(true);
|
}
|
|
@Override
|
public void setBackground(Color bg) {
|
super.setBackground(bg);
|
if (null != this.leftP) {
|
this.leftP.setBackground(bg);
|
}
|
if (null != rightP) {
|
this.rightP.setBackground(bg);
|
}
|
}
|
|
// ÄêÔ ÉÏÏ··½·¨
|
private void nextMonth() {
|
calendar.add(Calendar.MONTH, 1);
|
}
|
|
private void lastMonth() {
|
calendar.add(Calendar.MONTH, -1);
|
}
|
|
private void nextYear() {
|
calendar.add(Calendar.YEAR, 1);
|
}
|
|
private void lastYear() {
|
calendar.add(Calendar.YEAR, -1);
|
}
|
|
// ¸üРÄêÔ ÏÔʾ
|
public void updateDate() {
|
this.center.setText(sdfYm.format(calendar.getTime()));
|
}
|
}
|
|
/**
|
* ÈÕÆÚ ²å¼þ£¬ÖмäÑ¡ÔñÈÕÆÚ¹¦ÄÜ
|
*/
|
private class CenterPanel extends JPanel {
|
// ĬÈÏ ÐÇÆÚÌ죬ΪһÖܵĵÚÒ»Ìì
|
private final int startWeek;
|
// ĬÈÏ ÐÇÆÚÈÕΪµÚÒ»Ì죬¼´0ϱê
|
private final String[] weekName = {"ÈÕ", "Ò»", "¶þ", "Èý", "ËÄ", "Îå", "Áù"};
|
private final List<MyLabel> myLabelList = new ArrayList<>();
|
|
public CenterPanel(int startWeek) {
|
this.startWeek = startWeek > 7 ? 6 : (Math.max(startWeek, 0));
|
setLayout(new GridLayout(7, 7, 5, 5));
|
initCenterPanel();
|
}
|
|
private void initCenterPanel() {
|
// ±í¸ñµÚÒ»ÐУ¬ÏÔʾ ÐÇÆÚ
|
// ÖØÇÐÊý×é
|
cutArrays();
|
for (String s : this.weekName) {
|
add(new JLabel(s, JLabel.CENTER));
|
}
|
updateDate();
|
setVisible(true);
|
}
|
|
// »æÖÆ JLable ÈÕÆÚ×é¼þ
|
private void updateDate() {
|
removeAllJLabel();
|
Date temp = calendar.getTime();
|
Calendar tmpCalendar = Calendar.getInstance();
|
tmpCalendar.setTime(temp);
|
tmpCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
int index = tmpCalendar.get(Calendar.DAY_OF_WEEK);
|
// ÏÈͨ¹ý ¼ÆË㣬½«ÐÇÆÚºÍÈÕÆÚÄܹ»ÔÚ±í¸ñÖÐ¶ÔÆä
|
int sum = (index == 1 ? 8 : index) - startWeek;
|
// ÔÙ´¦Àíһϣ¬¶ÔÓÚÌØÊâÖµµÄÎÊÌâ
|
if (sum == 1) {
|
sum = 8;
|
} else if (sum < 0) {
|
sum += 7;
|
} else if (sum == 0) {
|
sum = 7;
|
}
|
tmpCalendar.add(Calendar.DAY_OF_MONTH, -sum);
|
for (int i = 0; i < 42; i++) {
|
tmpCalendar.add(Calendar.DAY_OF_MONTH, 1);
|
MyLabel myLabel = new MyLabel(tmpCalendar.get(Calendar.YEAR),
|
tmpCalendar.get(Calendar.MONTH), tmpCalendar.get(Calendar.DAY_OF_MONTH));
|
myLabel.addMouseListener(new MouseAdapter() {
|
@Override
|
public void mouseReleased(MouseEvent e) {
|
MyLabel myLabel = (MyLabel) e.getSource();
|
selectDate(myLabel.year, myLabel.month, myLabel.day);
|
}
|
|
@Override
|
public void mouseEntered(MouseEvent e) {
|
myLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
myLabel.isHover = true;
|
}
|
|
@Override
|
public void mouseExited(MouseEvent e) {
|
myLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
|
myLabel.isHover = false;
|
}
|
});
|
myLabelList.add(myLabel);
|
add(myLabel);
|
}
|
}
|
|
// ɾ³ý ÈÕÆÚ JLable×é¼þ
|
private void removeAllJLabel() {
|
for (MyLabel myLabel : myLabelList) {
|
remove(myLabel);
|
}
|
}
|
|
|
@Override
|
public void setBackground(Color bg) {
|
super.setBackground(bg);
|
}
|
|
// ´¦ÀíÊý×飬°´ÕÕ Ö¸¶¨µÄ¿ªÊ¼ÐÇÆÚ´¦Àí
|
private void cutArrays() {
|
String[] e = Arrays.copyOfRange(this.weekName, this.startWeek, this.weekName.length);
|
String[] s = Arrays.copyOfRange(this.weekName, 0, this.startWeek);
|
System.arraycopy(e, 0, this.weekName, 0, e.length);
|
System.arraycopy(s, 0, this.weekName, e.length, s.length);
|
}
|
|
/**
|
* ×Ô¶¨Òå JLabel
|
*/
|
private class MyLabel extends JLabel {
|
private final int year;
|
private final int month;
|
private final int day;
|
private boolean opaque = false;
|
private Border currBorder = null;
|
public boolean isHover;
|
|
public MyLabel(int year, int month, int day) {
|
super("" + day, JLabel.CENTER);
|
this.year = year;
|
this.month = month;
|
this.day = day;
|
nowCalendar.set(year, month, day);
|
|
// µ±ÔµÄÌìÉèÖÃΪºÚÉ«£¬·ñÔòÉèÖÃΪ ÁÁ»ÒÉ«
|
if (this.month == calendar.get(Calendar.MONTH)) {
|
// ½«µ±Ô ÖÜÁù¡¢ÖÜÈÕ ±ê¼ÇΪºìÉ«
|
if (nowCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || nowCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
|
this.setForeground(Color.RED);
|
} else {
|
this.setForeground(Color.BLACK);
|
}
|
} else {
|
this.setForeground(Color.LIGHT_GRAY);
|
}
|
|
// ÉèÖÃÑ¡ÔñµÄÈÕÆÚ
|
nowCalendar.setTime(select);
|
if (this.day == nowCalendar.get(Calendar.DAY_OF_MONTH) && this.month ==
|
nowCalendar.get(Calendar.MONTH) && this.year == nowCalendar.get(Calendar.YEAR)) {
|
this.opaque = true;
|
setOpaque(this.opaque);
|
this.setBackground(Color.CYAN);
|
this.setForeground(Color.WHITE);
|
}
|
|
// ÉèÖõ±Ç°ÈÕÆÚ£¬¸ø³öºìÉ«±ß¿ò
|
// ÏȰÑÈÕÀúÖØÖûØÀ´
|
nowCalendar.setTime(new Date());
|
if (this.year == nowCalendar.get(Calendar.YEAR) && this.month == nowCalendar.get(Calendar.MONTH) &&
|
this.day == nowCalendar.get(Calendar.DAY_OF_MONTH)) {
|
this.currBorder = BorderFactory.createLineBorder(Color.RED);
|
this.setBorder(this.currBorder);
|
}
|
}
|
|
@Override
|
protected void paintComponent(Graphics g) {
|
// Èç¹û±»Ñ¡ÖÐÁ˾ͻ³öÒ»¸öÐéÏß¿ò³öÀ´
|
if (isHover) {
|
Stroke s = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
|
BasicStroke.JOIN_BEVEL, 1.0f,
|
new float[]{2.0f, 2.0f}, 1.0f);
|
Graphics2D gd = (Graphics2D) g;
|
gd.setStroke(s);
|
// ÉèÖÃÐéÏßÑÕÉ«
|
gd.setColor(Color.BLACK);
|
Polygon p = new Polygon();
|
p.addPoint(0, 0);
|
p.addPoint(getWidth() - 1, 0);
|
p.addPoint(getWidth() - 1, getHeight() - 1);
|
p.addPoint(0, getHeight() - 1);
|
gd.drawPolygon(p);
|
// Õë¶Ô Ñ¡ÖÐÈÕÆÚ & µ±Ç°ÈÕÆÚ ÒÑ´æÔڱ߿òµÄ£¬×öÌØÊâ´¦Àí
|
if (null != this.currBorder) {
|
this.setBorder(null);
|
}
|
if (this.opaque) {
|
this.setOpaque(false);
|
}
|
} else {
|
this.setBorder(this.currBorder);
|
this.setOpaque(this.opaque);
|
}
|
super.paintComponent(g);
|
this.repaint();
|
}
|
}
|
}
|
|
/**
|
* ×îµØÏ£¬ÉèÖÃʼþ
|
*/
|
private class BottomPanel extends JPanel {
|
private JTextField hourText;
|
private JTextField minText;
|
|
public BottomPanel() {
|
super();
|
initBottomPanel();
|
}
|
|
// ³õʼ»¯ ×é¼þ
|
private void initBottomPanel() {
|
setLayout(new FlowLayout(FlowLayout.CENTER));
|
int col = Math.floorDiv(((width - 70) >> 1), 11);
|
JLabel title = new JLabel("ʱ¼ä", JLabel.CENTER);
|
|
// СʱÎı¾¿ò
|
this.hourText = new JTextField(null, col);
|
// ÉèÖà СʱÏÔʾ
|
this.hourText.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
|
this.hourText.setHorizontalAlignment(SwingConstants.CENTER);
|
// ÉèÖà ×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ
|
MyDocument docHour = new MyDocument(23);
|
this.hourText.setDocument(docHour);
|
// ÉèÖúÃdocumentºó£¬²Å¿ÉÒÔÉèÖÃĬÈÏtext£¬·ñÔòÎÞ·¨ÏÔʾ
|
docHour.insertString(0, String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), null);
|
// ½ûÖ¹¸³Öµ/Õ³Ìù
|
this.hourText.setTransferHandler(null);
|
|
// ʱ¼ä·Ö¸ô·û
|
JLabel timeSeparator = new JLabel(":", JLabel.CENTER);
|
timeSeparator.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
|
|
// ·ÖÖÓÎı¾
|
this.minText = new JTextField(null, col);
|
this.minText.setBorder(BorderFactory.createEmptyBorder(2, 0, 2, 0));
|
this.minText.setHorizontalAlignment(SwingConstants.CENTER);
|
// ÉèÖà ×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ¡£
|
MyDocument docMin = new MyDocument(59);
|
this.minText.setDocument(docMin);
|
docMin.insertString(0, String.valueOf(calendar.get(Calendar.MINUTE)), null);
|
// ½ûÖ¹¸³Öµ/Õ³Ìù
|
this.minText.setTransferHandler(null);
|
|
add(title);
|
add(hourText);
|
add(timeSeparator);
|
add(minText);
|
setVisible(true);
|
}
|
|
// ÉèÖà ×Ô¶¨ÒåDocument£¬Ö÷ÒªÓû§¿ØÖÆÊäÈëºÍɾ³ýÎÊÌâ
|
private class MyDocument extends PlainDocument {
|
private int max;
|
private DecimalFormat dcf = new DecimalFormat("00");
|
|
public MyDocument(int max) {
|
this.max = max;
|
}
|
|
@Override
|
public void insertString(int offs, String str, AttributeSet a) {
|
int i = checkInput(offs, str);
|
if (i < 0 || i > max) {
|
return;
|
}
|
try {
|
remove(0, getLength());
|
String s = dcf.format(i);
|
super.insertString(0, s, a);
|
} catch (BadLocationException e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
* ÊäÈëÑéÖ¤
|
* Îı¾¿òÒÑÊäÈëµÄÖµ£¬²»Äܳ¬¹ýmaxÖµ¡£
|
*/
|
private int checkInput(int offs, String str) {
|
try {
|
StringBuilder text = new StringBuilder(getText(0, getLength())).insert(offs, str);
|
String s = text.toString().trim();
|
return Integer.parseInt(s);
|
} catch (NumberFormatException | ArrayIndexOutOfBoundsException | BadLocationException e) {
|
e.printStackTrace();
|
return -1;
|
}
|
}
|
}
|
}
|
|
|
public static void main(String[] args) {
|
|
Test2 dateTimePicker = new Test2("yyyy-MM-dd HH:mm", 160, 200);
|
JTextField showDate1 = new JTextField("µ¥»÷Ñ¡ÔñÈÕÆÚ");
|
dateTimePicker.setEnable(true).setSelect(new Date()).changeDateEvent(new Consumer<Test2>() {
|
@Override
|
public void accept(Test2 o) {
|
System.out.println(o.getSelect());
|
}
|
}).register(showDate1);
|
|
Test2 dateTimePicker1 = new Test2("yyyy-MM-dd", 200, 200);
|
JLabel label = new JLabel("µ¥»÷Ñ¡ÖÐÈÕÆÚ");
|
dateTimePicker1.setEnable(true).setTimeEnable(false).changeDateEvent(new Consumer<Test2>() {
|
@Override
|
public void accept(Test2 o) {
|
System.out.println(o.getSelect());
|
}
|
}).register(label);
|
|
JFrame jf = new JFrame("²âÊÔÈÕÆÚÑ¡ÔñÆ÷");
|
jf.add(showDate1, BorderLayout.NORTH);
|
jf.add(label, BorderLayout.SOUTH);
|
jf.setBounds(500, 500, 500, 500);
|
jf.setLocationRelativeTo(null);
|
jf.setVisible(true);
|
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
}
|
}
|