posts - 8,  comments - 174,  trackbacks - 0

          此類是一個swing選擇日期控件。
          效果如下圖:
          ?????????????????????qq.bmp

          ??1 // ?日期按鈕類
          ??2 @SuppressWarnings( " serial " )
          ??3 public ? class ?DateChooserJButton? extends ?JButton? {
          ??4
          ??5 ???? private ?DateChooser?dateChooser;
          ??6 ????
          ??7 ???? private ?JTextComponent??com;
          ??8 ????
          ??9 ???? private ?String?preLabel? = ? "" ;
          ?10
          ?11 ???? public ? void ?setJTextComponent(JTextComponent?com) {
          ?12 ???????? this .com = com;
          ?13 ????}

          ?14 ????
          ?15 ???? public ?DateChooserJButton()? {
          ?16 ???????? this ( null ,?getNowDate(),? null );
          ?17 ????}

          ?18
          ?19 ???? public ?DateChooserJButton(String?text)? {
          ?20 ???????? this (text,?getNowDate(),? null );
          ?21 ????}

          ?22
          ?23 ???? public ?DateChooserJButton(Icon?icon)? {
          ?24 ???????? this ( null ,?getNowDate(),?icon);
          ?25 ????}

          ?26
          ?27 ???? public ?DateChooserJButton(Date?date)? {
          ?28 ???????? this ( null ,?date,? null );
          ?29 ????}

          ?30
          ?31 ???? public ?DateChooserJButton(String?text,?Date?date,?Icon?icon)? {
          ?32 ???????? if ?(text? != ? null )
          ?33 ???????????? super .setText(text);
          ?34 ???????? if ?(icon? != ? null )
          ?35 ???????????? super .setIcon(icon);
          ?36 ????????setBorder( null );
          ?37 ????????setCursor( new ?Cursor(Cursor.HAND_CURSOR));
          ?38 ???????? super .addActionListener( new ?ActionListener()? {
          ?39 ???????????? public ? void ?actionPerformed(ActionEvent?e)? {
          ?40 ???????????????? if ?(dateChooser? == ? null )
          ?41 ????????????????????dateChooser? = ? new ?DateChooser();
          ?42 ????????????????Point?p? = ?getLocationOnScreen();
          ?43 ????????????????p.y? = ?p.y? + ? 30 ;
          ?44 ????????????????dateChooser.showDateChooser(p);
          ?45 ????????????}

          ?46 ????????}
          );
          ?47 ????}

          ?48
          ?49 ???? public ?DateChooserJButton(SimpleDateFormat?df,?String?dateString)? {
          ?50 ???????? this ();
          ?51 ????????setText(df,?dateString);
          ?52 ????}

          ?53
          ?54 ???? private ? static ?Date?getNowDate()? {
          ?55 ???????? return ?Calendar.getInstance().getTime();
          ?56 ????}

          ?57
          ?58 ???? private ? static ?SimpleDateFormat?getDefaultDateFormat()? {
          ?59 ???????? return ? new ?SimpleDateFormat( " yyyy-MM-dd " );
          ?60 ????}

          ?61
          ?62 ???? public ? void ?setText(SimpleDateFormat?df,?String?s)? {
          ?63 ????????Date?date;
          ?64 ???????? try ? {
          ?65 ????????????date? = ?df.parse(s);
          ?66 ????????}
          ? catch ?(ParseException?e)? {
          ?67 ????????????date? = ?getNowDate();
          ?68 ????????}

          ?69 ????????setDate(date);
          ?70 ????}

          ?71
          ?72 ???? public ? void ?setDate(Date?date)? {
          ?73 ????????com.setText(getDefaultDateFormat().format(date));
          ?74 ????}

          ?75
          ?76 ???? public ?Date?getDate()? {
          ?77 ????????String?dateString? = ?getText().substring(preLabel.length());
          ?78 ???????? try ? {
          ?79 ???????????? return ?getDefaultDateFormat().parse(dateString);
          ?80 ????????}
          ? catch ?(ParseException?e)? {
          ?81 ???????????? return ?getNowDate();
          ?82 ????????}

          ?83 ????}

          ?84
          ?85 ???? // ?覆蓋父類的方法使之無效
          ?86 ???? public ? void ?addActionListener(ActionListener?listener)? {
          ?87 ????}

          ?88
          ?89 ???? private ? class ?DateChooser? extends ?JPanel? implements ?ActionListener,
          ?90 ????????????ChangeListener? {
          ?91 ???????? int ?startYear? = ? 1980 ;? // ?默認【最小】顯示年份
          ?92
          ?93 ???????? int ?lastYear? = ? 2050 ;? // ?默認【最大】顯示年份
          ?94
          ?95 ???????? int ?width? = ? 200 ;? // ?界面寬度
          ?96
          ?97 ???????? int ?height? = ? 200 ;? // ?界面高度
          ?98
          ?99 ????????Color?backGroundColor? = ?Color.gray;? // ?底色
          100
          101 ???????? // ?月歷表格配色---------------- //
          102
          103 ????????Color?palletTableColor? = ?Color.white;? // ?日歷表底色
          104
          105 ????????Color?todayBackColor? = ?Color.orange;? // ?今天背景色
          106
          107 ????????Color?weekFontColor? = ?Color.blue;? // ?星期文字色
          108
          109 ????????Color?dateFontColor? = ?Color.black;? // ?日期文字色
          110
          111 ????????Color?weekendFontColor? = ?Color.red;? // ?周末文字色
          112
          113 ???????? // ?控制條配色------------------ //
          114 ????????Color?controlLineColor? = ?Color.pink;? // ?控制條底色
          115
          116 ????????Color?controlTextColor? = ?Color.white;? // ?控制條標簽文字色
          117
          118 ????????Color?rbFontColor? = ?Color.white;? // ?RoundBox文字色
          119
          120 ????????Color?rbBorderColor? = ?Color.red;? // ?RoundBox邊框色
          121
          122 ????????Color?rbButtonColor? = ?Color.pink;? // ?RoundBox按鈕色
          123
          124 ????????Color?rbBtFontColor? = ?Color.red;? // ?RoundBox按鈕文字色
          125
          126 ????????JDialog?dialog;
          127
          128 ????????JSpinner?yearSpin;
          129
          130 ????????JSpinner?monthSpin;
          131
          132 ????????JSpinner?hourSpin;
          133
          134 ????????JButton[][]?daysButton? = ? new ?JButton[ 6 ][ 7 ];
          135
          136 ????????DateChooser()? {
          137
          138 ????????????setLayout( new ?BorderLayout());
          139 ????????????setBorder( new ?LineBorder(backGroundColor,? 2 ));
          140 ????????????setBackground(backGroundColor);
          141
          142 ????????????JPanel?topYearAndMonth? = ?createYearAndMonthPanal();
          143 ????????????add(topYearAndMonth,?BorderLayout.NORTH);
          144 ????????????JPanel?centerWeekAndDay? = ?createWeekAndDayPanal();
          145 ????????????add(centerWeekAndDay,?BorderLayout.CENTER);
          146
          147 ????????}

          148
          149 ???????? private ?JPanel?createYearAndMonthPanal()? {
          150 ????????????Calendar?c? = ?getCalendar();
          151 ???????????? int ?currentYear? = ?c.get(Calendar.YEAR);
          152 ???????????? int ?currentMonth? = ?c.get(Calendar.MONTH)? + ? 1 ;
          153 ???????????? int ?currentHour? = ?c.get(Calendar.HOUR_OF_DAY);
          154
          155 ????????????JPanel?result? = ? new ?JPanel();
          156 ????????????result.setLayout( new ?FlowLayout());
          157 ????????????result.setBackground(controlLineColor);
          158
          159 ????????????yearSpin? = ? new ?JSpinner( new ?SpinnerNumberModel(currentYear,
          160 ????????????????????startYear,?lastYear,? 1 ));
          161 ????????????yearSpin.setPreferredSize( new ?Dimension( 48 ,? 20 ));
          162 ????????????yearSpin.setName( " Year " );
          163 ????????????yearSpin.setEditor( new ?JSpinner.NumberEditor(yearSpin,? " #### " ));
          164 ????????????yearSpin.addChangeListener( this );
          165 ????????????result.add(yearSpin);
          166
          167 ????????????JLabel?yearLabel? = ? new ?JLabel( " " );
          168 ????????????yearLabel.setForeground(controlTextColor);
          169 ????????????result.add(yearLabel);
          170
          171 ????????????monthSpin? = ? new ?JSpinner( new ?SpinnerNumberModel(currentMonth,? 1 ,
          172 ???????????????????? 12 ,? 1 ));
          173 ????????????monthSpin.setPreferredSize( new ?Dimension( 35 ,? 20 ));
          174 ????????????monthSpin.setName( " Month " );
          175 ????????????monthSpin.addChangeListener( this );
          176 ????????????result.add(monthSpin);
          177
          178 ????????????JLabel?monthLabel? = ? new ?JLabel( " " );
          179 ????????????monthLabel.setForeground(controlTextColor);
          180 ????????????result.add(monthLabel);
          181
          182 ????????????hourSpin? = ? new ?JSpinner( new ?SpinnerNumberModel(currentHour,? 0 ,? 23 ,
          183 ???????????????????? 1 ));
          184 ????????????hourSpin.setPreferredSize( new ?Dimension( 35 ,? 20 ));
          185 ????????????hourSpin.setName( " Hour " );
          186 ????????????hourSpin.addChangeListener( this );
          187 ????????????result.add(hourSpin);
          188
          189 ????????????JLabel?hourLabel? = ? new ?JLabel( " " );
          190 ????????????hourLabel.setForeground(controlTextColor);
          191 ????????????result.add(hourLabel);
          192
          193 ???????????? return ?result;
          194 ????????}

          195
          196 ???????? private ?JPanel?createWeekAndDayPanal()? {
          197 ????????????String?colname[]? = ? {? " " ,? " " ,? " " ,? " " ,? " " ,? " " ,? " " ?} ;
          198 ????????????JPanel?result? = ? new ?JPanel();
          199 ???????????? // ?設置固定字體,以免調用環境改變影響界面美觀
          200 ????????????result.setFont( new ?Font( " 宋體 " ,?Font.PLAIN,? 12 ));
          201 ????????????result.setLayout( new ?GridLayout( 7 ,? 7 ));
          202 ????????????result.setBackground(Color.white);
          203 ????????????JLabel?cell;
          204
          205 ???????????? for ?( int ?i? = ? 0 ;?i? < ? 7 ;?i ++ )? {
          206 ????????????????cell? = ? new ?JLabel(colname[i]);
          207 ????????????????cell.setHorizontalAlignment(JLabel.RIGHT);
          208 ???????????????? if ?(i? == ? 0 ? || ?i? == ? 6 )
          209 ????????????????????cell.setForeground(weekendFontColor);
          210 ???????????????? else
          211 ????????????????????cell.setForeground(weekFontColor);
          212 ????????????????result.add(cell);
          213 ????????????}

          214
          215 ???????????? int ?actionCommandId? = ? 0 ;
          216 ???????????? for ?( int ?i? = ? 0 ;?i? < ? 6 ;?i ++ )
          217 ???????????????? for ?( int ?j? = ? 0 ;?j? < ? 7 ;?j ++ )? {
          218 ????????????????????JButton?numberButton? = ? new ?JButton();
          219 ????????????????????numberButton.setBorder( null );
          220 ????????????????????numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
          221 ????????????????????numberButton.setActionCommand(String
          222 ????????????????????????????.valueOf(actionCommandId));
          223 ????????????????????numberButton.addActionListener( this );
          224 ????????????????????numberButton.setBackground(palletTableColor);
          225 ????????????????????numberButton.setForeground(dateFontColor);
          226 ???????????????????? if ?(j? == ? 0 ? || ?j? == ? 6 )
          227 ????????????????????????numberButton.setForeground(weekendFontColor);
          228 ???????????????????? else
          229 ????????????????????????numberButton.setForeground(dateFontColor);
          230 ????????????????????daysButton[i][j]? = ?numberButton;
          231 ????????????????????result.add(numberButton);
          232 ????????????????????actionCommandId ++ ;
          233 ????????????????}

          234
          235 ???????????? return ?result;
          236 ????????}

          237
          238 ???????? private ?JDialog?createDialog(Frame?owner)? {
          239 ????????????JDialog?result? = ? new ?JDialog(owner,? " 日期時間選擇 " ,? true );
          240 ????????????result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
          241 ????????????result.getContentPane().add( this ,?BorderLayout.CENTER);
          242 ????????????result.pack();
          243 ????????????result.setSize(width,?height);
          244 ???????????? return ?result;
          245 ????????}

          246
          247 ????????@SuppressWarnings( " deprecation " )
          248 ???????? void ?showDateChooser(Point?position)? {
          249 ????????????Frame?owner? = ?(Frame)?SwingUtilities
          250 ????????????????????.getWindowAncestor(DateChooserJButton. this );
          251 ???????????? if ?(dialog? == ? null ? || ?dialog.getOwner()? != ?owner)
          252 ????????????????dialog? = ?createDialog(owner);
          253 ????????????dialog.setLocation(getAppropriateLocation(owner,?position));
          254 ????????????flushWeekAndDay();
          255 ????????????dialog.show();
          256 ????????}

          257
          258 ????????Point?getAppropriateLocation(Frame?owner,?Point?position)? {
          259 ????????????Point?result? = ? new ?Point(position);
          260 ????????????Point?p? = ?owner.getLocation();
          261 ???????????? int ?offsetX? = ?(position.x? + ?width)? - ?(p.x%
          posted on 2007-08-15 14:30 月月鳥 閱讀(6617) 評論(13)  編輯  收藏 所屬分類: J2SE

          FeedBack:
          # re: swing 選擇日期控件
          2007-08-15 15:18 | 冷面閻羅
          我之前做了一個
          你還可以加上農歷
          http://www.aygfsteel.com/soddabao/archive/2007/01/04/91729.html  回復  更多評論
            
          # re: swing 選擇日期控件
          2007-08-15 15:40 | 交口稱贊
          哈哈
          看來大家都喜歡寫啊
          我也寫了一個swt的,是下拉列表方式的。
            回復  更多評論
            
          # re: swing 選擇日期控件
          2007-08-15 21:33 | 交口稱贊
          你要考慮多語言
          星期頭不要寫死,最好從java對象里面取

          還要要考慮每周開始星期也是可以變的  回復  更多評論
            
          # re: swing 選擇日期控件
          2007-08-15 21:34 | 交口稱贊
          16時?
          是16日吧?  回復  更多評論
            
          # re: swing 選擇日期控件
          2007-10-18 10:47 | 333
          eee  回復  更多評論
            
          # re: swing 選擇日期控件
          2008-08-21 11:27 | Mini
          不全呀,請發全了好嗎  回復  更多評論
            
          # re: swing 選擇日期控件
          2008-08-21 11:30 | Mini
          請您給我發一份完全版的過來好嗎,ghybeyond@sina.com,謝謝了,急用  回復  更多評論
            
          # re: swing 選擇日期控件[未登錄]
          2009-06-05 11:01 | Jerry
          能不能把代碼發全? 如果可以的話發一份到我的郵箱jerry_zhou@139.com  回復  更多評論
            
          # re: swing 選擇日期控件[未登錄]
          2009-10-03 22:12 |
          能不能把代碼發全? 如果可以的話發一份到我的郵箱findsunny@126.com 謝謝  回復  更多評論
            
          # re: swing 選擇日期控件
          2012-03-02 14:40 | 游客
          你要么就別發,發一半算什么?炫耀??  回復  更多評論
            
          # re: swing 選擇日期控件
          2013-11-18 15:52 | 11222
          這個日期和星期不對應啊  回復  更多評論
            
          # re: swing 選擇日期控件
          2014-08-27 10:47 | zhangyi
          @交口稱贊 求下拉出時間的代碼!感激不盡  回復  更多評論
            
          # re: swing 選擇日期控件
          2014-08-27 10:52 | zhangyi
          @交口稱贊 郵箱arvinsw521403@163.com  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(5)

          隨筆分類(8)

          隨筆檔案(8)

          文章分類(1)

          文章檔案(1)

          其他

          娛樂

          技術

          搜索

          •  

          積分與排名

          • 積分 - 116294
          • 排名 - 503

          最新隨筆

          最新評論

          評論排行榜

          主站蜘蛛池模板: 航空| 昂仁县| 宝丰县| 阿克| 闵行区| 禹城市| 永福县| 称多县| 延边| 娱乐| 宣化县| 昭苏县| 县级市| 军事| 蓬安县| 邛崃市| 宁津县| 阜城县| 兴宁市| 通辽市| 崇州市| 康保县| 定陶县| 永寿县| 屯门区| 凌海市| 慈溪市| 九龙县| 房产| 视频| 岢岚县| 卓资县| 肇州县| 阜南县| 竹溪县| 治县。| 阳新县| 淮阳县| 鄂尔多斯市| 江门市| 赣州市|