目的
网上Java Swing组件太少,并且UI太复古,索性自己写了一个,使用了flatlaf皮肤美化包和ikonli图标库,有感兴趣的同学可以去了解,以下是源码,可供参考。
效果图
动态图中的效果有bug 已修复 懒得录制了 就这样看着吧
代码
DatePickerX 时间组件
package com.tools.common;
import com.tools.callback.DatePockerXCallback;
import com.tools.enums.Week;
import com.tools.util.DateConvertUtil;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.swing.FontIcon;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DatePickerX
{
//当前时间
public Date currentTime;
private int year;
private int month;
private int day;
private int hour;
private int minute;
private int second;
public JButton yearButton;
public JButton monthButton;
private JPanel centerPanel;
private JPanel topToolsPanel;
public JDialog jDialog;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
private String checkJlbleName="";
private DatePockerXCallback datePockerXCallback;
public DatePickerX (Date currentTime, DatePockerXCallback datePockerXCallback,Component parentComponent ){
jDialog=new JDialog();
this.datePockerXCallback=datePockerXCallback;
this.currentTime=currentTime;
Calendar calendar=Calendar.getInstance();
calendar.setTime(currentTime);
this.year=calendar.get(Calendar.YEAR);
this.month=calendar.get(Calendar.MONTH)+1;
this.day=calendar.get(Calendar.DATE);
this.hour=calendar.get(Calendar.HOUR_OF_DAY);
this.minute=calendar.get(Calendar.MINUTE);
this.second=calendar.get(Calendar.SECOND);
checkDateView(parentComponent);
jDialogListener(parentComponent);
}
private void jDialogListener(Component parentComponent){
jDialog.addWindowListener(new java.awt.event.WindowAdapter() {
//打开
@Override
public void windowActivated(java.awt.event.WindowEvent evt) {
//Point point=MouseInfo.getPointerInfo().getLocation();
//弹框位置
//jDialog.setLocation((int) (point.getX()-(jDialog.getWidth()/2)), (int) (point.getY()+20));
//jDialog.setLocation((int) (parentComponent.getX()-(jDialog.getWidth()/2)), (int) (parentComponent.getY()+20));
}
//第一次打开
@Override
public void windowOpened(WindowEvent e) {
Point point=MouseInfo.getPointerInfo().getLocation();
//弹框位置
jDialog.setLocation((int) (point.getX()-(jDialog.getWidth()/2)), (int) (point.getY()+20));
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
public void checkDateView(Component parentComponent){
jDialog.setSize(400,300);
jDialog.setUndecorated(true);
jDialog.setAlwaysOnTop(true);
JPanel jPanel=new JPanel();
jPanel.setSize(jDialog.getWidth(),jDialog.getHeight());
jPanel.setLayout(null);
//设置边框颜色
jPanel.setBorder(BorderFactory.createLineBorder(new Color(144,144,144)));
jDialog.add(jPanel, BorderLayout.CENTER);
//顶部操作栏
topToolsPanel(jPanel);
//头部分隔符
/* JSeparator separator=new JSeparator(JSeparator.HORIZONTAL);
separator.setBounds(0,50,jPanel.getWidth(),1);
separator.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.LIGHT_GRAY));
jPanel.add(separator);*/
centerPanel=new JPanel();
centerPanel.setLayout(null);
centerPanel.setBounds(0,50,jPanel.getWidth(),jPanel.getHeight()-100);
//设置边框颜色
centerPanel.setBorder(BorderFactory.createMatteBorder(1,1,0,1,new Color(144,144,144)));
//顶部周一到周日
//opWeek(centerPanel);
//日期
showDate(year,month,centerPanel);
jPanel.add(centerPanel);
//底部分割线
JSeparator separator1=new JSeparator(JSeparator.HORIZONTAL);
separator1.setBounds(0,jPanel.getHeight()-50,jPanel.getWidth(),1);
separator1.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.LIGHT_GRAY));
jPanel.add(separator1);
//时
JTextField hourText=new JTextField();
hourText.setBounds(10,jPanel.getHeight()-40,40,30);
hourText.setText(hour+"");
jPanel.add(hourText);
//分
JTextField minuteText=new JTextField();
minuteText.setBounds(60,jPanel.getHeight()-40,40,30);
minuteText.setText(minute+"");
jPanel.add(minuteText);
//秒
JTextField secondText=new JTextField();
secondText.setBounds(110,jPanel.getHeight()-40,40,30);
secondText.setText(second+"");
jPanel.add(secondText);
//取消
JButton colse=new JButton("取消");
colse.setBounds(jPanel.getWidth()-80,jPanel.getHeight()-40,70,30);
colse.addActionListener(e->{
jDialog.setVisible(false);
});
jPanel.add(colse);
//确认
JButton confirm=new JButton("确认");
confirm.setBounds(jPanel.getWidth()-80-80,jPanel.getHeight()-40,70,30);
jPanel.add(confirm);
confirm.addActionListener(e->{
if (hourText.getText().isEmpty()) {
hour = 00;
} else {
int val =Integer.parseInt(hourText.getText());
//必须是0-24时
if (val>24 || val<0){
JOptionPane.showMessageDialog(jDialog,"小时必须是0-24时");
}
hour = Integer.parseInt(hourText.getText());
}
if (minuteText.getText().isEmpty()) {
minute = 00;
} else {
int val =Integer.parseInt(minuteText.getText());
//必须是0-60分
if (val>60 || val<0){
JOptionPane.showMessageDialog(jDialog,"分钟必须是0-60分");
}
minute = Integer.parseInt(minuteText.getText());
}
if (secondText.getText().isEmpty()) {
second = 00;
} else {
int val =Integer.parseInt(secondText.getText());
//必须是0-60秒
if (val>60 || val<0){
JOptionPane.showMessageDialog(jDialog,"秒必须是0-60秒");
}
second = Integer.parseInt(secondText.getText());
}
Calendar calendar1=Calendar.getInstance();
calendar1.set(Calendar.YEAR,year);
calendar1.set(Calendar.MONTH,month-1);
calendar1.set(Calendar.DATE,day);
calendar1.set(Calendar.HOUR_OF_DAY,hour);
calendar1.set(Calendar.MINUTE,minute);
calendar1.set(Calendar.SECOND,second);
currentTime=calendar1.getTime();
Map<String,Object> map= new HashMap<>();
map.put("year",year);
map.put("month",month);
map.put("day",day);
map.put("hour",hour);
map.put("minute",minute);
map.put("second",second);
map.put("time",currentTime);
datePockerXCallback.saveCallback(map);
jDialog.setVisible(false);
});
//jDialog.setVisible(true);
}
private void topToolsPanel(JPanel jPanel){
topToolsPanel=new JPanel();
//topToolsPanel.removeAll();
Calendar calendar=Calendar.getInstance();
calendar.setTime(currentTime);
topToolsPanel.setLayout(null);
topToolsPanel.setBounds(1,1,jPanel.getWidth()-2,49);
topToolsPanel.setName("topToolsPanel");
//上一年箭头
FontIcon leftYearIcon=new FontIcon();
leftYearIcon.setIkon(AntDesignIconsOutlined.DOUBLE_LEFT);
leftYearIcon.setIconSize(14);
leftYearIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
//上一年
JButton upYearButton=new JButton("",leftYearIcon);
upYearButton.setBounds(10,10,40,30);
upYearButton.setBackground(new Color(0,0,0,0));
upYearButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(upYearButton);
topToolsPanel.add(upYearButton);
//月左箭头
FontIcon leftMonthIcon=new FontIcon();
leftMonthIcon.setIkon(AntDesignIconsOutlined.LEFT);
leftMonthIcon.setIconSize(14);
leftMonthIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
//上一月
JButton upMonthButton=new JButton("",leftMonthIcon);
upMonthButton.setBounds(50,10,40,30);
upMonthButton.setBackground(new Color(0,0,0,0));
upMonthButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(upMonthButton);
topToolsPanel.add(upMonthButton);
//年按钮
yearButton=new JButton(calendar.get(Calendar.YEAR)+"年");
yearButton.setBounds(topToolsPanel.getWidth()/2-60,10,70,30);
//yearButton.setOpaque(true);
yearButton.setBackground(new Color(0,0,0,0));
yearButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(yearButton);
topToolsPanel.add(yearButton);
//月按钮
monthButton=new JButton(calendar.get(Calendar.MONTH)+1+"月");
monthButton.setBounds(topToolsPanel.getWidth()/2,10,40,30);
//monthButton.setOpaque(true);
monthButton.setBackground(new Color(0,0,0,0));
monthButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(monthButton);
topToolsPanel.add(monthButton);
//月右箭头
FontIcon rightMonthIcon=new FontIcon();
rightMonthIcon.setIkon(AntDesignIconsOutlined.RIGHT);
rightMonthIcon.setIconSize(14);
rightMonthIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
//下一月
JButton downMonthButton=new JButton("",rightMonthIcon);
downMonthButton.setBounds(topToolsPanel.getWidth()-90,10,40,30);
downMonthButton.setBackground(new Color(0,0,0,0));
downMonthButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(downMonthButton);
topToolsPanel.add(downMonthButton);
//下一年箭头
FontIcon rightYearIcon=new FontIcon();
rightYearIcon.setIkon(AntDesignIconsOutlined.DOUBLE_RIGHT);
rightYearIcon.setIconSize(14);
rightYearIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
//下一年
JButton downYearButton=new JButton("",rightYearIcon);
downYearButton.setBounds(topToolsPanel.getWidth()-50,10,40,30);
downYearButton.setBackground(new Color(0,0,0,0));
downYearButton.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(downYearButton);
topToolsPanel.add(downYearButton);
yearButton.addActionListener(e->{
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
//重新加载
topToolsPanel.setVisible(false);
centerPanel.setBounds(0,0,jPanel.getWidth(),centerPanel.getHeight()+49);
yearJPane(centerPanel,calendar.get(Calendar.YEAR)-8,calendar.get(Calendar.YEAR)+7);
});
monthButton.addActionListener(e->{
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
//重新加载
topToolsPanel.setVisible(false);
centerPanel.setBounds(0,0,jPanel.getWidth(),centerPanel.getHeight()+49);
//重新加载
monthJPane(centerPanel,year);
});
upYearButton.addActionListener(e->{
year=year-1;
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
yearButton.setText(year+"年");
showDate(year,month,centerPanel);
});
downYearButton.addActionListener(e->{
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
year=year+1;
yearButton.setText(year+"年");
showDate(year,month,centerPanel);
});
upMonthButton.addActionListener(e->{
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
if (month==1){
year=year-1;
month=12;
yearButton.setText(year+"年");
}else{
month=month-1;
}
monthButton.setText(month+"月");
showDate(year,month,centerPanel);
});
downMonthButton.addActionListener(e->{
//删除
centerPanel.removeAll();
//更新UI
centerPanel.updateUI();
if (month==12){
year=year+1;
month=1;
yearButton.setText(year+"年");
}else{
month=month+1;
}
monthButton.setText(month+"月");
showDate(year,month,centerPanel);
});
jPanel.add(topToolsPanel);
}
private void setJButtonColor(JButton button) {
Font newFont=button.getFont().deriveFont(14f);
button.setFont(newFont);
button.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
button.setForeground(new Color(64,158,255));
if (button.getIcon()!=null){
FontIcon fontIcon=(FontIcon) button.getIcon();
fontIcon.setIconColor(new Color(64,158,255));
}
}
@Override
public void mouseExited(MouseEvent e) {
button.setForeground(UIManager.getColor("$Button.foreground"));
if (button.getIcon()!=null){
FontIcon fontIcon=(FontIcon) button.getIcon();
fontIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
}
}
});
}
/**
* 年份
* @param jPanel
*/
private void yearJPane(JPanel jPanel,int startYear,int endYear){
//一页多少年
int yearCount=16;
JPanel top=new JPanel();
top.setLayout(null);
top.setBounds(1,1,jPanel.getWidth()-2,40);
top.setName("topPanel");
JLabel yearLabel=new JLabel(startYear+"年-"+endYear+"年",JLabel.CENTER);
yearLabel.setBounds(jPanel.getWidth()/2-75,10,150,30);
Font font=yearLabel.getFont().deriveFont(14f);
yearLabel.setFont(font);
top.add(yearLabel);
//上一页箭头
FontIcon leftYearIcon=new FontIcon();
leftYearIcon.setIkon(AntDesignIconsOutlined.DOUBLE_LEFT);
leftYearIcon.setIconSize(14);
leftYearIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
JButton left=new JButton("",leftYearIcon);
left.setBounds(10,10,40,30);
left.setBackground(new Color(0,0,0,0));
left.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(left);
top.add(left);
//下一页箭头
FontIcon rightYearIcon=new FontIcon();
rightYearIcon.setIkon(AntDesignIconsOutlined.DOUBLE_RIGHT);
rightYearIcon.setIconSize(14);
rightYearIcon.setIconColor(UIManager.getColor("Button.default.foreground"));
JButton right=new JButton("",rightYearIcon);
//right.setFont(new Font(null,Font.PLAIN,18));
right.setBounds(jPanel.getWidth()-50,10,40,30);
right.setBackground(new Color(0,0,0,0));
right.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
setJButtonColor(right);
left.addActionListener(e->{
//删除
jPanel.removeAll();
SwingUtilities.updateComponentTreeUI(jPanel);
//重新加载
yearJPane(jPanel,startYear-yearCount,startYear-1);
});
right.addActionListener(e->{
//删除
jPanel.removeAll();
SwingUtilities.updateComponentTreeUI(jPanel);
//重新加载
yearJPane(jPanel,endYear+1,endYear+yearCount);
});
top.add(right);
jPanel.add(top);
JSeparator separator=new JSeparator(JSeparator.HORIZONTAL);
separator.setBounds(0,50,jPanel.getWidth(),1);
separator.setBorder(BorderFactory.createMatteBorder(1,0,0,0,Color.LIGHT_GRAY));
jPanel.add(separator);
JPanel content=new JPanel();
content.setLayout(null);
content.setBounds(1,51,jPanel.getWidth()-2,jPanel.getHeight()-51);
content.setName("content");
int width=content.getWidth()/4;
int height=content.getHeight()/4;
int index=startYear;
Calendar date = Calendar.getInstance();
int year = date.get(Calendar.YEAR);
for (int i=0;i<4;i++){
for (int j=0;j<4;j++){
JLabel jLabel=new JLabel(index+"",JLabel.CENTER);
jLabel.setBounds(j*width,i*height,width,height);
jLabel.setName(index+"");
content.add(jLabel);
/*
if (year==index){
jLabel.setOpaque(true);
jLabel.setBackground(new Color(64,158,255));
checkJlbleName=jLabel.getName();
}
*/
int finalIndex = index;
jLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
/* if (!checkJlbleName.isEmpty()){
Component[] components=content.getComponents();
for (Component component : components) {
if (component.getName().equals(checkJlbleName)){
component.setBackground(UIManager.getColor("@background"));
}
}
}
jLabel.setOpaque(true);
jLabel.setBackground(new Color(64,158,255));
checkJlbleName=jLabel.getName();*/
jPanel.removeAll();
SwingUtilities.updateComponentTreeUI(jPanel);
setYear(finalIndex);
yearButton.setText(finalIndex+"年");
monthJPane(jPanel, finalIndex);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jLabel.setOpaque(true);
jLabel.setBackground(UIManager.getColor("Button.background"));
}
@Override
public void mouseExited(MouseEvent e) {
jLabel.setBackground(null);
}
});
index++;
}
}
jPanel.add(content);
jPanel.validate();
jPanel.repaint();
}
/**
* 月份
*/
private void monthJPane(JPanel jPanel,int year){
JPanel content=new JPanel();
content.setLayout(null);
content.setBounds(1,1,jPanel.getWidth()-2,jPanel.getHeight()-2);
content.setName("content");
jPanel.add(content);
int width=content.getWidth()/4;
int height=content.getHeight()/3;
int index=1;
for (int i=0;i<3;i++){
for (int j=0;j<4;j++){
JLabel jLabel=new JLabel(index+"月",JLabel.CENTER);
jLabel.setBounds(j*width,i*height,width,height);
jLabel.setName(index+"");
content.add(jLabel);
int finalIndex = index;
jLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
jPanel.removeAll();
SwingUtilities.updateComponentTreeUI(jPanel);
monthButton.setText(finalIndex+"月");
setMonth(finalIndex);
topToolsPanel.setVisible(true);
centerPanel.setBounds(0,50,jPanel.getWidth(),jPanel.getHeight()-49);
showDate(year,month,jPanel);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jLabel.setOpaque(true);
jLabel.setBackground(UIManager.getColor("Button.background"));
}
@Override
public void mouseExited(MouseEvent e) {
jLabel.setBackground(null);
}
});
index++;
}
}
}
/**
* 显示日期
* @param year
* @param month
* @param jPanel
*/
private void showDate(int year,int month,JPanel jPanel){
topWeek(jPanel);
JPanel content=new JPanel();
content.setLayout(null);
content.setBounds(1,30,jPanel.getWidth()-2,jPanel.getHeight()-30);
content.setName("content");
//一共有多少天
int day= DateConvertUtil.getMoneyDay2(year, month);
//第一天是周几
Date firstDayOfMonth1=DateConvertUtil.getFirstDayOfMonth1(year, month);
String weekName=DateConvertUtil.getWeek(firstDayOfMonth1);
int weekIndex=Week.getIndex(weekName);
//第一行空格数量
int sp=7-weekIndex;
//以一共有多少周
int week=6;
//lable宽度
int labelWidth=content.getWidth()/7;
//lable高度
int labelHeight=content.getHeight()/week;
int index=1;
for (int i=0;i<week;i++) {
for (int j = 0; j < 7; j++) {
if (i == 0) {
//第一行
if (j+1 < weekIndex) {
//空白
Calendar calendar=DateConvertUtil.dateDayCalculation(firstDayOfMonth1,-(6-j-sp));
JLabel jLabel = new JLabel(calendar.get(Calendar.DATE)+"", JLabel.CENTER);
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
jLabel.setEnabled(false);
content.add(jLabel);
} else {
//日期
JLabel jLabel = new JLabel(index +"", JLabel.CENTER);
jLabel.setName(index+"");
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
content.add(jLabel);
//设置背景色
setJLableColor(jLabel,content);
index++;
}
}else if(index>day){
//空白
Calendar calendar=DateConvertUtil.dateDayCalculation(firstDayOfMonth1,index-1);
JLabel jLabel = new JLabel(calendar.get(Calendar.DATE)+"", JLabel.CENTER);
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
jLabel.setEnabled(false);
content.add(jLabel);
index++;
/* //最后一行
if (index>day) {
//空白
JLabel jLabel = new JLabel("", JLabel.CENTER);
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
content.add(jLabel);
}else{
//日期
JLabel jLabel = new JLabel(index +"", JLabel.CENTER);
jLabel.setName(index+"");
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
content.add(jLabel);
setJLableColor(jLabel,content);
index++;
}*/
}else{
//中间行
JLabel jLabel = new JLabel(index +"", JLabel.CENTER);
jLabel.setName(index+"");
jLabel.setBounds(j * labelWidth, i * labelHeight, labelWidth, labelHeight);
content.add(jLabel);
setJLableColor(jLabel,content);
index++;
}
}
}
jPanel.add(content);
}
/**
* 顶部周一到周日
* @param jPanel
*/
private void topWeek(JPanel jPanel){
JPanel topPanel=new JPanel();
topPanel.setLayout(null);
topPanel.setBounds(1,1,jPanel.getWidth()-2,30);
topPanel.setName("topPanel");
int width=topPanel.getWidth()/7;
//周一到周日
int index=1;
for (int i=0;i<7;i++){
JLabel jLabel=new JLabel(Week.getName(index),JLabel.CENTER);
if (i==0){
jLabel.setBounds(1,1,width,30);
}else{
jLabel.setBounds(width*i,1,width,30);
}
/* if (i==6) {
jLabel.setBorder(BorderFactory.createMatteBorder(0,0,1,0,Color.LIGHT_GRAY));
}else{
jLabel.setBorder(BorderFactory.createMatteBorder(0,0,1,1,Color.LIGHT_GRAY));
}*/
topPanel.add(jLabel);
jLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
/* System.out.println("获取焦点");
jLabel.setOpaque(true);
jLabel.setBackground(new Color(64,158,255));*/
}
@Override
public void mouseExited(MouseEvent e) {
/* System.out.println("失去焦点");
Color color=UIManager.getColor("@background");
jLabel.setBackground(color);*/
}
});
index++;
}
jPanel.add(topPanel);
}
private void setJLableColor(JLabel jLabel,JPanel jPanel){
jLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (!checkJlbleName.isEmpty()){
Component[] components=jPanel.getComponents();
for (Component component : components) {
if (component.getName()==null){
continue;
}
if (component.getName().equals(checkJlbleName)){
component.setBackground(UIManager.getColor("@background"));
}
}
}
/* if (!checkJlbleName.isEmpty()){
JLabel label=getComponentByName(jPanel,checkJlbleName);
Color color=UIManager.getColor("@background");
label.setBackground(color);
}*/
jLabel.setOpaque(true);
jLabel.setBackground(UIManager.getColor("Button.background"));
checkJlbleName=jLabel.getName();
setDay(Integer.parseInt(jLabel.getText()));
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
jLabel.setOpaque(true);
jLabel.setBackground(UIManager.getColor("Button.background"));
}
@Override
public void mouseExited(MouseEvent e) {
if (jLabel.getName().equals(checkJlbleName)){
return;
}
jLabel.setBackground(null);
}
});
}
}
DatePockerXCallback 回调接口
import java.util.Map;
public interface DatePockerXCallback {
public void saveCallback(Map<String,Object> map);
}
DateConvertUtil 时间工具类
package com.tools.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 时间转化工具
*/
public class DateConvertUtil {
/**
* 根据时间转换为时间戳
* @param date
* @param timestampType 转换类型 0毫秒 1秒
* @return
*/
public long getTimeStamp(Date date, int timestampType)
{
long times = date.getTime();
if (timestampType == 1)
{
times = times/1000L;
}
return times;
}
/**
* 时间戳转时间
* @param timestamp
* @param timestampType 时间戳格式 0毫秒 1秒
* @return
*/
public Date getDateTime(long timestamp,int timestampType)
{
if (timestampType == 1)
{
//如果时间戳格式是秒,需要江时间戳变为毫秒
timestamp = timestamp * 1000L;
}
Date dateTime = new Date(timestamp);
return dateTime;
}
/**
* 格式化传入的时间,将时间转化为指定格式字符串
* @param date
* @param format 时间格式,如:yyyy-MM-dd HH:mm:ss SSS 或 yyyy年MM月dd日 HH:mm:ss
* @return
*/
public String getDateTimeString(Date date,String format )
{
if (format == null || format.length() <=0)
{
return null;
}
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timeString = sdf.format(date);
return timeString;
}
/**
* 格式化传入的时间戳,将时间戳转化为指定格式字符串
* @param timestamp
* @param format 时间格式,如:yyyy-MM-dd HH:mm:ss SSS 或 yyyy年MM月dd日 HH:mm:ss *
* @param timestampType 时间戳格式 0毫秒 1秒
* @return
*/
public String getTimeStampString(long timestamp,String format ,int timestampType)
{
if (format == null || format.length() <=0)
{
return null;
}
if (timestampType == 1)
{
//如果时间戳格式是秒,需要江时间戳变为毫秒
timestamp = timestamp * 1000L;
}
Date dateTime = new Date(timestamp);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(format);
String timeString = sdf.format(dateTime);
return timeString;
}
/**
* 传入时间 获得天数 如果要得到其他月份的天数 设置对象的月份以及年份即可
* */
public static int getMoneyDay2(int year , int month) {
Calendar a = Calendar.getInstance();//获取当前时间
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);// Calendar月份是以0开始的 所以要-1
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int day = a.get(Calendar.DATE);
return day;
}
/**
* 获取指定年月的第一天
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth1(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最小天数
int firstDay = cal.getMinimum(Calendar.DATE);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH,firstDay);
//格式化日期
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return cal.getTime();
}
//根据日期取得星期几
public static String getWeek(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("EEEE", Locale.CHINA);
String week = sdf.format(date);
return week;
}
/**
* 日期天数计算
*/
public static Calendar dateDayCalculation(Date date,int value){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, value);
return cal;
}
}
使用方式
1、初始化
2、显示面板
3、回调处理
//选择时间
JButton selectTimeButton=new JButton("选择时间");
selectTimeButton.setBounds(timestampResultTextField.getX()+timestampResultTextField.getWidth()+10, 30, 100, 20);
jPanel.add(selectTimeButton);
//初始化时间
DatePickerX datePickerX=new DatePickerX(new Date(), new DatePockerXCallback() {
//回调函数 具体可以打印这个Map里面内容 包含 年-秒
@Override
public void saveCallback(Map<String, Object> map) {
timestampTextField.setText(((Date)map.get("time")).getTime()+"");
}
}, selectTimeButton);
selectTimeButton.addActionListener(e->{
//显示面板
datePickerX.jDialog.setVisible(true);
});
maven
<!-- https://mvnrepository.com/artifact/com.formdev/flatlaf -->
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>3.2.5</version>
</dependency>
<!-- ikon图标库 -->
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-swing</artifactId>
<version>12.3.1</version>
</dependency>
<!-- antd蚂蚁图标库 -->
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-antdesignicons-pack</artifactId>
<version>12.3.1</version>
</dependency>