1、JAVA实现日期选择控件JAVA实现日期选择控件实现效果如下所示:想自己写个小的记帐本,数据库那该没问题了,想想前台的布置的时候,突然想要有个日期选择的控件旧好了,我知道DELPHI里有的,不知道JAVA里有没有,百渡下,好象没有很成型的,还有的要收费的,找到了下面的程序,是嵌到Button里的,自己写了个测试程序,还行,界面还算可以,基本功能也基本实现,虽然有些地方不是很智能,有待改进.程序如下:import java.util.Date;import java.util.Calendar;import java.text.DateFormat;import java.text.Simple
2、DateFormat;import java.text.ParseException;import java.awt.Color;import java.awt.Font;import java.awt.Point;import java.awt.Dimension;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Component;import java.awt.Cursor;import java.awt.Frame;import java.
3、awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;/import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JPanel;impo
4、rt javax.swing.JLabel;import javax.swing.JSpinner;import javax.swing.JSpinner.NumberEditor;import javax.swing.SpinnerNumberModel;import javax.swing.SwingUtilities;import javax.swing.SwingConstants;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import javax.swing.border.
5、LineBorder;public class DateChooserJButton extends JButton privateDateChooserdateChooser = null;private String preLabel = ;publicDateChooserJButton() this(getNowDate();publicDateChooserJButton(SimpleDateFormatdf, String dateString) this();setText(df, dateString);publicDateChooserJButton(Date date) t
6、his(, date);public DateChooserJButton(String preLabel, Date date) if (preLabel != null)this.preLabel = preLabel;setDate(date);setBorder(null);setCursor(new Cursor(Cursor.HAND_CURSOR);super.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (dateChooser = null)dateCh
7、ooser = new DateChooser();Point p = getLocationOnScreen();p.y = p.y + 30;dateChooser.showDateChooser(p););private static Date getNowDate() return Calendar.getInstance().getTime();private static SimpleDateFormatgetDefaultDateFormat() return new SimpleDateFormat(yyyy-MM-ddHH:MM:ss); /按钮显示的日期格式/覆盖父类的方法
8、public void setText(String s) Date date;try date = getDefaultDateFormat().parse(s); catch (ParseException e) date = getNowDate();setDate(date);public void setText(SimpleDateFormatdf, String s) Date date;try date = df.parse(s); catch (ParseException e) date = getNowDate();setDate(date);public void se
9、tDate(Date date) super.setText(preLabel + getDefaultDateFormat().format(date);public Date getDate() String dateString = getText().substring(preLabel.length();try return getDefaultDateFormat().parse(dateString); catch (ParseException e) return getNowDate();/覆盖父类的方法使之无效public void addActionListener(Ac
10、tionListener listener) private class DateChooser extends JPanel implements ActionListener,ChangeListener intstartYear = 1980; /默认【最小】显示年份intlastYear = 2050; /默认【最大】显示年份int width = 200; /界面宽度int height = 200; /界面高度Color backGroundColor = Color.gray; /底色/月历表格配色-/Color palletTableColor = Color.white; /
11、日历表底色Color todayBackColor = Color.orange; /今天背景色Color weekFontColor = Color.blue; /星期文字色Color dateFontColor = Color.black; /日期文字色Color weekendFontColor = Color.red; /周末文字色/控制条配色-/Color controlLineColor = Color.pink; /控制条底色Color controlTextColor = Color.white; /控制条标签文字色Color rbFontColor = Color.white
12、; /RoundBox文字色Color rbBorderColor = Color.red; /RoundBox边框色Color rbButtonColor = Color.pink; /RoundBox按钮色Color rbBtFontColor = Color.red; /RoundBox按钮文字色JDialog dialog;JSpinner yearSpin;JSpinner monthSpin;JSpinnerhourSpin;JButton daysButton = new JButton67;DateChooser() setLayout(new BorderLayout();s
13、etBorder(new LineBorder(backGroundColor, 2);setBackground(backGroundColor);JPaneltopYearAndMonth = createYearAndMonthPanal();add(topYearAndMonth, BorderLayout.NORTH);JPanelcenterWeekAndDay = createWeekAndDayPanal();add(centerWeekAndDay, BorderLayout.CENTER);privateJPanelcreateYearAndMonthPanal() Cal
14、endar c = getCalendar();intcurrentYear = c.get(Calendar.YEAR);intcurrentMonth = c.get(Calendar.MONTH) + 1;intcurrentHour = c.get(Calendar.HOUR_OF_DAY);JPanel result = new JPanel();result.setLayout(new FlowLayout();result.setBackground(controlLineColor);yearSpin = new JSpinner(new SpinnerNumberModel(
15、currentYear,startYear, lastYear, 1);yearSpin.setPreferredSize(new Dimension(48, 20);yearSpin.setName(Year);yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, #);yearSpin.addChangeListener(this);result.add(yearSpin);JLabelyearLabel = new JLabel(年);yearLabel.setForeground(controlTextColor);result.
16、add(yearLabel);monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,12, 1);monthSpin.setPreferredSize(new Dimension(35, 20);monthSpin.setName(Month);monthSpin.addChangeListener(this);result.add(monthSpin);JLabelmonthLabel = new JLabel(月);monthLabel.setForeground(controlTextColor);result.a
17、dd(monthLabel);hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,1);hourSpin.setPreferredSize(new Dimension(35, 20);hourSpin.setName(Hour);hourSpin.addChangeListener(this);result.add(hourSpin);JLabelhourLabel = new JLabel(时);hourLabel.setForeground(controlTextColor);result.add(hourLa
18、bel);return result;private JPanelcreateWeekAndDayPanal() String colname = 日, 一, 二, 三, 四, 五, 六 ;JPanel result = new JPanel();/设置固定字体,以免调用环境改变影响界面美观result.setFont(new Font(宋体, Font.PLAIN, 12);result.setLayout(new GridLayout(7, 7);result.setBackground(Color.white);JLabel cell;for (int i = 0; i 7; i+) c
19、ell = new JLabel(colnamei);cell.setHorizontalAlignment(JLabel.RIGHT);if (i = 0 | i = 6)cell.setForeground(weekendFontColor);elsecell.setForeground(weekFontColor);result.add(cell);intactionCommandId = 0;for (int i = 0; i 6; i+)for (int j = 0; j 0) result.x -= offsetX;if (offsetY 0) result.y -= offset
20、Y;return result;private Calendar getCalendar() Calendar result = Calendar.getInstance();result.setTime(getDate();return result;privateintgetSelectedYear() return (Integer) yearSpin.getValue().intValue();privateintgetSelectedMonth() return (Integer) monthSpin.getValue().intValue();privateintgetSelect
21、edHour() return (Integer) hourSpin.getValue().intValue();private void dayColorUpdate(booleanisOldDay) Calendar c = getCalendar();int day = c.get(Calendar.DAY_OF_MONTH);c.set(Calendar.DAY_OF_MONTH, 1);intactionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);int i = actionCommandId / 7;int j = actio
22、nCommandId % 7;if (isOldDay)daysButtonij.setForeground(dateFontColor);elsedaysButtonij.setForeground(todayBackColor);private void flushWeekAndDay() Calendar c = getCalendar();c.set(Calendar.DAY_OF_MONTH, 1);intmaxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);intdayNo = 2 - c.get(Calendar.DAY_OF_
23、WEEK);for (int i = 0; i 6; i+) for (int j = 0; j = 1 &dayNo= maxDayNo)s = String.valueOf(dayNo);daysButtonij.setText(s);dayNo+;dayColorUpdate(false);public void stateChanged(ChangeEvent e) JSpinner source = (JSpinner) e.getSource();Calendar c = getCalendar();if (source.getName().equals(Hour) c.set(Calendar.HOUR_OF_DAY, getSelectedHour();setDate(c.getTime();return;dayColorUpdate(true);if (source.getName().equals(Year)c.set(Calendar.YEAR, getSelectedYea
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1