Java 日期选择器课件_第1页
Java 日期选择器课件_第2页
Java 日期选择器课件_第3页
Java 日期选择器课件_第4页
Java 日期选择器课件_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java 日期选择器效果如图:import java.awt.BasicStroke;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridLayout;import java.awt.Point;import ja

2、va.awt.Polygon;import java.awt.Stroke;import java.awt.Toolkit;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.text.Simple

3、DateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Comparator;import java.util.Date;import java.util.List;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Popup;i

4、mport javax.swing.PopupFactory;import javax.swing.SwingUtilities;import javax.swing.event.AncestorEvent;import javax.swing.event.AncestorListener;public class DateChooser extends JPanelprivate Date initDate;private Calendar now=Calendar.getInstance();private Calendar select;private JPanel monthPanel

5、;/月历private JP1 jp1;/四块面板,组成private JP2 jp2;private JP3 jp3;private JP4 jp4;private Font font=new Font(宋体,Font.PLAIN,12);private final LabelManager lm=new LabelManager();private JLabel showDate;/,toSelect;private SimpleDateFormat sdf=new SimpleDateFormat(yyyy年MM月dd日);private boolean isShow=false;pri

6、vate Popup pop;public DateChooser() this(new Date();public DateChooser(Date date)initDate=date;select=Calendar.getInstance();select.setTime(initDate);initPanel();initLabel();public void setEnabled(boolean b)super.setEnabled(b);showDate.setEnabled(b);public Date getDate()return select.getTime();/根据初始

7、化的日期,初始化面板private void initPanel()monthPanel=new JPanel(new BorderLayout();monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE);JPanel up=new JPanel(new BorderLayout();up.add(jp1=new JP1(),BorderLayout.NORTH);up.add(jp2=new JP2(),BorderLayout.CENTER);monthPanel.add(jp3=new JP3(),BorderLay

8、out.CENTER);monthPanel.add(up,BorderLayout.NORTH);monthPanel.add(jp4=new JP4(),BorderLayout.SOUTH);this.addAncestorListener(new AncestorListener()public void ancestorAdded(AncestorEvent event) public void ancestorRemoved(AncestorEvent event) /只要祖先组件一移动,马上就让popup消失public void ancestorMoved(AncestorEv

9、ent event) hidePanel(););/初始化标签private void initLabel()showDate=new JLabel(sdf.format(initDate);showDate.setRequestFocusEnabled(true);showDate.addMouseListener(new MouseAdapter()public void mousePressed(MouseEvent me)showDate.requestFocusInWindow(););/toSelect=new JLabel(sdf.format(initDate);/toSele

10、ct.setBorder(BorderFactory.createLineBorder(Color.BLACK);/toSelect.setRequestFocusEnabled(true);this.setBackground(Color.WHITE);this.add(showDate,BorderLayout.CENTER);/this.add(toSelect,BorderLayout.EAST);this.setPreferredSize(new Dimension(90,25);this.setBorder(BorderFactory.createLineBorder(Color.

11、GRAY);showDate.addMouseListener(new MouseAdapter()public void mouseEntered(MouseEvent me)if(showDate.isEnabled()showDate.setCursor(new Cursor(Cursor.HAND_CURSOR);showDate.setForeground(Color.RED);public void mouseExited(MouseEvent me)if(showDate.isEnabled()showDate.setCursor(new Cursor(Cursor.DEFAUL

12、T_CURSOR);showDate.setForeground(Color.BLACK);public void mousePressed(MouseEvent me)if(showDate.isEnabled()showDate.setForeground(Color.CYAN);if(isShow)hidePanel();elseshowPanel(showDate);public void mouseReleased(MouseEvent me)if(showDate.isEnabled()showDate.setForeground(Color.BLACK););showDate.a

13、ddFocusListener(new FocusListener()public void focusLost(FocusEvent e)hidePanel();public void focusGained(FocusEvent e);/根据新的日期刷新private void refresh()jp1.updateDate();jp3.updateDate();SwingUtilities.updateComponentTreeUI(this);/提交日期private void commit()System.out.println(选中的日期是:+sdf.format(select.g

14、etTime();showDate.setText(sdf.format(select.getTime();hidePanel();private void hidePanel()if(pop!=null)isShow=false;pop.hide();pop=null;private void showPanel(Component owner)if(pop!=null)pop.hide();Point show=new Point(0,showDate.getHeight();SwingUtilities.convertPointToScreen(show,showDate);Dimens

15、ion size=Toolkit.getDefaultToolkit().getScreenSize();int x=show.x;int y=show.y;if(xsize.width-295)x=size.width-295;if(yelsey-=188;pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y);pop.show();isShow=true;private class JP1 extends JPanelJLabel left,right,center;public JP1()super(new

16、BorderLayout();this.setBackground(new Color(160,185,215);initJP1();private void initJP1()left=new JLabel( ,JLabel.CENTER);right.setToolTipText(下一月);left.setBorder(BorderFactory.createEmptyBorder(10,0,0,0);right.setBorder(BorderFactory.createEmptyBorder(10,0,0,0);center=new JLabel(,JLabel.CENTER);upd

17、ateDate();this.add(left,BorderLayout.WEST);this.add(center,BorderLayout.CENTER);this.add(right,BorderLayout.EAST);this.setPreferredSize(new Dimension(295,25);left.addMouseListener(new MouseAdapter()public void mouseEntered(MouseEvent me)left.setCursor(new Cursor(Cursor.HAND_CURSOR);left.setForegroun

18、d(Color.RED);public void mouseExited(MouseEvent me)left.setCursor(new Cursor(Cursor.DEFAULT_CURSOR);left.setForeground(Color.BLACK);public void mousePressed(MouseEvent me)select.add(Calendar.MONTH,-1);left.setForeground(Color.WHITE);refresh();public void mouseReleased(MouseEvent me)left.setForegroun

19、d(Color.BLACK););right.addMouseListener(new MouseAdapter()public void mouseEntered(MouseEvent me)right.setCursor(new Cursor(Cursor.HAND_CURSOR);right.setForeground(Color.RED);public void mouseExited(MouseEvent me)right.setCursor(new Cursor(Cursor.DEFAULT_CURSOR);right.setForeground(Color.BLACK);publ

20、ic void mousePressed(MouseEvent me)select.add(Calendar.MONTH,1);right.setForeground(Color.WHITE);refresh();public void mouseReleased(MouseEvent me)right.setForeground(Color.BLACK););private void updateDate()center.setText(select.get(Calendar.YEAR)+年+(select.get(Calendar.MONTH)+1)+月);private class JP

21、2 extends JPanelpublic JP2()this.setPreferredSize(new Dimension(295,20);protected void paintComponent(Graphics g)g.setFont(font);g.drawString(星期日 星期一 星期二 星期三 星期四 星期五 星期六,5,10);g.drawLine(0,15,getWidth(),15);private class JP3 extends JPanelpublic JP3()super(new GridLayout(6,7);this.setPreferredSize(n

22、ew Dimension(295,100);initJP3();private void initJP3()updateDate();public void updateDate()this.removeAll();lm.clear();Date temp=select.getTime();Calendar select=Calendar.getInstance();select.setTime(temp);select.set(Calendar.DAY_OF_MONTH,1);int index=select.get(Calendar.DAY_OF_WEEK);int sum=(index=

23、1?8:index);select.add(Calendar.DAY_OF_MONTH,0-sum);for(int i=0;i42;i+)select.add(Calendar.DAY_OF_MONTH,1);lm.addLabel(new MyLabel(select.get(Calendar.YEAR),select.get(Calendar.MONTH),select.get(Calendar.DAY_OF_MONTH);for(MyLabel my:lm.getLabels()this.add(my);select.setTime(temp);private class MyLabe

24、l extends JLabel implements Comparator,MouseListener,MouseMotionListenerprivate int year,month,day;private boolean isSelected;public MyLabel(int year,int month,int day)super(+day,JLabel.CENTER);this.year=year;this.day=day;this.month=month;this.addMouseListener(this);this.addMouseMotionListener(this)

25、;this.setFont(font);if(month=select.get(Calendar.MONTH)this.setForeground(Color.BLACK);elsethis.setForeground(Color.LIGHT_GRAY);if(day=select.get(Calendar.DAY_OF_MONTH)this.setBackground(new Color(160,185,215);elsethis.setBackground(Color.WHITE);public boolean getIsSelected()return isSelected;public

26、 void setSelected(boolean b,boolean isDrag)isSelected=b;if(b&!isDrag)int temp=select.get(Calendar.MONTH);select.set(year,month,day);if(temp=month)SwingUtilities.updateComponentTreeUI(jp3);elserefresh();this.repaint();protected void paintComponent(Graphics g)if(day=select.get(Calendar.DAY_OF_MONTH)&m

27、onth=select.get(Calendar.MONTH)/如果当前日期是选择日期,则高亮显示g.setColor(new Color(160,185,215);g.fillRect(0,0,getWidth(),getHeight();if(year=now.get(Calendar.YEAR)&month=now.get(Calendar.MONTH)&day=now.get(Calendar.DAY_OF_MONTH)/如果日期和当前日期一样,则用红框Graphics2D gd=(Graphics2D)g;gd.setColor(Color.RED);Polygon p=new Po

28、lygon();p.addPoint(0,0);p.addPoint(getWidth()-1,0);p.addPoint(getWidth()-1,getHeight()-1);p.addPoint(0,getHeight()-1);gd.drawPolygon(p);if(isSelected)/如果被选中了就画出一个虚线框出来Stroke s=new BasicStroke(1.0f,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_BEVEL,1.0f,new float2.0f,2.0f,1.0f);Graphics2D gd=(Graphics2D)g

29、;gd.setStroke(s);gd.setColor(Color.BLACK);Polygon p=new Polygon();p.addPoint(0,0);p.addPoint(getWidth()-1,0);p.addPoint(getWidth()-1,getHeight()-1);p.addPoint(0,getHeight()-1);gd.drawPolygon(p);super.paintComponent(g);public boolean contains(Point p)return this.getBounds().contains(p);private void u

30、pdate()repaint();public void mouseClicked(MouseEvent e) public void mousePressed(MouseEvent e) isSelected=true;update();public void mouseReleased(MouseEvent e) Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);lm.setSelect(p,false);commit();public void mouseEntered(MouseEvent e) public void

31、 mouseExited(MouseEvent e) public void mouseDragged(MouseEvent e) Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);lm.setSelect(p,true);public void mouseMoved(MouseEvent e) public int compare(MyLabel o1, MyLabel o2) Calendar c1=Calendar.getInstance();c1.set(o1.year,o2.month,o1.day);Calenda

32、r c2=Calendar.getInstance();c2.set(o2.year,o2.month,o2.day);return pareTo(c2);private class LabelManagerprivate List list;public LabelManager()list=new ArrayList();public List getLabels()return list;public void addLabel(MyLabel my)list.add(my);public void clear()list.clear();public void setSelect(My

33、Label my,boolean b)for(MyLabel m:list)if(m.equals(my)m.setSelected(true,b);elsem.setSelected(false,b);public void setSelect(Point p,boolean b)/如果是拖动,则要优化一下,以提高效率if(b)/表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和/将要显示的标签找到了就可以了boolean findPrevious=false,findNext=false;for(MyLabel m:list)if(m.contains(p)findNext=true;if(m.getIsSelected()findPrevious=true;elsem.setSelected(true,b);else if(m.getIsSelected()findPrevious=true;m.setSelected(false,b);if(findPrevious&findNext)return;elseMyLabel temp=null;for(MyLabel m:list)if(m.contains(p)temp=m;else if(m.getIsSelected(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论