Java-Swing单击文本框弹出日历组件

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JavaSwing单击文本框弹出日历组件2011-04-0716:12packagecom.niit.swing2;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.Frame;importjava.awt.GridLayout;importjava.awt.Point;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjavax.swing.JButton;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JFormattedTextField;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JSpinner;importjavax.swing.SpinnerNumberModel;importjavax.swing.SwingConstants;importjavax.swing.SwingUtilities;importjavax.swing.border.LineBorder;importjavax.swing.event.ChangeEvent;importjavax.swing.event.ChangeListener;publicclassDateChooserextendsJPanelimplementsActionListener,ChangeListener{/****/privatestaticfinallongserialVersionUID=1L;intstartYear=1980;//默认【最小】显示年份intlastYear=2050;//默认【最大】显示年份intwidth=270;//界面宽度intheight=200;//界面高度ColorbackGroundColor=Color.gray;//底色//月历表格配色----------------//ColorpalletTableColor=Color.white;//日历表底色ColortodayBackColor=Color.orange;//今天背景色ColorweekFontColor=Color.blue;//星期文字色ColordateFontColor=Color.black;//日期文字色ColorweekendFontColor=Color.red;//周末文字色//控制条配色------------------//ColorcontrolLineColor=Color.pink;//控制条底色ColorcontrolTextColor=Color.white;//控制条标签文字色ColorrbFontColor=Color.white;//RoundBox文字色ColorrbBorderColor=Color.red;//RoundBox边框色ColorrbButtonColor=Color.pink;//RoundBox按钮色ColorrbBtFontColor=Color.red;//RoundBox按钮文字色JDialogdialog;//用于显示日期选择控件JSpinneryearSpin;//调节年份的JSpinnerJSpinnermonthSpin;//调节月份JSpinnerhourSpin;//调节小时JSpinnerminuteSpin;//调节分钟JButton[][]daysButton=newJButton[6][7];//用于显示当前月份每一天所对应的星期的按钮数组JFormattedTextFieldjFormattedTextField;//显示当前选择日期的有格式输入框Calendarc=getCalendar();Calendarcal=Calendar.getInstance();intcurrentDay=cal.get(Calendar.DAY_OF_MONTH);DateChooser(JFormattedTextFieldjftf){jFormattedTextField=jftf;//设置布局及边框setLayout(newBorderLayout());setBorder(newLineBorder(backGroundColor,2));setBackground(backGroundColor);//初始化及添加子面板JPaneltopYearAndMonth=createYearAndMonthPanal();add(topYearAndMonth,BorderLayout.NORTH);JPanelcenterWeekAndDay=createWeekAndDayPanal();add(centerWeekAndDay,BorderLayout.CENTER);}/****功能描述:用于创建年份及月份显示面板*@return用于创建年份及月份显示面板JPanel对象*/privateJPanelcreateYearAndMonthPanal(){intcurrentYear=c.get(Calendar.YEAR);intcurrentMonth=c.get(Calendar.MONTH)+1;intcurrentHour=c.get(Calendar.HOUR_OF_DAY);intcurrentMinute=c.get(Calendar.MINUTE);JPanelresult=newJPanel();result.setLayout(newFlowLayout());result.setBackground(controlLineColor);yearSpin=newJSpinner(newSpinnerNumberModel(currentYear,startYear,lastYear,1));yearSpin.setPreferredSize(newDimension(48,20));yearSpin.setName(Year);yearSpin.setEditor(newJSpinner.NumberEditor(yearSpin,####));yearSpin.addChangeListener(this);result.add(yearSpin);JLabelyearLabel=newJLabel(年);yearLabel.setForeground(controlTextColor);result.add(yearLabel);monthSpin=newJSpinner(newSpinnerNumberModel(currentMonth,1,12,1));monthSpin.setPreferredSize(newDimension(35,20));monthSpin.setName(Month);monthSpin.addChangeListener(this);result.add(monthSpin);JLabelmonthLabel=newJLabel(月);monthLabel.setForeground(controlTextColor);result.add(monthLabel);hourSpin=newJSpinner(newSpinnerNumberModel(currentHour,0,23,1));hourSpin.setPreferredSize(newDimension(35,20));hourSpin.setName(Hour);hourSpin.addChangeListener(this);result.add(hourSpin);JLabelhourLabel=newJLabel(时);hourLabel.setForeground(controlTextColor);result.add(hourLabel);minuteSpin=newJSpinner(newSpinnerNumberModel(currentMinute,0,59,1));minuteSpin.setPreferredSize(newDimension(35,20));minuteSpin.setName(Minute);minuteSpin.addChangeListener(this);result.add(minuteSpin);JLabelminuteLabel=newJLabel(分);minuteLabel.setForeground(controlTextColor);result.add(minuteLabel);returnresult;}/****功能描述:用于创建当前月份中每一天按对应星期排列所在面板*@return*/privateJPanelcreateWeekAndDayPanal(){Stringcolname[]={日,一,二,三,四,五,六};JPanelresult=newJPanel();//设置固定字体,以免调用环境改变影响界面美观result.setFont(newFont(宋体,Font.PLAIN,12));result.setLayout(newGridLayout(7,7));result.setBackground(Color.white);JLabelcell;//显示星期for(inti=0;i7;i++){cell=newJLabel(colname[i]);cell.setHorizontalAlignment(JLabel.CENTER);if(i==0||i==6)cell.setForeground(weekendFontColor);elsecell.setForeground(weekFontColor);result.add(cell);}intactionCommandId=0;for(inti=0;i6;i++)for(intj=0;j7;j++){JButtonnumberButton=newJButton();numberButton.setBorder(null);numberButton.setHorizontalAlignment(SwingConstants.CENTER);numberButton.setActionCommand(String.valueOf(actionCommandId));numberButton.addActionListener(this);numberButton.setBackground(palletTableColor);numberButton.setForeground(dateFontColor);if(j==0||j==6)numberButton.setForeground(weekendFontColor);elsenumberButton.setForeground(dateFontColor);daysButton[i][j]=numberButton;numberButton.addMouseListener(newMouseAdapte

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功