当前位置: 代码迷 >> Java相关 >> [原创]日期选择器
  详细解决方案

[原创]日期选择器

热度:259   发布时间:2007-11-17 08:18:08.0
学习的榜样!
----------------解决方案--------------------------------------------------------

版主千里好厉害啊!


----------------解决方案--------------------------------------------------------

拿着研究一下~ 还有 内部类怎么会乱呢? 我比较喜欢内部类~


----------------解决方案--------------------------------------------------------
真厉害!!!!!
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]千里冰封[/underline][/bold] 于 2007-8-29 13:16 发表 [url=http://bbs.bc-cn.net/redirect.php?goto=findpost&pid=973042&ptid=165105][/url]
回到当前日期是有的,就是点击最下面的"今天:2007年08月29日"就回到当前的日期了至于加一个上一年和下一年,我当前也考虑了,加了两个按钮好像就很容易按错了 ...

不会啊,使用GridBagLayout布局,挺好的,给你改了一下,加了上一年和下一年,呵呵
----------------解决方案--------------------------------------------------------
有个问题,就是那个setToolTipText()在这里没用,运行时当鼠标移到上面的时候不显示提示信息,不知道怎么回事
程序代码:
    private class JP1 extends JPanel{
        JLabel lastYear,nextYear,lastMonth,nextMonth,center;
        public JP1(){
            //super(new BorderLayout());
            this.setBackground(new Color(160,185,215));
            initJP1();
        }
        private void initJP1(){            
            lastYear=new JLabel(" << ",JLabel.CENTER);
            nextYear=new JLabel(" >> ",JLabel.CENTER);
            lastYear.setToolTipText("上一年");
            nextYear.setToolTipText("下一年");
            lastMonth=new JLabel(" < ",JLabel.CENTER);
            lastMonth.setToolTipText("上一月");
            nextMonth=new JLabel(" > ",JLabel.CENTER);
            nextMonth.setToolTipText("下一月");
            center=new JLabel("",JLabel.CENTER);
            updateDate();
            
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            this.setLayout(gridbag);
            c.weightx=0.5;
            gridbag.setConstraints(lastYear, c);
            this.add(lastYear);
            c.weightx=0.5;
            gridbag.setConstraints(lastMonth, c);
            this.add(lastMonth);
            
            c.weightx=8.0;
            gridbag.setConstraints(center, c);
            this.add(center);            
            
            c.weightx=0.5;
            gridbag.setConstraints(nextMonth, c);
            this.add(nextMonth);
            c.weightx=0.5;
            gridbag.setConstraints(nextYear, c);
            this.add(nextYear);

            //this.add(left,BorderLayout.WEST);
            //this.add(center,BorderLayout.CENTER);
            //this.add(right,BorderLayout.EAST);
            this.setPreferredSize(new Dimension(295,25));
            lastYear.addMouseListener(new MouseAdapter(){
                public void mouseEntered(MouseEvent me){
                    lastYear.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    lastYear.setForeground(Color.RED);
                }
                public void mouseExited(MouseEvent me){
                    lastYear.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    lastYear.setForeground(Color.BLACK);
                }
                public void mousePressed(MouseEvent me){
                    select.add(Calendar.YEAR,-1);
                    lastYear.setForeground(Color.WHITE);
                    refresh();
                }
                public void mouseReleased(MouseEvent me){
                    lastYear.setForeground(Color.BLACK);
                }
            });
            nextYear.addMouseListener(new MouseAdapter(){
                public void mouseEntered(MouseEvent me){
                    nextYear.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    nextYear.setForeground(Color.RED);
                }
                public void mouseExited(MouseEvent me){
                    nextYear.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    nextYear.setForeground(Color.BLACK);
                }
                public void mousePressed(MouseEvent me){
                    select.add(Calendar.YEAR,1);
                    nextYear.setForeground(Color.WHITE);
                    refresh();
                }
                public void mouseReleased(MouseEvent me){
                    nextYear.setForeground(Color.BLACK);
                }
            });

            lastMonth.addMouseListener(new MouseAdapter(){
                public void mouseEntered(MouseEvent me){
                    lastMonth.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    lastMonth.setForeground(Color.RED);
                }
                public void mouseExited(MouseEvent me){
                    lastMonth.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    lastMonth.setForeground(Color.BLACK);
                }
                public void mousePressed(MouseEvent me){
                    select.add(Calendar.MONTH,-1);
                    lastMonth.setForeground(Color.WHITE);
                    refresh();
                }
                public void mouseReleased(MouseEvent me){
                    lastMonth.setForeground(Color.BLACK);
                }
            });
            nextMonth.addMouseListener(new MouseAdapter(){
                public void mouseEntered(MouseEvent me){
                    nextMonth.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    nextMonth.setForeground(Color.RED);
                }
                public void mouseExited(MouseEvent me){
                    nextMonth.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    nextMonth.setForeground(Color.BLACK);
                }
                public void mousePressed(MouseEvent me){
                    select.add(Calendar.MONTH,1);
                    nextMonth.setForeground(Color.WHITE);
                    refresh();
                }
                public void mouseReleased(MouseEvent me){
                    nextMonth.setForeground(Color.BLACK);
                }
            });
        }
        private void updateDate(){
            center.setText(select.get(Calendar.YEAR)+"年"+(select.get(Calendar.MONTH)+1)+"月");
        }
    }

----------------解决方案--------------------------------------------------------
还改了一个地方,  MyLabel.mouseReleased(),改为双击时选定日期,这样更人性化一点
      public void mouseReleased(MouseEvent e) {
            // 双击时选定
            if (e.getClickCount() == 2) {
                commit();
                return;
            }
            Point p=SwingUtilities.convertPoint(this,e.getPoint(),jp3);
            lm.setSelect(p,false);
        }
----------------解决方案--------------------------------------------------------
呵呵,你改得不错
----------------解决方案--------------------------------------------------------
学习一下
----------------解决方案--------------------------------------------------------
你好,我打不开论坛里的文件,你可以把这个文件发送到我邮箱吗?我的邮箱地址fanliumei@126.com.谢谢了!
----------------解决方案--------------------------------------------------------
  相关解决方案