`
a137268431
  • 浏览: 157597 次
文章分类
社区版块
存档分类
最新评论

DateChooser日期选择器swing

 
阅读更多

网上找的,不错的日期选择器,在此分享给大家。

Java代码收藏代码
  1. packagecn.xcu.ui;
  2. importjava.awt.BasicStroke;
  3. importjava.awt.BorderLayout;
  4. importjava.awt.Color;
  5. importjava.awt.Component;
  6. importjava.awt.Cursor;
  7. importjava.awt.Dimension;
  8. importjava.awt.Font;
  9. importjava.awt.Graphics;
  10. importjava.awt.Graphics2D;
  11. importjava.awt.GridLayout;
  12. importjava.awt.Point;
  13. importjava.awt.Polygon;
  14. importjava.awt.Stroke;
  15. importjava.awt.Toolkit;
  16. importjava.awt.event.FocusEvent;
  17. importjava.awt.event.FocusListener;
  18. importjava.awt.event.MouseAdapter;
  19. importjava.awt.event.MouseEvent;
  20. importjava.awt.event.MouseListener;
  21. importjava.awt.event.MouseMotionListener;
  22. importjava.text.SimpleDateFormat;
  23. importjava.util.ArrayList;
  24. importjava.util.Calendar;
  25. importjava.util.Comparator;
  26. importjava.util.Date;
  27. importjava.util.List;
  28. importjavax.swing.BorderFactory;
  29. importjavax.swing.JButton;
  30. importjavax.swing.JFrame;
  31. importjavax.swing.JLabel;
  32. importjavax.swing.JPanel;
  33. importjavax.swing.Popup;
  34. importjavax.swing.PopupFactory;
  35. importjavax.swing.SwingUtilities;
  36. importjavax.swing.event.AncestorEvent;
  37. importjavax.swing.event.AncestorListener;
  38. /**
  39. *日期选择器
  40. */
  41. publicclassDateChooserextendsJPanel{
  42. privatestaticfinallongserialVersionUID=4529266044762990227L;
  43. privateDateinitDate;
  44. privateCalendarnow=Calendar.getInstance();
  45. privateCalendarselect;
  46. privateJPanelmonthPanel;//月历
  47. privateJP1jp1;//四块面板,组成
  48. privateJP2jp2;
  49. privateJP3jp3;
  50. privateJP4jp4;
  51. privateFontfont=newFont("宋体",Font.PLAIN,12);
  52. privatefinalLabelManagerlm=newLabelManager();
  53. privateJLabelshowDate;//,toSelect;
  54. privateSimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
  55. privatebooleanisShow=false;
  56. privatePopuppop;
  57. /**
  58. *CreatesanewinstanceofDateChooser
  59. */
  60. publicDateChooser(){
  61. this(newDate());
  62. }
  63. publicDateChooser(Datedate){
  64. initDate=date;
  65. select=Calendar.getInstance();
  66. select.setTime(initDate);
  67. initPanel();
  68. initLabel();
  69. }
  70. publicvoidsetEnabled(booleanb){
  71. super.setEnabled(b);
  72. showDate.setEnabled(b);
  73. }
  74. /**
  75. *得到当前选择框的日期
  76. */
  77. publicDategetDate(){
  78. returnselect.getTime();
  79. }
  80. //根据初始化的日期,初始化面板
  81. privatevoidinitPanel(){
  82. monthPanel=newJPanel(newBorderLayout());
  83. monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
  84. JPanelup=newJPanel(newBorderLayout());
  85. up.add(jp1=newJP1(),BorderLayout.NORTH);
  86. up.add(jp2=newJP2(),BorderLayout.CENTER);
  87. monthPanel.add(jp3=newJP3(),BorderLayout.CENTER);
  88. monthPanel.add(up,BorderLayout.NORTH);
  89. monthPanel.add(jp4=newJP4(),BorderLayout.SOUTH);
  90. this.addAncestorListener(newAncestorListener(){
  91. publicvoidancestorAdded(AncestorEventevent){
  92. }
  93. publicvoidancestorRemoved(AncestorEventevent){
  94. }
  95. //只要祖先组件一移动,马上就让popup消失
  96. publicvoidancestorMoved(AncestorEventevent){
  97. hidePanel();
  98. }
  99. });
  100. }
  101. //初始化标签
  102. privatevoidinitLabel(){
  103. showDate=newJLabel(sdf.format(initDate));
  104. showDate.setRequestFocusEnabled(true);
  105. showDate.addMouseListener(newMouseAdapter(){
  106. publicvoidmousePressed(MouseEventme){
  107. showDate.requestFocusInWindow();
  108. }
  109. });
  110. //toSelect=newJLabel(sdf.format(initDate));
  111. //toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  112. //toSelect.setRequestFocusEnabled(true);
  113. this.setBackground(Color.WHITE);
  114. this.add(showDate,BorderLayout.CENTER);
  115. //this.add(toSelect,BorderLayout.EAST);
  116. this.setPreferredSize(newDimension(90,25));
  117. this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  118. showDate.addMouseListener(newMouseAdapter(){
  119. publicvoidmouseEntered(MouseEventme){
  120. if(showDate.isEnabled()){
  121. showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
  122. showDate.setForeground(Color.RED);
  123. }
  124. }
  125. publicvoidmouseExited(MouseEventme){
  126. if(showDate.isEnabled()){
  127. showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  128. showDate.setForeground(Color.BLACK);
  129. }
  130. }
  131. publicvoidmousePressed(MouseEventme){
  132. if(showDate.isEnabled()){
  133. showDate.setForeground(Color.CYAN);
  134. if(isShow){
  135. hidePanel();
  136. }else{
  137. showPanel(showDate);
  138. }
  139. }
  140. }
  141. publicvoidmouseReleased(MouseEventme){
  142. if(showDate.isEnabled()){
  143. showDate.setForeground(Color.BLACK);
  144. }
  145. }
  146. });
  147. showDate.addFocusListener(newFocusListener(){
  148. publicvoidfocusLost(FocusEvente){
  149. hidePanel();
  150. }
  151. publicvoidfocusGained(FocusEvente){
  152. }
  153. });
  154. }
  155. //根据新的日期刷新
  156. privatevoidrefresh(){
  157. jp1.updateDate();
  158. jp3.updateDate();
  159. SwingUtilities.updateComponentTreeUI(this);
  160. }
  161. //提交日期
  162. privatevoidcommit(){
  163. System.out.println("选中的日期是:"+sdf.format(select.getTime()));
  164. showDate.setText(sdf.format(select.getTime()));
  165. hidePanel();
  166. }
  167. //隐藏日期选择面板
  168. privatevoidhidePanel(){
  169. if(pop!=null){
  170. isShow=false;
  171. pop.hide();
  172. pop=null;
  173. }
  174. }
  175. //显示日期选择面板
  176. privatevoidshowPanel(Componentowner){
  177. if(pop!=null){
  178. pop.hide();
  179. }
  180. Pointshow=newPoint(0,showDate.getHeight());
  181. SwingUtilities.convertPointToScreen(show,showDate);
  182. Dimensionsize=Toolkit.getDefaultToolkit().getScreenSize();
  183. intx=show.x;
  184. inty=show.y;
  185. if(x<0){
  186. x=0;
  187. }
  188. if(x>size.width-295){
  189. x=size.width-295;
  190. }
  191. if(y<size.height-170){
  192. }else{
  193. y-=188;
  194. }
  195. pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
  196. pop.show();
  197. isShow=true;
  198. }
  199. /**
  200. *最上面的面板用来显示月份的增减
  201. */
  202. privateclassJP1extendsJPanel{
  203. JLabelleft,right,center;
  204. publicJP1(){
  205. super(newBorderLayout());
  206. this.setBackground(newColor(160,185,215));
  207. initJP1();
  208. }
  209. privatevoidinitJP1(){
  210. left=newJLabel("<<",JLabel.CENTER);
  211. left.setToolTipText("上一月");
  212. right=newJLabel(">>",JLabel.CENTER);
  213. right.setToolTipText("下一月");
  214. left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
  215. right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));
  216. center=newJLabel("",JLabel.CENTER);
  217. updateDate();
  218. this.add(left,BorderLayout.WEST);
  219. this.add(center,BorderLayout.CENTER);
  220. this.add(right,BorderLayout.EAST);
  221. this.setPreferredSize(newDimension(295,25));
  222. left.addMouseListener(newMouseAdapter(){
  223. publicvoidmouseEntered(MouseEventme){
  224. left.setCursor(newCursor(Cursor.HAND_CURSOR));
  225. left.setForeground(Color.RED);
  226. }
  227. publicvoidmouseExited(MouseEventme){
  228. left.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  229. left.setForeground(Color.BLACK);
  230. }
  231. publicvoidmousePressed(MouseEventme){
  232. select.add(Calendar.MONTH,-1);
  233. left.setForeground(Color.WHITE);
  234. refresh();
  235. }
  236. publicvoidmouseReleased(MouseEventme){
  237. left.setForeground(Color.BLACK);
  238. }
  239. });
  240. right.addMouseListener(newMouseAdapter(){
  241. publicvoidmouseEntered(MouseEventme){
  242. right.setCursor(newCursor(Cursor.HAND_CURSOR));
  243. right.setForeground(Color.RED);
  244. }
  245. publicvoidmouseExited(MouseEventme){
  246. right.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  247. right.setForeground(Color.BLACK);
  248. }
  249. publicvoidmousePressed(MouseEventme){
  250. select.add(Calendar.MONTH,1);
  251. right.setForeground(Color.WHITE);
  252. refresh();
  253. }
  254. publicvoidmouseReleased(MouseEventme){
  255. right.setForeground(Color.BLACK);
  256. }
  257. });
  258. }
  259. privatevoidupdateDate(){
  260. center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
  261. }
  262. }
  263. privateclassJP2extendsJPanel{
  264. publicJP2(){
  265. this.setPreferredSize(newDimension(295,20));
  266. }
  267. protectedvoidpaintComponent(Graphicsg){
  268. g.setFont(font);
  269. g.drawString("星期日星期一星期二星期三星期四星期五星期六",5,10);
  270. g.drawLine(0,15,getWidth(),15);
  271. }
  272. }
  273. privateclassJP3extendsJPanel{
  274. publicJP3(){
  275. super(newGridLayout(6,7));
  276. this.setPreferredSize(newDimension(295,100));
  277. initJP3();
  278. }
  279. privatevoidinitJP3(){
  280. updateDate();
  281. }
  282. publicvoidupdateDate(){
  283. this.removeAll();
  284. lm.clear();
  285. Datetemp=select.getTime();
  286. Calendarselect=Calendar.getInstance();
  287. select.setTime(temp);
  288. select.set(Calendar.DAY_OF_MONTH,1);
  289. intindex=select.get(Calendar.DAY_OF_WEEK);
  290. intsum=(index==1?8:index);
  291. select.add(Calendar.DAY_OF_MONTH,0-sum);
  292. for(inti=0;i<42;i++){
  293. select.add(Calendar.DAY_OF_MONTH,1);
  294. lm.addLabel(newMyLabel(select.get(Calendar.YEAR),
  295. select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));
  296. }
  297. for(MyLabelmy:lm.getLabels()){
  298. this.add(my);
  299. }
  300. select.setTime(temp);
  301. }
  302. }
  303. privateclassMyLabelextendsJLabelimplementsComparator<MyLabel>,
  304. MouseListener,MouseMotionListener{
  305. privateintyear,month,day;
  306. privatebooleanisSelected;
  307. publicMyLabel(intyear,intmonth,intday){
  308. super(""+day,JLabel.CENTER);
  309. this.year=year;
  310. this.day=day;
  311. this.month=month;
  312. this.addMouseListener(this);
  313. this.addMouseMotionListener(this);
  314. this.setFont(font);
  315. if(month==select.get(Calendar.MONTH)){
  316. this.setForeground(Color.BLACK);
  317. }else{
  318. this.setForeground(Color.LIGHT_GRAY);
  319. }
  320. if(day==select.get(Calendar.DAY_OF_MONTH)){
  321. this.setBackground(newColor(160,185,215));
  322. }else{
  323. this.setBackground(Color.WHITE);
  324. }
  325. }
  326. publicbooleangetIsSelected(){
  327. returnisSelected;
  328. }
  329. publicvoidsetSelected(booleanb,booleanisDrag){
  330. isSelected=b;
  331. if(b&&!isDrag){
  332. inttemp=select.get(Calendar.MONTH);
  333. select.set(year,month,day);
  334. if(temp==month){
  335. SwingUtilities.updateComponentTreeUI(jp3);
  336. }else{
  337. refresh();
  338. }
  339. }
  340. this.repaint();
  341. }
  342. protectedvoidpaintComponent(Graphicsg){
  343. if(day==select.get(Calendar.DAY_OF_MONTH)&&
  344. month==select.get(Calendar.MONTH)){
  345. //如果当前日期是选择日期,则高亮显示
  346. g.setColor(newColor(160,185,215));
  347. g.fillRect(0,0,getWidth(),getHeight());
  348. }
  349. if(year==now.get(Calendar.YEAR)&&
  350. month==now.get(Calendar.MONTH)&&
  351. day==now.get(Calendar.DAY_OF_MONTH)){
  352. //如果日期和当前日期一样,则用红框
  353. Graphics2Dgd=(Graphics2D)g;
  354. gd.setColor(Color.RED);
  355. Polygonp=newPolygon();
  356. p.addPoint(0,0);
  357. p.addPoint(getWidth()-1,0);
  358. p.addPoint(getWidth()-1,getHeight()-1);
  359. p.addPoint(0,getHeight()-1);
  360. gd.drawPolygon(p);
  361. }
  362. if(isSelected){//如果被选中了就画出一个虚线框出来
  363. Strokes=newBasicStroke(1.0f,BasicStroke.CAP_SQUARE,
  364. BasicStroke.JOIN_BEVEL,1.0f,newfloat[]{2.0f,2.0f},1.0f);
  365. Graphics2Dgd=(Graphics2D)g;
  366. gd.setStroke(s);
  367. gd.setColor(Color.BLACK);
  368. Polygonp=newPolygon();
  369. p.addPoint(0,0);
  370. p.addPoint(getWidth()-1,0);
  371. p.addPoint(getWidth()-1,getHeight()-1);
  372. p.addPoint(0,getHeight()-1);
  373. gd.drawPolygon(p);
  374. }
  375. super.paintComponent(g);
  376. }
  377. publicbooleancontains(Pointp){
  378. returnthis.getBounds().contains(p);
  379. }
  380. privatevoidupdate(){
  381. repaint();
  382. }
  383. publicvoidmouseClicked(MouseEvente){
  384. }
  385. publicvoidmousePressed(MouseEvente){
  386. isSelected=true;
  387. update();
  388. }
  389. publicvoidmouseReleased(MouseEvente){
  390. Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
  391. lm.setSelect(p,false);
  392. commit();
  393. }
  394. publicvoidmouseEntered(MouseEvente){
  395. }
  396. publicvoidmouseExited(MouseEvente){
  397. }
  398. publicvoidmouseDragged(MouseEvente){
  399. Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
  400. lm.setSelect(p,true);
  401. }
  402. publicvoidmouseMoved(MouseEvente){
  403. }
  404. publicintcompare(MyLabelo1,MyLabelo2){
  405. Calendarc1=Calendar.getInstance();
  406. c1.set(o1.year,o2.month,o1.day);
  407. Calendarc2=Calendar.getInstance();
  408. c2.set(o2.year,o2.month,o2.day);
  409. returnc1.compareTo(c2);
  410. }
  411. }
  412. privateclassLabelManager{
  413. privateList<MyLabel>list;
  414. publicLabelManager(){
  415. list=newArrayList<MyLabel>();
  416. }
  417. publicList<MyLabel>getLabels(){
  418. returnlist;
  419. }
  420. publicvoidaddLabel(MyLabelmy){
  421. list.add(my);
  422. }
  423. publicvoidclear(){
  424. list.clear();
  425. }
  426. publicvoidsetSelect(MyLabelmy,booleanb){
  427. for(MyLabelm:list){
  428. if(m.equals(my)){
  429. m.setSelected(true,b);
  430. }else{
  431. m.setSelected(false,b);
  432. }
  433. }
  434. }
  435. publicvoidsetSelect(Pointp,booleanb){
  436. //如果是拖动,则要优化一下,以提高效率
  437. if(b){
  438. //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和
  439. //将要显示的标签找到了就可以了
  440. booleanfindPrevious=false,findNext=false;
  441. for(MyLabelm:list){
  442. if(m.contains(p)){
  443. findNext=true;
  444. if(m.getIsSelected()){
  445. findPrevious=true;
  446. }else{
  447. m.setSelected(true,b);
  448. }
  449. }elseif(m.getIsSelected()){
  450. findPrevious=true;
  451. m.setSelected(false,b);
  452. }
  453. if(findPrevious&&findNext){
  454. return;
  455. }
  456. }
  457. }else{
  458. MyLabeltemp=null;
  459. for(MyLabelm:list){
  460. if(m.contains(p)){
  461. temp=m;
  462. }elseif(m.getIsSelected()){
  463. m.setSelected(false,b);
  464. }
  465. }
  466. if(temp!=null){
  467. temp.setSelected(true,b);
  468. }
  469. }
  470. }
  471. }
  472. privateclassJP4extendsJPanel{
  473. publicJP4(){
  474. super(newBorderLayout());
  475. this.setPreferredSize(newDimension(295,20));
  476. this.setBackground(newColor(160,185,215));
  477. SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
  478. finalJLabeljl=newJLabel("今天:"+sdf.format(newDate()));
  479. jl.setToolTipText("点击回到今天日期");
  480. this.add(jl,BorderLayout.CENTER);
  481. jl.addMouseListener(newMouseAdapter(){
  482. publicvoidmouseEntered(MouseEventme){
  483. jl.setCursor(newCursor(Cursor.HAND_CURSOR));
  484. jl.setForeground(Color.RED);
  485. }
  486. publicvoidmouseExited(MouseEventme){
  487. jl.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  488. jl.setForeground(Color.BLACK);
  489. }
  490. publicvoidmousePressed(MouseEventme){
  491. jl.setForeground(Color.WHITE);
  492. select.setTime(newDate());
  493. refresh();
  494. commit();
  495. }
  496. publicvoidmouseReleased(MouseEventme){
  497. jl.setForeground(Color.BLACK);
  498. }
  499. });
  500. }
  501. }
  502. publicstaticvoidmain(String[]args){
  503. finalDateChoosermp=newDateChooser();
  504. JFramejf=newJFrame("test");
  505. jf.add(mp,BorderLayout.CENTER);
  506. jf.add(newJButton("测试用的"),BorderLayout.NORTH);
  507. jf.pack();
  508. jf.setLocationRelativeTo(null);
  509. jf.setVisible(true);
  510. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  511. }
  512. }

补充,上面是直接从网上拷的,没有“年”的增减功能,下面是我补充完整的代码.

1,增加“年”的增减操作。

2,增加指定日期格式的构造方法。

与大家分享一下。

Java代码收藏代码
  1. packagecn.xcu.ui;
  2. importjava.awt.BasicStroke;
  3. importjava.awt.BorderLayout;
  4. importjava.awt.Color;
  5. importjava.awt.Component;
  6. importjava.awt.Cursor;
  7. importjava.awt.Dimension;
  8. importjava.awt.Font;
  9. importjava.awt.Graphics;
  10. importjava.awt.Graphics2D;
  11. importjava.awt.GridLayout;
  12. importjava.awt.Point;
  13. importjava.awt.Polygon;
  14. importjava.awt.Stroke;
  15. importjava.awt.Toolkit;
  16. importjava.awt.event.FocusEvent;
  17. importjava.awt.event.FocusListener;
  18. importjava.awt.event.MouseAdapter;
  19. importjava.awt.event.MouseEvent;
  20. importjava.awt.event.MouseListener;
  21. importjava.awt.event.MouseMotionListener;
  22. importjava.text.SimpleDateFormat;
  23. importjava.util.ArrayList;
  24. importjava.util.Calendar;
  25. importjava.util.Comparator;
  26. importjava.util.Date;
  27. importjava.util.List;
  28. importjavax.swing.BorderFactory;
  29. importjavax.swing.JButton;
  30. importjavax.swing.JFrame;
  31. importjavax.swing.JLabel;
  32. importjavax.swing.JPanel;
  33. importjavax.swing.Popup;
  34. importjavax.swing.PopupFactory;
  35. importjavax.swing.SwingUtilities;
  36. importjavax.swing.event.AncestorEvent;
  37. importjavax.swing.event.AncestorListener;
  38. /**
  39. *日期选择器,可以指定日期的显示格式
  40. */
  41. publicclassDateChooserextendsJPanel{
  42. privatestaticfinallongserialVersionUID=4529266044762990227L;
  43. privateDateinitDate;
  44. privateCalendarnow=Calendar.getInstance();
  45. privateCalendarselect;
  46. privateJPanelmonthPanel;//月历
  47. privateJP1jp1;//四块面板,组成
  48. privateJP2jp2;
  49. privateJP3jp3;
  50. privateJP4jp4;
  51. privateFontfont=newFont("宋体",Font.PLAIN,12);
  52. privatefinalLabelManagerlm=newLabelManager();
  53. privateJLabelshowDate;//,toSelect;
  54. privateSimpleDateFormatsdf;
  55. privatebooleanisShow=false;
  56. privatePopuppop;
  57. /**
  58. *CreatesanewinstanceofDateChooser
  59. */
  60. publicDateChooser(){
  61. this(newDate());
  62. }
  63. publicDateChooser(Datedate){
  64. this(date,"yyyy年MM月dd日");
  65. }
  66. publicDateChooser(Stringformat){
  67. this(newDate(),format);
  68. }
  69. publicDateChooser(Datedate,Stringformat){
  70. initDate=date;
  71. sdf=newSimpleDateFormat(format);
  72. select=Calendar.getInstance();
  73. select.setTime(initDate);
  74. initPanel();
  75. initLabel();
  76. }
  77. /**
  78. *是否允许用户选择
  79. */
  80. publicvoidsetEnabled(booleanb){
  81. super.setEnabled(b);
  82. showDate.setEnabled(b);
  83. }
  84. /**
  85. *得到当前选择框的日期
  86. */
  87. publicDategetDate(){
  88. returnselect.getTime();
  89. }
  90. //根据初始化的日期,初始化面板
  91. privatevoidinitPanel(){
  92. monthPanel=newJPanel(newBorderLayout());
  93. monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
  94. JPanelup=newJPanel(newBorderLayout());
  95. up.add(jp1=newJP1(),BorderLayout.NORTH);
  96. up.add(jp2=newJP2(),BorderLayout.CENTER);
  97. monthPanel.add(jp3=newJP3(),BorderLayout.CENTER);
  98. monthPanel.add(up,BorderLayout.NORTH);
  99. monthPanel.add(jp4=newJP4(),BorderLayout.SOUTH);
  100. this.addAncestorListener(newAncestorListener(){
  101. publicvoidancestorAdded(AncestorEventevent){
  102. }
  103. publicvoidancestorRemoved(AncestorEventevent){
  104. }
  105. //只要祖先组件一移动,马上就让popup消失
  106. publicvoidancestorMoved(AncestorEventevent){
  107. hidePanel();
  108. }
  109. });
  110. }
  111. //初始化标签
  112. privatevoidinitLabel(){
  113. showDate=newJLabel(sdf.format(initDate));
  114. showDate.setRequestFocusEnabled(true);
  115. showDate.addMouseListener(newMouseAdapter(){
  116. publicvoidmousePressed(MouseEventme){
  117. showDate.requestFocusInWindow();
  118. }
  119. });
  120. //toSelect=newJLabel(sdf.format(initDate));
  121. //toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  122. //toSelect.setRequestFocusEnabled(true);
  123. this.setBackground(Color.WHITE);
  124. this.add(showDate,BorderLayout.CENTER);
  125. //this.add(toSelect,BorderLayout.EAST);
  126. this.setPreferredSize(newDimension(90,25));
  127. this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  128. showDate.addMouseListener(newMouseAdapter(){
  129. publicvoidmouseEntered(MouseEventme){
  130. if(showDate.isEnabled()){
  131. showDate.setCursor(newCursor(Cursor.HAND_CURSOR));
  132. showDate.setForeground(Color.RED);
  133. }
  134. }
  135. publicvoidmouseExited(MouseEventme){
  136. if(showDate.isEnabled()){
  137. showDate.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  138. showDate.setForeground(Color.BLACK);
  139. }
  140. }
  141. publicvoidmousePressed(MouseEventme){
  142. if(showDate.isEnabled()){
  143. showDate.setForeground(Color.CYAN);
  144. if(isShow){
  145. hidePanel();
  146. }else{
  147. showPanel(showDate);
  148. }
  149. }
  150. }
  151. publicvoidmouseReleased(MouseEventme){
  152. if(showDate.isEnabled()){
  153. showDate.setForeground(Color.BLACK);
  154. }
  155. }
  156. });
  157. showDate.addFocusListener(newFocusListener(){
  158. publicvoidfocusLost(FocusEvente){
  159. hidePanel();
  160. }
  161. publicvoidfocusGained(FocusEvente){
  162. }
  163. });
  164. }
  165. //根据新的日期刷新
  166. privatevoidrefresh(){
  167. jp1.updateDate();
  168. jp3.updateDate();
  169. SwingUtilities.updateComponentTreeUI(this);
  170. }
  171. //提交日期
  172. privatevoidcommit(){
  173. System.out.println("选中的日期是:"+sdf.format(select.getTime()));
  174. showDate.setText(sdf.format(select.getTime()));
  175. hidePanel();
  176. }
  177. //隐藏日期选择面板
  178. privatevoidhidePanel(){
  179. if(pop!=null){
  180. isShow=false;
  181. pop.hide();
  182. pop=null;
  183. }
  184. }
  185. //显示日期选择面板
  186. privatevoidshowPanel(Componentowner){
  187. if(pop!=null){
  188. pop.hide();
  189. }
  190. Pointshow=newPoint(0,showDate.getHeight());
  191. SwingUtilities.convertPointToScreen(show,showDate);
  192. Dimensionsize=Toolkit.getDefaultToolkit().getScreenSize();
  193. intx=show.x;
  194. inty=show.y;
  195. if(x<0){
  196. x=0;
  197. }
  198. if(x>size.width-295){
  199. x=size.width-295;
  200. }
  201. if(y<size.height-170){
  202. }else{
  203. y-=188;
  204. }
  205. pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);
  206. pop.show();
  207. isShow=true;
  208. }
  209. /**
  210. *最上面的面板用来显示月份的增减
  211. */
  212. privateclassJP1extendsJPanel{
  213. JLabelyearleft,yearright,monthleft,monthright,center,centercontainer;
  214. publicJP1(){
  215. super(newBorderLayout());
  216. this.setBackground(newColor(160,185,215));
  217. initJP1();
  218. }
  219. privatevoidinitJP1(){
  220. yearleft=newJLabel("<<",JLabel.CENTER);
  221. yearleft.setToolTipText("上一年");
  222. yearright=newJLabel(">>",JLabel.CENTER);
  223. yearright.setToolTipText("下一年");
  224. yearleft.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
  225. yearright.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
  226. monthleft=newJLabel("<",JLabel.RIGHT);
  227. monthleft.setToolTipText("上一月");
  228. monthright=newJLabel(">",JLabel.LEFT);
  229. monthright.setToolTipText("下一月");
  230. monthleft.setBorder(BorderFactory.createEmptyBorder(2,30,0,0));
  231. monthright.setBorder(BorderFactory.createEmptyBorder(2,0,0,30));
  232. centercontainer=newJLabel("",JLabel.CENTER);
  233. centercontainer.setLayout(newBorderLayout());
  234. center=newJLabel("",JLabel.CENTER);
  235. centercontainer.add(monthleft,BorderLayout.WEST);
  236. centercontainer.add(center,BorderLayout.CENTER);
  237. centercontainer.add(monthright,BorderLayout.EAST);
  238. this.add(yearleft,BorderLayout.WEST);
  239. this.add(centercontainer,BorderLayout.CENTER);
  240. this.add(yearright,BorderLayout.EAST);
  241. this.setPreferredSize(newDimension(295,25));
  242. updateDate();
  243. yearleft.addMouseListener(newMouseAdapter(){
  244. publicvoidmouseEntered(MouseEventme){
  245. yearleft.setCursor(newCursor(Cursor.HAND_CURSOR));
  246. yearleft.setForeground(Color.RED);
  247. }
  248. publicvoidmouseExited(MouseEventme){
  249. yearleft.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  250. yearleft.setForeground(Color.BLACK);
  251. }
  252. publicvoidmousePressed(MouseEventme){
  253. select.add(Calendar.YEAR,-1);
  254. yearleft.setForeground(Color.WHITE);
  255. refresh();
  256. }
  257. publicvoidmouseReleased(MouseEventme){
  258. yearleft.setForeground(Color.BLACK);
  259. }
  260. });
  261. yearright.addMouseListener(newMouseAdapter(){
  262. publicvoidmouseEntered(MouseEventme){
  263. yearright.setCursor(newCursor(Cursor.HAND_CURSOR));
  264. yearright.setForeground(Color.RED);
  265. }
  266. publicvoidmouseExited(MouseEventme){
  267. yearright.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  268. yearright.setForeground(Color.BLACK);
  269. }
  270. publicvoidmousePressed(MouseEventme){
  271. select.add(Calendar.YEAR,1);
  272. yearright.setForeground(Color.WHITE);
  273. refresh();
  274. }
  275. publicvoidmouseReleased(MouseEventme){
  276. yearright.setForeground(Color.BLACK);
  277. }
  278. });
  279. monthleft.addMouseListener(newMouseAdapter(){
  280. publicvoidmouseEntered(MouseEventme){
  281. monthleft.setCursor(newCursor(Cursor.HAND_CURSOR));
  282. monthleft.setForeground(Color.RED);
  283. }
  284. publicvoidmouseExited(MouseEventme){
  285. monthleft.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  286. monthleft.setForeground(Color.BLACK);
  287. }
  288. publicvoidmousePressed(MouseEventme){
  289. select.add(Calendar.MONTH,-1);
  290. monthleft.setForeground(Color.WHITE);
  291. refresh();
  292. }
  293. publicvoidmouseReleased(MouseEventme){
  294. monthleft.setForeground(Color.BLACK);
  295. }
  296. });
  297. monthright.addMouseListener(newMouseAdapter(){
  298. publicvoidmouseEntered(MouseEventme){
  299. monthright.setCursor(newCursor(Cursor.HAND_CURSOR));
  300. monthright.setForeground(Color.RED);
  301. }
  302. publicvoidmouseExited(MouseEventme){
  303. monthright.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  304. monthright.setForeground(Color.BLACK);
  305. }
  306. publicvoidmousePressed(MouseEventme){
  307. select.add(Calendar.MONTH,1);
  308. monthright.setForeground(Color.WHITE);
  309. refresh();
  310. }
  311. publicvoidmouseReleased(MouseEventme){
  312. monthright.setForeground(Color.BLACK);
  313. }
  314. });
  315. }
  316. privatevoidupdateDate(){
  317. center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
  318. }
  319. }
  320. privateclassJP2extendsJPanel{
  321. publicJP2(){
  322. this.setPreferredSize(newDimension(295,20));
  323. }
  324. protectedvoidpaintComponent(Graphicsg){
  325. g.setFont(font);
  326. g.drawString("星期日星期一星期二星期三星期四星期五星期六",5,10);
  327. g.drawLine(0,15,getWidth(),15);
  328. }
  329. }
  330. privateclassJP3extendsJPanel{
  331. publicJP3(){
  332. super(newGridLayout(6,7));
  333. this.setPreferredSize(newDimension(295,100));
  334. initJP3();
  335. }
  336. privatevoidinitJP3(){
  337. updateDate();
  338. }
  339. publicvoidupdateDate(){
  340. this.removeAll();
  341. lm.clear();
  342. Datetemp=select.getTime();
  343. Calendarselect=Calendar.getInstance();
  344. select.setTime(temp);
  345. select.set(Calendar.DAY_OF_MONTH,1);
  346. intindex=select.get(Calendar.DAY_OF_WEEK);
  347. intsum=(index==1?8:index);
  348. select.add(Calendar.DAY_OF_MONTH,0-sum);
  349. for(inti=0;i<42;i++){
  350. select.add(Calendar.DAY_OF_MONTH,1);
  351. lm.addLabel(newMyLabel(select.get(Calendar.YEAR),
  352. select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH)));
  353. }
  354. for(MyLabelmy:lm.getLabels()){
  355. this.add(my);
  356. }
  357. select.setTime(temp);
  358. }
  359. }
  360. privateclassMyLabelextendsJLabelimplementsComparator<MyLabel>,
  361. MouseListener,MouseMotionListener{
  362. privateintyear,month,day;
  363. privatebooleanisSelected;
  364. publicMyLabel(intyear,intmonth,intday){
  365. super(""+day,JLabel.CENTER);
  366. this.year=year;
  367. this.day=day;
  368. this.month=month;
  369. this.addMouseListener(this);
  370. this.addMouseMotionListener(this);
  371. this.setFont(font);
  372. if(month==select.get(Calendar.MONTH)){
  373. this.setForeground(Color.BLACK);
  374. }else{
  375. this.setForeground(Color.LIGHT_GRAY);
  376. }
  377. if(day==select.get(Calendar.DAY_OF_MONTH)){
  378. this.setBackground(newColor(160,185,215));
  379. }else{
  380. this.setBackground(Color.WHITE);
  381. }
  382. }
  383. publicbooleangetIsSelected(){
  384. returnisSelected;
  385. }
  386. publicvoidsetSelected(booleanb,booleanisDrag){
  387. isSelected=b;
  388. if(b&&!isDrag){
  389. inttemp=select.get(Calendar.MONTH);
  390. select.set(year,month,day);
  391. if(temp==month){
  392. SwingUtilities.updateComponentTreeUI(jp3);
  393. }else{
  394. refresh();
  395. }
  396. }
  397. this.repaint();
  398. }
  399. protectedvoidpaintComponent(Graphicsg){
  400. if(day==select.get(Calendar.DAY_OF_MONTH)&&
  401. month==select.get(Calendar.MONTH)){
  402. //如果当前日期是选择日期,则高亮显示
  403. g.setColor(newColor(160,185,215));
  404. g.fillRect(0,0,getWidth(),getHeight());
  405. }
  406. if(year==now.get(Calendar.YEAR)&&
  407. month==now.get(Calendar.MONTH)&&
  408. day==now.get(Calendar.DAY_OF_MONTH)){
  409. //如果日期和当前日期一样,则用红框
  410. Graphics2Dgd=(Graphics2D)g;
  411. gd.setColor(Color.RED);
  412. Polygonp=newPolygon();
  413. p.addPoint(0,0);
  414. p.addPoint(getWidth()-1,0);
  415. p.addPoint(getWidth()-1,getHeight()-1);
  416. p.addPoint(0,getHeight()-1);
  417. gd.drawPolygon(p);
  418. }
  419. if(isSelected){//如果被选中了就画出一个虚线框出来
  420. Strokes=newBasicStroke(1.0f,BasicStroke.CAP_SQUARE,
  421. BasicStroke.JOIN_BEVEL,1.0f,newfloat[]{2.0f,2.0f},1.0f);
  422. Graphics2Dgd=(Graphics2D)g;
  423. gd.setStroke(s);
  424. gd.setColor(Color.BLACK);
  425. Polygonp=newPolygon();
  426. p.addPoint(0,0);
  427. p.addPoint(getWidth()-1,0);
  428. p.addPoint(getWidth()-1,getHeight()-1);
  429. p.addPoint(0,getHeight()-1);
  430. gd.drawPolygon(p);
  431. }
  432. super.paintComponent(g);
  433. }
  434. publicbooleancontains(Pointp){
  435. returnthis.getBounds().contains(p);
  436. }
  437. privatevoidupdate(){
  438. repaint();
  439. }
  440. publicvoidmouseClicked(MouseEvente){
  441. }
  442. publicvoidmousePressed(MouseEvente){
  443. isSelected=true;
  444. update();
  445. }
  446. publicvoidmouseReleased(MouseEvente){
  447. Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
  448. lm.setSelect(p,false);
  449. commit();
  450. }
  451. publicvoidmouseEntered(MouseEvente){
  452. }
  453. publicvoidmouseExited(MouseEvente){
  454. }
  455. publicvoidmouseDragged(MouseEvente){
  456. Pointp=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
  457. lm.setSelect(p,true);
  458. }
  459. publicvoidmouseMoved(MouseEvente){
  460. }
  461. publicintcompare(MyLabelo1,MyLabelo2){
  462. Calendarc1=Calendar.getInstance();
  463. c1.set(o1.year,o2.month,o1.day);
  464. Calendarc2=Calendar.getInstance();
  465. c2.set(o2.year,o2.month,o2.day);
  466. returnc1.compareTo(c2);
  467. }
  468. }
  469. privateclassLabelManager{
  470. privateList<MyLabel>list;
  471. publicLabelManager(){
  472. list=newArrayList<MyLabel>();
  473. }
  474. publicList<MyLabel>getLabels(){
  475. returnlist;
  476. }
  477. publicvoidaddLabel(MyLabelmy){
  478. list.add(my);
  479. }
  480. publicvoidclear(){
  481. list.clear();
  482. }
  483. publicvoidsetSelect(MyLabelmy,booleanb){
  484. for(MyLabelm:list){
  485. if(m.equals(my)){
  486. m.setSelected(true,b);
  487. }else{
  488. m.setSelected(false,b);
  489. }
  490. }
  491. }
  492. publicvoidsetSelect(Pointp,booleanb){
  493. //如果是拖动,则要优化一下,以提高效率
  494. if(b){
  495. //表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和
  496. //将要显示的标签找到了就可以了
  497. booleanfindPrevious=false,findNext=false;
  498. for(MyLabelm:list){
  499. if(m.contains(p)){
  500. findNext=true;
  501. if(m.getIsSelected()){
  502. findPrevious=true;
  503. }else{
  504. m.setSelected(true,b);
  505. }
  506. }elseif(m.getIsSelected()){
  507. findPrevious=true;
  508. m.setSelected(false,b);
  509. }
  510. if(findPrevious&&findNext){
  511. return;
  512. }
  513. }
  514. }else{
  515. MyLabeltemp=null;
  516. for(MyLabelm:list){
  517. if(m.contains(p)){
  518. temp=m;
  519. }elseif(m.getIsSelected()){
  520. m.setSelected(false,b);
  521. }
  522. }
  523. if(temp!=null){
  524. temp.setSelected(true,b);
  525. }
  526. }
  527. }
  528. }
  529. privateclassJP4extendsJPanel{
  530. publicJP4(){
  531. super(newBorderLayout());
  532. this.setPreferredSize(newDimension(295,20));
  533. this.setBackground(newColor(160,185,215));
  534. SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月dd日");
  535. finalJLabeljl=newJLabel("今天:"+sdf.format(newDate()));
  536. jl.setToolTipText("点击选择今天日期");
  537. this.add(jl,BorderLayout.CENTER);
  538. jl.addMouseListener(newMouseAdapter(){
  539. publicvoidmouseEntered(MouseEventme){
  540. jl.setCursor(newCursor(Cursor.HAND_CURSOR));
  541. jl.setForeground(Color.RED);
  542. }
  543. publicvoidmouseExited(MouseEventme){
  544. jl.setCursor(newCursor(Cursor.DEFAULT_CURSOR));
  545. jl.setForeground(Color.BLACK);
  546. }
  547. publicvoidmousePressed(MouseEventme){
  548. jl.setForeground(Color.WHITE);
  549. select.setTime(newDate());
  550. refresh();
  551. commit();
  552. }
  553. publicvoidmouseReleased(MouseEventme){
  554. jl.setForeground(Color.BLACK);
  555. }
  556. });
  557. }
  558. }
  559. publicstaticvoidmain(String[]args){
  560. finalDateChoosermp=newDateChooser("yyyy-MM-dd");
  561. JFramejf=newJFrame("测试日期选择器");
  562. jf.add(mp,BorderLayout.CENTER);
  563. jf.add(newJButton("测试用的"),BorderLayout.NORTH);
  564. jf.pack();
  565. jf.setLocationRelativeTo(null);
  566. jf.setVisible(true);
  567. jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  568. }
  569. }

运行效果图如下:


分享到:
评论

相关推荐

    java swing 时间日期选择控件

    JCalendar包含一个日历面板和一个日期选择器,用户可以通过点击日、周、月或年视图来选择日期。它还支持日期范围选择、日期格式化以及多语言环境。 2. **JDatePicker**: 这是SwingX库中的一个组件,它提供了一个...

    java 日期选择控件DateChooser

    Java 日期选择控件DateChooser是Java Swing库中的一种组件,用于在GUI应用程序中方便用户进行日期选择。这个控件通常被用在需要用户输入日期的场景,如表单、日历应用或时间相关的功能中。DateChooser提供了一个可视...

    swing 日期选择器 支持点击文本框选择 和自定义按钮选择

    swing 日期选择器 支持点击文本框选择 和自定义按钮选择

    JAVA Swing日期选择控件datepicker的使用

    1. 添加依赖:为了在Swing应用中使用DatePicker,可以引入JCalendar库,这是一个包含日期选择器组件的第三方库。将JCalendar的jar文件添加到项目的类路径中。 2. 引入组件:在程序中导入JCalendar的相关类,例如`...

    java swing 日期控件

    `JDatePicker` 是Swing中最常用的日期选择组件,它提供了日期选择器的界面。`JDatePicker` 结合了`JFormattedTextField` 和一个日期模型(通常是`DefaultDateModel`),允许用户通过文本字段或日历小部件来输入或...

    JAVA SWING 日期控件(目前支持JLable和JTextField,若有需要可自行扩展其它控件)

    2. **自定义日期控件**:通常,Java Swing没有内置的日期选择器,但开发者可以使用诸如JCalendar、JDatePicker等第三方库,或者像这个博客中提到的那样,创建自己的日期选择组件。DateChooser.java文件很可能是这个...

    Java Swing 日期时间组件

    在Swing中,开发人员可以使用各种组件来创建用户界面,其中包括日期和时间选择器组件。这个主题主要关注的是`DateChooser`,一个允许用户选择日期和时间的控件,通常用在需要用户输入日期或时间的应用场景。 `...

    java swing 日历

    Java Swing 是Java GUI(图形用户界面)库...通过这两个类,开发者可以创建一个交互式的日历选择器,为用户提供方便的方式来选择日期。学习这个项目,有助于理解Java Swing的组件使用、事件处理以及如何自定义GUI组件。

    Java日期选择控件

    这个组件继承自`javax.swing.JButton`,并且内部包含了一个`DateChooser`对象,该对象负责显示日期选择对话框。当用户点击这个按钮时,会弹出日期选择对话框,让用户选择日期,然后将选择的日期更新到按钮的文字显示...

    非常不错的swing日历控件

    在 Java Swing 中,日历控件是一种常用的 GUI 组件,用于选择日期和时间。本文将详细介绍一个非常不错的 Swing 日历控件,包括其实现机制、主要组件、方法修改和使用方法。 日历控件概述 该日历控件类继承自 ...

    一个神奇的swing软件Jgoodies

    4. **日期和时间选择器**:Jgoodies Forms提供了日期选择器(DateChooser)和时间选择器(TimePicker),它们提供了比Swing内置组件更友好的日期和时间输入界面。 5. **对话框和提示**:Jgoodies提供了一系列预定义...

    学生管理系统

    在构建学生管理系统的过程中,经常会用到各种编程组件,如日期选择器(DateChooser),它是用来方便用户选择日期的交互元素,尤其在处理涉及时间的事务时不可或缺。 DateChooser组件在Java编程语言中常见于Swing或...

    java-swing-datepicker.zip_Java编程_Java_

    标题"java-swing-datepicker.zip"暗示了这个压缩包可能包含了一个关于Java Swing日期选择器的示例代码或教程。`JDateChooser`是Java Swing中用于日期选择的标准控件,它允许用户通过一个日历视图选择日期,而不仅仅...

    文本框日历

    在这个特定的项目中,"文本框日历"是基于Java Swing实现的一个功能,它将日历与文本框结合,为用户提供日期选择的功能。 首先,让我们深入了解一下`DateChooser.java`。这个文件很可能是自定义日历组件的核心代码。...

    java日历控件犹如c#一样可以选择的日历

    总结来说,这个Java日历控件通过自定义`JPanel`和相关的监听器实现了类似C#的日期选择功能。用户可以通过点击`JTextField`来显示日历,然后在日历上选择日期,选择的日期会显示在`JTextField`中。这种自定义控件为...

    java日历-时间选择

    例如,使用Java Swing的JCalendar库可以创建一个日期选择器: ```java import com.toedter.calendar.JDateChooser; JDateChooser dateChooser = new JDateChooser(); // 添加到容器中 JFrame frame = new ...

    日历 JAVA程序

    在Swing中,`JCalendar`组件可以用于创建日期选择器,而JavaFX提供`DatePicker`类。这些组件可以与事件监听器结合,当用户选择日期时触发特定的事件。开发者可能会自定义这些组件的行为,例如添加事件管理,以在用户...

    java swing mysql实现的学生选课成绩信息管理系统.zip

    此外,`DateChooser.class`表示系统可能包含日期选择功能,这在处理与时间相关的任务时非常实用,比如设定课程的时间表或者记录成绩的录入日期。`ClassManage.class`可能涉及班级管理,便于按照班级查看和管理学生...

    Java日历组件及使用示例.pdf

    TestDC类则是关于DateChooser组件的使用,它展示了如何创建一个日期选择对话框,并将选定的日期设置到文本字段中。 根据文件内容,以上是关于Java日历组件及使用示例的一些核心知识点。这些组件和类的使用是Java...

    Java编写日历面板代码及调用 Eclipse可用

    以下是一个基于Java Swing的日历选择器(DateChooser)的实现,适用于Eclipse开发环境。 1. **组件导入**: 首先,你需要导入相关的Java类库,如`java.awt.*`、`javax.swing.*`等,这些库包含了创建日历面板所需的...

Global site tag (gtag.js) - Google Analytics