这段时间由于公司需要,在做C/S的东西,自然考虑优先用SWING实现,自然遇到了日期选择控件的问题,在网上GOOGLE了,发现了一个不错的控件,故转载之.
参考界面如下:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/heroying/2005.9.30.12.41.18.10.14.jpg)
//
DatePicker.java
package
com.kxsoft.component;
//
修改
import
java.awt.
*
;
import
java.awt.event.
*
;
import
java.util.GregorianCalendar;
import
java.util.Date;
import
java.util.Calendar;
import
java.text.DateFormat;
import
java.text.FieldPosition;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.swing.
*
;
import
javax.swing.plaf.BorderUIResource;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
final
class
DatePicker
extends
JPanel
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final long serialVersionUID = 1L;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final int startX = 10;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final int startY = 60;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Font smallFont = new Font("Dialog", Font.PLAIN, 10);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Font largeFont = new Font("Dialog", Font.PLAIN, 12);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Insets insets = new Insets(2, 2, 2, 2);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Color highlight = new Color(255, 255, 204);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Color white = new Color(255, 255, 255);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final Color gray = new Color(204, 204, 204);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private Component selectedDay = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private GregorianCalendar selectedDate = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private GregorianCalendar originalDate = null;
private boolean hideOnSelect = true;
private final JButton backButton = new JButton();
private final JLabel monthAndYear = new JLabel();
private final JButton forwardButton = new JButton();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private final JLabel[] dayHeadings = new JLabel[]...{
new JLabel("日"),
new JLabel("一"),
new JLabel("二"),
new JLabel("三"),
new JLabel("四"),
new JLabel("五"),
new JLabel("六")};
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private final JLabel[][] daysInMonth = new JLabel[][]...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()},
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel(),
new JLabel()}
};
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private final JButton todayButton = new JButton();
private final JButton cancelButton = new JButton();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public DatePicker() ...{
super();
selectedDate = getToday();
init();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public DatePicker(final Date initialDate) ...{
super();
if (null == initialDate)
selectedDate = getToday();
else
(selectedDate = new GregorianCalendar()).setTime(initialDate);
originalDate = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
selectedDate.get(Calendar.DATE));
init();
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public boolean isHideOnSelect() ...{
return hideOnSelect;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setHideOnSelect(final boolean hideOnSelect) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (this.hideOnSelect != hideOnSelect) ...{
this.hideOnSelect = hideOnSelect;
initButtons(false);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Date getDate() ...{
if (null != selectedDate)
return selectedDate.getTime();
return null;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void init() ...{
setLayout(new AbsoluteLayout());
this.setMinimumSize(new Dimension(161, 226));
this.setMaximumSize(getMinimumSize());
this.setPreferredSize(getMinimumSize());
this.setBorder(new BorderUIResource.EtchedBorderUIResource());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
backButton.setFont(smallFont);
backButton.setText("<");
backButton.setMargin(insets);
backButton.setDefaultCapable(false);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
backButton.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(final ActionEvent evt) ...{
onBackClicked(evt);
}
});
add(backButton, new AbsoluteConstraints(10, 10, 20, 20));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
monthAndYear.setFont(largeFont);
monthAndYear.setHorizontalAlignment(JTextField.CENTER);
monthAndYear.setText(formatDateText(selectedDate.getTime()));
add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));
forwardButton.setFont(smallFont);
forwardButton.setText(">");
forwardButton.setMargin(insets);
forwardButton.setDefaultCapable(false);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
forwardButton.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(final ActionEvent evt) ...{
onForwardClicked(evt);
}
});
add(forwardButton, new AbsoluteConstraints(130, 10, 20, 20));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int x = startX;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int ii = 0; ii < dayHeadings.length; ii++) ...{
dayHeadings[ii].setOpaque(true);
dayHeadings[ii].setBackground(Color.LIGHT_GRAY);
dayHeadings[ii].setForeground(Color.WHITE);
dayHeadings[ii].setHorizontalAlignment(JLabel.CENTER);
add(dayHeadings[ii], new AbsoluteConstraints(x, 40, 21, 21));
x += 20;
}
x = startX;
int y = startY;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int ii = 0; ii < daysInMonth.length; ii++) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int jj = 0; jj < daysInMonth[ii].length; jj++) ...{
daysInMonth[ii][jj].setOpaque(true);
daysInMonth[ii][jj].setBackground(white);
daysInMonth[ii][jj].setFont(smallFont);
daysInMonth[ii][jj].setHorizontalAlignment(JLabel.CENTER);
daysInMonth[ii][jj].setText("");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
daysInMonth[ii][jj].addMouseListener(new MouseAdapter() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void mouseClicked(final MouseEvent evt) ...{
onDayClicked(evt);
}
});
add(daysInMonth[ii][jj], new AbsoluteConstraints(x, y, 21, 21));
x += 20;
}
x = startX;
y += 20;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
initButtons(true);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
calculateCalendar();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void initButtons(final boolean firstTime) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (firstTime) ...{
final Dimension buttonSize = new Dimension(68, 24);
todayButton.setText("今天");
todayButton.setMargin(insets);
todayButton.setMaximumSize(buttonSize);
todayButton.setMinimumSize(buttonSize);
todayButton.setPreferredSize(buttonSize);
todayButton.setDefaultCapable(true);
todayButton.setSelected(true);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
todayButton.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(final ActionEvent evt) ...{
onToday(evt);
}
});
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cancelButton.setText("取消");
cancelButton.setMargin(insets);
cancelButton.setMaximumSize(buttonSize);
cancelButton.setMinimumSize(buttonSize);
cancelButton.setPreferredSize(buttonSize);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
cancelButton.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(final ActionEvent evt) ...{
onCancel(evt);
}
});
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
this.remove(todayButton);
this.remove(cancelButton);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (hideOnSelect) ...{
add(todayButton, new AbsoluteConstraints(25, 190, 52, -1));
add(cancelButton, new AbsoluteConstraints(87, 190, 52, -1));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} else ...{
add(todayButton, new AbsoluteConstraints(55, 190, 52, -1));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onToday(final java.awt.event.ActionEvent evt) ...{
selectedDate = getToday();
setVisible(!hideOnSelect);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (isVisible()) ...{
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onCancel(final ActionEvent evt) ...{
selectedDate = originalDate;
setVisible(!hideOnSelect);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onForwardClicked(final java.awt.event.ActionEvent evt) ...{
final int day = selectedDate.get(Calendar.DATE);
selectedDate.set(Calendar.DATE, 1);
selectedDate.add(Calendar.MONTH, 1);
selectedDate.set(Calendar.DATE,
Math.min(day, calculateDaysInMonth(selectedDate)));
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onBackClicked(final java.awt.event.ActionEvent evt) ...{
final int day = selectedDate.get(Calendar.DATE);
selectedDate.set(Calendar.DATE, 1);
selectedDate.add(Calendar.MONTH, -1);
selectedDate.set(Calendar.DATE,
Math.min(day, calculateDaysInMonth(selectedDate)));
monthAndYear.setText(formatDateText(selectedDate.getTime()));
calculateCalendar();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onDayClicked(final java.awt.event.MouseEvent evt) ...{
final javax.swing.JLabel fld = (javax.swing.JLabel) evt.getSource();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (!"".equals(fld.getText())) ...{
fld.setBackground(highlight);
selectedDay = fld;
selectedDate.set(
Calendar.DATE,
Integer.parseInt(fld.getText()));
setVisible(!hideOnSelect);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static GregorianCalendar getToday() ...{
final GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.HOUR_OF_DAY, 0);
gc.set(Calendar.MINUTE, 0);
gc.set(Calendar.SECOND, 0);
gc.set(Calendar.MILLISECOND, 0);
return gc;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void calculateCalendar() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (null != selectedDay) ...{
selectedDay.setBackground(white);
selectedDay = null;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
final GregorianCalendar c = new GregorianCalendar(
selectedDate.get(Calendar.YEAR),
selectedDate.get(Calendar.MONTH),
1);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
final int maxDay = calculateDaysInMonth(c);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
final int selectedDay = Math.min(maxDay, selectedDate.get(
Calendar.DATE));
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int dow = c.get(Calendar.DAY_OF_WEEK);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int dd = 0; dd < dow; dd++) ...{
daysInMonth[0][dd].setText("");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int week;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
do ...{
week = c.get(Calendar.WEEK_OF_MONTH);
dow = c.get(Calendar.DAY_OF_WEEK);
final JLabel fld = this.daysInMonth[week - 1][dow - 1];
fld.setText(Integer.toString(c.get(Calendar.DATE)));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (selectedDay == c.get(Calendar.DATE)) ...{
fld.setBackground(highlight);
this.selectedDay = fld;
}
if (c.get(Calendar.DATE) >= maxDay)
break;
c.add(Calendar.DATE, 1);
} while (c.get(Calendar.DATE) <= maxDay);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
week--;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int ww = week; ww < daysInMonth.length; ww++) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for (int dd = dow; dd < daysInMonth[ww].length; dd++) ...{
daysInMonth[ww][dd].setText("");
}
dow = 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
c.set(Calendar.DATE, selectedDay);
selectedDate = c;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static int calculateDaysInMonth(final Calendar c) ...{
int daysInMonth = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
switch (c.get(Calendar.MONTH)) ...{
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
daysInMonth = 31;
break;
case 3:
case 5:
case 8:
case 10:
daysInMonth = 30;
break;
case 1:
final int year = c.get(Calendar.YEAR);
daysInMonth =
(0 == year % 1000) ? 29 :
(0 == year % 100) ? 28 :
(0 == year % 4) ? 29 : 28;
break;
}
return daysInMonth;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static String formatDateText(final Date dt) ...{
final DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
final StringBuffer mm = new StringBuffer();
final StringBuffer yy = new StringBuffer();
final FieldPosition mmfp = new FieldPosition(DateFormat.MONTH_FIELD);
final FieldPosition yyfp = new FieldPosition(DateFormat.YEAR_FIELD);
df.format(dt, mm, mmfp);
df.format(dt, yy, yyfp);
return (mm.toString().substring(mmfp.getBeginIndex(), mmfp.getEndIndex()) +
"月 " + yy.toString().substring(yyfp.getBeginIndex(), yyfp.getEndIndex()) + "年");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
//
DataField.java
package
com.kxsoft.component;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Insets;
import
java.awt.Point;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.event.ComponentAdapter;
import
java.awt.event.ComponentEvent;
import
java.awt.event.MouseAdapter;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseMotionAdapter;
import
java.net.URL;
import
java.text.DateFormat;
import
java.text.ParseException;
import
java.util.Date;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.swing.ImageIcon;
import
javax.swing.JButton;
import
javax.swing.JDialog;
import
javax.swing.JFrame;
import
javax.swing.JPanel;
import
javax.swing.JTextField;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
final
class
DateField
extends
JPanel
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static final long serialVersionUID = 1L;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private final JTextField dateText = new JTextField(12);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private final JButton dropdownButton = new JButton();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private DatePicker dp;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private JDialog dlg;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Point origin = new Point();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
final class Listener extends ComponentAdapter ...{
//日期选择面板消失时调用的方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void componentHidden(final ComponentEvent evt) ...{
final Date dt = ((DatePicker) evt.getSource()).getDate();
if (null != dt)
dateText.setText(dateToString(dt));
dlg.dispose();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public DateField() ...{
super();
init();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public DateField(final Date initialDate) ...{
super();
init();
dateText.setText(dateToString(initialDate));
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public Date getDate() ...{
return stringToDate(dateText.getText());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void setDate(Date date) ...{
String v = dateToString(date);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (v == null) ...{
v = "";
}
dateText.setText(v);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void init() ...{
setLayout(new BorderLayout());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
dateText.setText("");
dateText.setEditable(false);
dateText.setBackground(new Color(255, 255, 255));
add(dateText, BorderLayout.CENTER);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
URL url = DateField.class.getResource("/com/kxsoft/component/dropdown.gif");//修改路径,取你本地的图片路径.
ImageIcon icon = new ImageIcon(url);
dropdownButton.setIcon(icon);
dropdownButton.setMargin(new Insets(2, 2, 2, 2));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
dropdownButton.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(final ActionEvent evt) ...{
onButtonClick(evt);
}
});
add(dropdownButton, BorderLayout.EAST);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onButtonClick(final java.awt.event.ActionEvent evt) ...{
if ("".equals(dateText.getText()))
dp = new DatePicker();
else
dp = new DatePicker(stringToDate(dateText.getText()));
dp.addComponentListener(new Listener());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
final Point p = dateText.getLocationOnScreen();
p.setLocation(p.getX(), p.getY() - 1 + dateText.getSize().getHeight());
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
dlg = new JDialog(new JFrame(), true);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
dlg.addMouseListener(new MouseAdapter() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void mousePressed(MouseEvent e) ...{
origin.x = e.getX();
origin.y = e.getY();
}
});
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
dlg.addMouseMotionListener(new MouseMotionAdapter() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void mouseDragged(MouseEvent e) ...{
Point p = dlg.getLocation();
dlg.setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
}
});
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
dlg.setLocation(p);
dlg.setResizable(false);
dlg.setUndecorated(true);
dlg.getContentPane().add(dp);
dlg.pack();
dlg.setVisible(true);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static String dateToString(final Date dt) ...{
if (null != dt)
return DateFormat.getDateInstance(DateFormat.LONG).format(dt);
return null;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static Date stringToDate(final String s) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
return DateFormat.getDateInstance(DateFormat.LONG).parse(s);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (ParseException e) ...{
return null;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main(String[] args) ...{
JDialog dlg = new JDialog(new JFrame(), true);
DateField df = new DateField();
dlg.getContentPane().add(df);
dlg.pack();
dlg.setVisible(true);
System.out.println(df.getDate().toString());
System.exit(0);
}
}
package
com.hzlongway.hero.tools;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
AbsoluteConstraints.java
import
java.awt.Dimension;
import
java.awt.Point;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
AbsoluteConstraints
implements
java.io.Serializable
...
{
static final long serialVersionUID = 5261460716622152494L;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int x;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int y;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int width = -1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int height = -1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(Point pos) ...{
this(pos.x, pos.y);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(int x, int y) ...{
this.x = x;
this.y = y;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(Point pos, Dimension size) ...{
this.x = pos.x;
this.y = pos.y;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (size != null) ...{
this.width = size.width;
this.height = size.height;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(int x, int y, int width, int height) ...{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getX() ...{
return x;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getY() ...{
return y;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getWidth() ...{
return width;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getHeight() ...{
return height;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String toString() ...{
return super.toString() + " [x=" + x + ", y=" + y + ", width=" + width
+ ", height=" + height + "]";
}
}
package
com.hzlongway.hero.tools;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//
AbsoluteConstraints.java
import
java.awt.Dimension;
import
java.awt.Point;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
AbsoluteConstraints
implements
java.io.Serializable
...
{
static final long serialVersionUID = 5261460716622152494L;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int x;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int y;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int width = -1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public int height = -1;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(Point pos) ...{
this(pos.x, pos.y);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(int x, int y) ...{
this.x = x;
this.y = y;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(Point pos, Dimension size) ...{
this.x = pos.x;
this.y = pos.y;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (size != null) ...{
this.width = size.width;
this.height = size.height;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public AbsoluteConstraints(int x, int y, int width, int height) ...{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getX() ...{
return x;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getY() ...{
return y;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getWidth() ...{
return width;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public int getHeight() ...{
return height;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String toString() ...{
return super.toString() + " [x=" + x + ", y=" + y + ", width=" + width
+ ", height=" + height + "]";
}
}
原帖地址点击此处.