ImageVerifierCode 换一换
格式:DOCX , 页数:9 ,大小:31.50KB ,
资源ID:11631344      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11631344.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(JAVA实现日期选择控件.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

JAVA实现日期选择控件.docx

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