- 浏览: 306780 次
- 性别:
- 来自: 北京
最新评论
-
zczjdyj123:
...
Linux SSH 登录其他机器 -
chenzheng8975:
搞得不错 32个赞。。。。。
圆角PopupWindow对话框和圆角EditText -
guoxianyu521:
感谢楼主!!!!
DateChooser日期选择器swing -
雪尹江泽:
千言万语,就一句:太感谢你了!
圆角PopupWindow对话框和圆角EditText -
laozhao:
非常感谢楼主,非常好用。抄走了。呵呵。
DateChooser日期选择器swing
网上找的,不错的日期选择器,在此分享给大家。
package cn.xcu.ui; 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 java.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.SimpleDateFormat; 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; import javax.swing.PopupFactory; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; /** * 日期选择器 */ public class DateChooser extends JPanel{ private static final long serialVersionUID = 4529266044762990227L; private Date initDate; private Calendar now=Calendar.getInstance(); private Calendar select; private JPanel monthPanel;//月历 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; private Popup pop; /** * Creates a new instance of DateChooser */ 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(); } //根据初始化的日期,初始化面板 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(),BorderLayout.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(AncestorEvent 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)); // toSelect.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.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.DEFAULT_CURSOR)); showDate.setForeground(Color.BLACK); } } public void mousePressed(MouseEvent me){ if(showDate.isEnabled()){ showDate.setForeground(Color.CYAN); if(isShow){ hidePanel(); }else{ showPanel(showDate); } } } public void mouseReleased(MouseEvent me){ if(showDate.isEnabled()){ showDate.setForeground(Color.BLACK); } } }); showDate.addFocusListener(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.getTime())); 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); Dimension size=Toolkit.getDefaultToolkit().getScreenSize(); int x=show.x; int y=show.y; if(x<0){ x=0; } if(x>size.width-295){ x=size.width-295; } if(y<size.height-170){ }else{ y-=188; } pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y); pop.show(); isShow=true; } /** * 最上面的面板用来显示月份的增减 */ private class JP1 extends JPanel{ JLabel left,right,center; public JP1(){ super(new BorderLayout()); this.setBackground(new Color(160,185,215)); initJP1(); } private void initJP1(){ left=new JLabel(" << ",JLabel.CENTER); left.setToolTipText("上一月"); right=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); updateDate(); 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.setForeground(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.setForeground(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); } public 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 JP2 extends JPanel{ public 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 JPanel{ public JP3(){ super(new GridLayout(6,7)); this.setPreferredSize(new 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==1?8:index); select.add(Calendar.DAY_OF_MONTH,0-sum); for(int i=0;i<42;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 MyLabel extends JLabel implements Comparator<MyLabel>, MouseListener,MouseMotionListener{ private 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); this.setFont(font); if(month==select.get(Calendar.MONTH)){ this.setForeground(Color.BLACK); }else{ this.setForeground(Color.LIGHT_GRAY); } if(day==select.get(Calendar.DAY_OF_MONTH)){ this.setBackground(new Color(160,185,215)); }else{ this.setBackground(Color.WHITE); } } public boolean getIsSelected(){ return isSelected; } public 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); }else{ refresh(); } } this.repaint(); } protected void paintComponent(Graphics g){ if(day==select.get(Calendar.DAY_OF_MONTH)&& month==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 Polygon(); 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 float[]{2.0f,2.0f},1.0f); Graphics2D gd=(Graphics2D)g; 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 update(){ 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 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); Calendar c2=Calendar.getInstance(); c2.set(o2.year,o2.month,o2.day); return c1.compareTo(c2); } } private class LabelManager{ private List<MyLabel> list; public LabelManager(){ list=new ArrayList<MyLabel>(); } public List<MyLabel> getLabels(){ return list; } public void addLabel(MyLabel my){ list.add(my); } public void clear(){ list.clear(); } public void setSelect(MyLabel my, boolean b){ for(MyLabel m:list){ if(m.equals(my)){ m.setSelected(true,b); }else{ m.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; }else{ m.setSelected(true,b); } }else if(m.getIsSelected()){ findPrevious=true; m.setSelected(false,b); } if(findPrevious&&findNext){ return; } } }else{ MyLabel temp=null; for(MyLabel m:list){ if(m.contains(p)){ temp=m; }else if(m.getIsSelected()){ m.setSelected(false,b); } } if(temp!=null){ temp.setSelected(true,b); } } } } private class JP4 extends JPanel{ public JP4(){ super(new BorderLayout()); this.setPreferredSize(new Dimension(295,20)); this.setBackground(new Color(160,185,215)); SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"); final JLabel jl=new JLabel("今天: "+sdf.format(new Date())); jl.setToolTipText("点击回到今天日期"); this.add(jl,BorderLayout.CENTER); jl.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ jl.setCursor(new Cursor(Cursor.HAND_CURSOR)); jl.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); jl.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ jl.setForeground(Color.WHITE); select.setTime(new Date()); refresh(); commit(); } public void mouseReleased(MouseEvent me){ jl.setForeground(Color.BLACK); } }); } } public static void main(String[] args) { final DateChooser mp = new DateChooser(); JFrame jf = new JFrame("test"); jf.add(mp, BorderLayout.CENTER); jf.add(new JButton("测试用的"),BorderLayout.NORTH); jf.pack(); jf.setLocationRelativeTo(null); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
补充,上面是直接从网上拷的,没有“年”的增减功能,下面是我补充完整的代码.
1,增加“年”的增减操作。
2,增加指定日期格式的构造方法。
与大家分享一下。
package cn.xcu.ui; 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 java.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.SimpleDateFormat; 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; import javax.swing.PopupFactory; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; /** * 日期选择器,可以指定日期的显示格式 */ public class DateChooser extends JPanel{ private static final long serialVersionUID = 4529266044762990227L; private Date initDate; private Calendar now=Calendar.getInstance(); private Calendar select; private JPanel monthPanel;//月历 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; private boolean isShow=false; private Popup pop; /** * Creates a new instance of DateChooser */ public DateChooser() { this(new Date()); } public DateChooser(Date date){ this(date, "yyyy年MM月dd日"); } public DateChooser(String format){ this(new Date(), format); } public DateChooser(Date date, String format){ initDate=date; sdf=new SimpleDateFormat(format); 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(); } //根据初始化的日期,初始化面板 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(),BorderLayout.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(AncestorEvent 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)); // toSelect.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.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.DEFAULT_CURSOR)); showDate.setForeground(Color.BLACK); } } public void mousePressed(MouseEvent me){ if(showDate.isEnabled()){ showDate.setForeground(Color.CYAN); if(isShow){ hidePanel(); }else{ showPanel(showDate); } } } public void mouseReleased(MouseEvent me){ if(showDate.isEnabled()){ showDate.setForeground(Color.BLACK); } } }); showDate.addFocusListener(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.getTime())); 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); Dimension size=Toolkit.getDefaultToolkit().getScreenSize(); int x=show.x; int y=show.y; if(x<0){ x=0; } if(x>size.width-295){ x=size.width-295; } if(y<size.height-170){ }else{ y-=188; } pop=PopupFactory.getSharedInstance().getPopup(owner,monthPanel,x,y); pop.show(); isShow=true; } /** * 最上面的面板用来显示月份的增减 */ private class JP1 extends JPanel{ JLabel yearleft,yearright,monthleft,monthright,center,centercontainer; public JP1(){ super(new BorderLayout()); this.setBackground(new Color(160,185,215)); initJP1(); } private void initJP1(){ yearleft=new JLabel(" <<",JLabel.CENTER); yearleft.setToolTipText("上一年"); yearright=new JLabel(">> ",JLabel.CENTER); yearright.setToolTipText("下一年"); yearleft.setBorder(BorderFactory.createEmptyBorder(2,0,0,0)); yearright.setBorder(BorderFactory.createEmptyBorder(2,0,0,0)); monthleft=new JLabel(" <", JLabel.RIGHT); monthleft.setToolTipText("上一月"); monthright=new JLabel("> ", JLabel.LEFT); monthright.setToolTipText("下一月"); monthleft.setBorder(BorderFactory.createEmptyBorder(2,30,0,0)); monthright.setBorder(BorderFactory.createEmptyBorder(2,0,0,30)); centercontainer=new JLabel("", JLabel.CENTER); centercontainer.setLayout(new BorderLayout()); center=new JLabel("", JLabel.CENTER); centercontainer.add(monthleft,BorderLayout.WEST); centercontainer.add(center,BorderLayout.CENTER); centercontainer.add(monthright,BorderLayout.EAST); this.add(yearleft,BorderLayout.WEST); this.add(centercontainer,BorderLayout.CENTER); this.add(yearright,BorderLayout.EAST); this.setPreferredSize(new Dimension(295,25)); updateDate(); yearleft.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ yearleft.setCursor(new Cursor(Cursor.HAND_CURSOR)); yearleft.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ yearleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); yearleft.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ select.add(Calendar.YEAR,-1); yearleft.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me){ yearleft.setForeground(Color.BLACK); } }); yearright.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ yearright.setCursor(new Cursor(Cursor.HAND_CURSOR)); yearright.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ yearright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); yearright.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ select.add(Calendar.YEAR,1); yearright.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me){ yearright.setForeground(Color.BLACK); } }); monthleft.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ monthleft.setCursor(new Cursor(Cursor.HAND_CURSOR)); monthleft.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ monthleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); monthleft.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ select.add(Calendar.MONTH,-1); monthleft.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me){ monthleft.setForeground(Color.BLACK); } }); monthright.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ monthright.setCursor(new Cursor(Cursor.HAND_CURSOR)); monthright.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ monthright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); monthright.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ select.add(Calendar.MONTH,1); monthright.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me){ monthright.setForeground(Color.BLACK); } }); } private void updateDate(){ center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月"); } } private class JP2 extends JPanel{ public 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 JPanel{ public JP3(){ super(new GridLayout(6,7)); this.setPreferredSize(new 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==1?8:index); select.add(Calendar.DAY_OF_MONTH,0-sum); for(int i=0;i<42;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 MyLabel extends JLabel implements Comparator<MyLabel>, MouseListener,MouseMotionListener{ private 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); this.setFont(font); if(month==select.get(Calendar.MONTH)){ this.setForeground(Color.BLACK); }else{ this.setForeground(Color.LIGHT_GRAY); } if(day==select.get(Calendar.DAY_OF_MONTH)){ this.setBackground(new Color(160,185,215)); }else{ this.setBackground(Color.WHITE); } } public boolean getIsSelected(){ return isSelected; } public 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); }else{ refresh(); } } this.repaint(); } protected void paintComponent(Graphics g){ if(day==select.get(Calendar.DAY_OF_MONTH)&& month==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 Polygon(); 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 float[]{2.0f,2.0f},1.0f); Graphics2D gd=(Graphics2D)g; 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 update(){ 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 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); Calendar c2=Calendar.getInstance(); c2.set(o2.year,o2.month,o2.day); return c1.compareTo(c2); } } private class LabelManager{ private List<MyLabel> list; public LabelManager(){ list=new ArrayList<MyLabel>(); } public List<MyLabel> getLabels(){ return list; } public void addLabel(MyLabel my){ list.add(my); } public void clear(){ list.clear(); } public void setSelect(MyLabel my, boolean b){ for(MyLabel m:list){ if(m.equals(my)){ m.setSelected(true,b); }else{ m.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; }else{ m.setSelected(true,b); } }else if(m.getIsSelected()){ findPrevious=true; m.setSelected(false,b); } if(findPrevious&&findNext){ return; } } }else{ MyLabel temp=null; for(MyLabel m:list){ if(m.contains(p)){ temp=m; }else if(m.getIsSelected()){ m.setSelected(false,b); } } if(temp!=null){ temp.setSelected(true,b); } } } } private class JP4 extends JPanel{ public JP4(){ super(new BorderLayout()); this.setPreferredSize(new Dimension(295,20)); this.setBackground(new Color(160,185,215)); SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"); final JLabel jl=new JLabel("今天: "+sdf.format(new Date())); jl.setToolTipText("点击选择今天日期"); this.add(jl,BorderLayout.CENTER); jl.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent me){ jl.setCursor(new Cursor(Cursor.HAND_CURSOR)); jl.setForeground(Color.RED); } public void mouseExited(MouseEvent me){ jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); jl.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me){ jl.setForeground(Color.WHITE); select.setTime(new Date()); refresh(); commit(); } public void mouseReleased(MouseEvent me){ jl.setForeground(Color.BLACK); } }); } } public static void main(String[] args) { final DateChooser mp = new DateChooser("yyyy-MM-dd"); JFrame jf = new JFrame("测试日期选择器"); jf.add(mp, BorderLayout.CENTER); jf.add(new JButton("测试用的"),BorderLayout.NORTH); jf.pack(); jf.setLocationRelativeTo(null); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
运行效果图如下:
发表评论
-
汉字转拼音—pinyin4j实例
2010-12-14 19:13 2718综合应用小工具,汉字转拼音组件pinyin4j实例。(程 ... -
托盘闪动消息提醒(有声音+源码)
2010-12-14 15:14 2299发一个小例子供大家参考,java实现托盘闪动消息提醒,同 ... -
JTable的数据导出到Excel(网络代码修改)
2010-12-14 14:44 2221最近用Swing做了个小项目,其中有个把表格(JTable实现 ... -
带背景图片的JPanel
2010-12-14 09:54 3010说明:本实例实现了两种背景图片的设置方法: 1,小幅图片自动 ... -
Java记事本程序
2008-09-13 10:07 7579这个记事本程序是仿照 ...
相关推荐
JCalendar包含一个日历面板和一个日期选择器,用户可以通过点击日、周、月或年视图来选择日期。它还支持日期范围选择、日期格式化以及多语言环境。 2. **JDatePicker**: 这是SwingX库中的一个组件,它提供了一个...
Java 日期选择控件DateChooser是Java Swing库中的一种组件,用于在GUI应用程序中方便用户进行日期选择。这个控件通常被用在需要用户输入日期的场景,如表单、日历应用或时间相关的功能中。DateChooser提供了一个可视...
swing 日期选择器 支持点击文本框选择 和自定义按钮选择
1. 添加依赖:为了在Swing应用中使用DatePicker,可以引入JCalendar库,这是一个包含日期选择器组件的第三方库。将JCalendar的jar文件添加到项目的类路径中。 2. 引入组件:在程序中导入JCalendar的相关类,例如`...
`JDatePicker` 是Swing中最常用的日期选择组件,它提供了日期选择器的界面。`JDatePicker` 结合了`JFormattedTextField` 和一个日期模型(通常是`DefaultDateModel`),允许用户通过文本字段或日历小部件来输入或...
2. **自定义日期控件**:通常,Java Swing没有内置的日期选择器,但开发者可以使用诸如JCalendar、JDatePicker等第三方库,或者像这个博客中提到的那样,创建自己的日期选择组件。DateChooser.java文件很可能是这个...
在Swing中,开发人员可以使用各种组件来创建用户界面,其中包括日期和时间选择器组件。这个主题主要关注的是`DateChooser`,一个允许用户选择日期和时间的控件,通常用在需要用户输入日期或时间的应用场景。 `...
Java Swing 是Java GUI(图形用户界面)库...通过这两个类,开发者可以创建一个交互式的日历选择器,为用户提供方便的方式来选择日期。学习这个项目,有助于理解Java Swing的组件使用、事件处理以及如何自定义GUI组件。
这个组件继承自`javax.swing.JButton`,并且内部包含了一个`DateChooser`对象,该对象负责显示日期选择对话框。当用户点击这个按钮时,会弹出日期选择对话框,让用户选择日期,然后将选择的日期更新到按钮的文字显示...
在 Java Swing 中,日历控件是一种常用的 GUI 组件,用于选择日期和时间。本文将详细介绍一个非常不错的 Swing 日历控件,包括其实现机制、主要组件、方法修改和使用方法。 日历控件概述 该日历控件类继承自 ...
4. **日期和时间选择器**:Jgoodies Forms提供了日期选择器(DateChooser)和时间选择器(TimePicker),它们提供了比Swing内置组件更友好的日期和时间输入界面。 5. **对话框和提示**:Jgoodies提供了一系列预定义...
在构建学生管理系统的过程中,经常会用到各种编程组件,如日期选择器(DateChooser),它是用来方便用户选择日期的交互元素,尤其在处理涉及时间的事务时不可或缺。 DateChooser组件在Java编程语言中常见于Swing或...
标题"java-swing-datepicker.zip"暗示了这个压缩包可能包含了一个关于Java Swing日期选择器的示例代码或教程。`JDateChooser`是Java Swing中用于日期选择的标准控件,它允许用户通过一个日历视图选择日期,而不仅仅...
在这个特定的项目中,"文本框日历"是基于Java Swing实现的一个功能,它将日历与文本框结合,为用户提供日期选择的功能。 首先,让我们深入了解一下`DateChooser.java`。这个文件很可能是自定义日历组件的核心代码。...
总结来说,这个Java日历控件通过自定义`JPanel`和相关的监听器实现了类似C#的日期选择功能。用户可以通过点击`JTextField`来显示日历,然后在日历上选择日期,选择的日期会显示在`JTextField`中。这种自定义控件为...
例如,使用Java Swing的JCalendar库可以创建一个日期选择器: ```java import com.toedter.calendar.JDateChooser; JDateChooser dateChooser = new JDateChooser(); // 添加到容器中 JFrame frame = new ...
在Swing中,`JCalendar`组件可以用于创建日期选择器,而JavaFX提供`DatePicker`类。这些组件可以与事件监听器结合,当用户选择日期时触发特定的事件。开发者可能会自定义这些组件的行为,例如添加事件管理,以在用户...
此外,`DateChooser.class`表示系统可能包含日期选择功能,这在处理与时间相关的任务时非常实用,比如设定课程的时间表或者记录成绩的录入日期。`ClassManage.class`可能涉及班级管理,便于按照班级查看和管理学生...
TestDC类则是关于DateChooser组件的使用,它展示了如何创建一个日期选择对话框,并将选定的日期设置到文本字段中。 根据文件内容,以上是关于Java日历组件及使用示例的一些核心知识点。这些组件和类的使用是Java...
以下是一个基于Java Swing的日历选择器(DateChooser)的实现,适用于Eclipse开发环境。 1. **组件导入**: 首先,你需要导入相关的Java类库,如`java.awt.*`、`javax.swing.*`等,这些库包含了创建日历面板所需的...