⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dscomboxdatepopup.java

📁 用JAVA编写的绘图程序 功能简介: 支持存储
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
          }
          return (intD <= 30 && intD > 0);
          //2月份
        case 2:

          //区别闰年
          if ( (intY % 4 == 0 && intY % 100 != 0) || intY % 400 == 0) {
            //如果输入大于29,则修改为29
            if (intD > 29) {
              textComponent.setText(strDate.substring(0, 8) +
                                    "29");
              textComponent.setCaretPosition(iCaretPosition + 1);
            }
            if (intD < 1) {
              textComponent.setText(strDate.substring(0, 8) +
                                    "01");
              textComponent.setCaretPosition(iCaretPosition + 1);
            }
            return (intD <= 29 && intD > 0);
          }
          else {
            //如果输入大于28,则修改为28
            if (intD > 28) {
              textComponent.setText(strDate.substring(0, 8) +
                                    "28");
              textComponent.setCaretPosition(iCaretPosition + 1);
            }
            if (intD < 1) {
              textComponent.setText(strDate.substring(0, 8) +
                                    "01");
              textComponent.setCaretPosition(iCaretPosition + 1);
            }
            return (intD <= 28 && intD > 0);
          }
          //默认返回假
        default:
          return false;
      }
    }

    public static String getCurrentDate() {
      return (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new
          java.util.Date());
    }
  }

  /**
   * <p>Title: JDatePicker</p>
   * <p>Description: DatePopup 选择框弹出的日期选择面板</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: </p>
   * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
   * @version 1.0
   */
  class DatePopup
      extends BasicComboPopup {
    protected Calendar calendar;
    protected JLabel monthLabel;
    protected JPanel days = null;

    protected Color selectedBackground;
    protected Color selectedForeground;
    protected Color background;
    protected Color foreground;
    ImageIcon todayIcon;

    public DatePopup(JComboBox box) {
      super(box);
      try {
        //从JDatePicker.jar文件中获取today.gif资源
        java.net.URL url = new java.net.URL(
            "jar:file:" + (new File("OpenSwing.jar").
                           getAbsolutePath().replace(File.separatorChar,
            '/')) +
            "!/image/today.gif");
        todayIcon = new ImageIcon(url);
      }
      catch (Exception ex) {
        todayIcon = new ImageIcon();
      }
      calendar = Calendar.getInstance();
      background = UIManager.getColor("ComboBox.background");
      foreground = UIManager.getColor("ComboBox.foreground");
      selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
      selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
      initializePopup();
    }

    /**
     * 初始化弹出面板
     */
    protected void initializePopup() {
      //   << <   9999/99/99  > >>
      JPanel header = new JPanel();
      header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
      header.setBackground(new Color(0, 0, 220));
      header.setForeground(Color.white);
      header.setPreferredSize(new Dimension(1, 25));

      JLabel label;
      label = createUpdateButton(Calendar.YEAR, -1);
      label.setText("<<");
      header.add(Box.createHorizontalStrut(12));
      header.add(label);
      header.add(Box.createHorizontalStrut(12));

      label = createUpdateButton(Calendar.MONTH, -1);
      label.setText("< ");
      header.add(label);

      monthLabel = new JLabel("", JLabel.CENTER);
      monthLabel.setBackground(new Color(0, 0, 128));
      monthLabel.setForeground(Color.white);
      header.add(Box.createHorizontalGlue());
      header.add(monthLabel);
      header.add(Box.createHorizontalGlue());

      label = createUpdateButton(Calendar.MONTH, 1);
      label.setText(" >");
      header.add(label);

      label = createUpdateButton(Calendar.YEAR, 1);
      label.setText(">>");

      header.add(Box.createHorizontalStrut(12));
      header.add(label);
      header.add(Box.createHorizontalStrut(12));

      //星期日 星期一 星期二 星期三 星期四 星期五 星期六
      JPanel pWeeks = new JPanel(new GridLayout(0, 7));
      pWeeks.setBackground(background);
      pWeeks.setOpaque(true);
      DateFormatSymbols sy = new DateFormatSymbols(Locale.getDefault());
      String strWeeks[] = sy.getShortWeekdays();
      for (int i = 1; i <= 7; i++) {
        label = new JLabel();
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setForeground(foreground);
        label.setText(strWeeks[i]);
        pWeeks.add(label);
      }
      //中间放日期的面板
      days = new JPanel(new GridLayout(0, 7));
      days.setBorder(new TopBottomLineBorder(Color.black));
      days.setBackground(background);
      days.setOpaque(true);
      JPanel pCenter = new JPanel(new BorderLayout());
      pCenter.setBackground(background);
      pCenter.setOpaque(true);
      pCenter.add(pWeeks, BorderLayout.NORTH);
      pCenter.add(days, BorderLayout.CENTER);
      //显示今天的日期,直接单击图标跳到今天
      JLabel lbToday = new JDayLable(new Date(), "当前日期:"
                                     + dateFormat.format(new Date()));
      lbToday.setIcon(todayIcon);
      lbToday.setHorizontalAlignment(JLabel.LEFT);
      JPanel pSouth = new JPanel(new BorderLayout());
      pSouth.setBackground(background);
      pSouth.setForeground(foreground);
      pSouth.add(lbToday, BorderLayout.CENTER);

      setForeground(foreground);
      setBackground(background);
      setBorder(BorderFactory.createLineBorder(Color.black));
      setLayout(new BorderLayout());
      removeAll();
      add(BorderLayout.NORTH, header);
      add(BorderLayout.CENTER, pCenter);
      add(BorderLayout.SOUTH, pSouth);
    }

    /**
     * 显示弹出面板
     */
    public void show() {
      updatePopup();
      super.show();
    }

    /**
     * 创建上一月,下一月,上一年,下一年"按钮"
     * @param field int
     * @param amount int
     * @return JLabel
     */
    protected JLabel createUpdateButton(final int field, final int amount) {
      final JLabel label = new JLabel();
      final Border selectedBorder = new LineBorder(Color.black);
      final Border unselectedBorder = new EmptyBorder(selectedBorder.
          getBorderInsets(new JLabel()));
      label.setBorder(unselectedBorder);
      label.setBackground(new Color(0, 0, 128));
      label.setForeground(Color.white);
      label.setRequestFocusEnabled(false);
      label.addMouseListener(new MouseAdapter() {
        public void mouseReleased(MouseEvent e) {
          calendar.add(field, amount);
          updatePopup();
        }

        public void mouseEntered(MouseEvent e) {
          label.setBorder(selectedBorder);
        }

        public void mouseExited(MouseEvent e) {
          label.setBorder(unselectedBorder);
        }
      });
      return label;
    }

    /**
     * 更新日期
     */
    protected void updatePopup() {
      monthLabel.setText(monthFormat.format(calendar.getTime()));
      days.removeAll();

      Calendar setupCalendar = (Calendar) calendar.clone();
      setupCalendar.set(Calendar.DAY_OF_MONTH, 1);
      int first = setupCalendar.get(Calendar.DAY_OF_WEEK);
      /**
                      1  2  3  4  5  6  7
       2  3  4  5  6  7  8  9  10 11 12 13
       8  9  10 11 12 13 14 15 16 17 18 19
       14 15 16 17 18 19 20 21 22 23 24 25
       20 21 22 23 24 25 26 27 28 29 30 31
       26 27 28 29 30 31
       */
      setupCalendar.add(Calendar.DATE, -first);
      int flag = 0;
      for (int i = 0; i < 42; i++) {
        setupCalendar.add(Calendar.DATE, 1);
        String txt = dayFormat.format(setupCalendar.getTime());
        JLabel label = new JDayLable(setupCalendar.getTime(), txt);
        days.add(label);
        if (txt.equals("1")) {
          flag++;
        }
        if (flag != 1) {
          label.setEnabled(false);
        }
      }
      days.repaint();
      pack();
    }

    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: TopBottomLineBorder只带上下两条线的边界Border</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    class TopBottomLineBorder
        extends AbstractBorder {
      private Color lineColor;
      public TopBottomLineBorder(Color color) {
        lineColor = color;
      }

      public void paintBorder(Component c, Graphics g, int x, int y,
                              int width, int height) {
        g.setColor(lineColor);
        g.drawLine(0, 0, c.getWidth(), 0);
        g.drawLine(0, c.getHeight() - 1, c.getWidth(),
                   c.getHeight() - 1);
      }
    }

    /**
     * <p>Title: JDatePicker</p>
     * <p>Description:JDayLable 带选取日期功能的JLabel </p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
     * @version 1.0
     */
    class JDayLable
        extends JLabel
        implements MouseListener {
      Date date;
      Image img;
      boolean isToday = false;
      public JDayLable(Date date, String text) {
        setHorizontalAlignment(JLabel.CENTER);
        setForeground(foreground);
        setPreferredSize(new Dimension(40, 20));
        setToolTipText(dateFormat.format(date));
        addMouseListener(this);
        this.date = date;
        setText(text);
        Date d = new Date();
        isToday = (text.length() < 3) &&
            dateFormat.format(date).equals(dateFormat.format(d));
        if (calendar.getTime().equals(date)) {
          setBorder(new LineBorder(selectedBackground, 1));
        }
      }

      public void mousePressed(MouseEvent e) {}

      public void mouseClicked(MouseEvent e) {}

      public void mouseReleased(MouseEvent e) {
        if (isEnabled()) {
          setOpaque(false);
          setBackground(background);
          setForeground(foreground);
        }
        calendar.setTime(date);

        if (comboBox.isEditable() && comboBox.getEditor() != null) {
          comboBox.configureEditor(comboBox.getEditor(),
                                   dateFormat.format(calendar.getTime()));
        }
        comboBox.setSelectedItem(dateFormat.format(calendar.getTime()));
        comboBox.setPopupVisible(false);
      }

      public void mouseEntered(MouseEvent e) {
        if (isEnabled()) {
          setOpaque(true);
          setBackground(selectedBackground);
          setForeground(selectedForeground);
        }
      }

      public void mouseExited(MouseEvent e) {
        if (isEnabled()) {
          setOpaque(false);
          setBackground(background);
          setForeground(foreground);
        }
      }

      public void paint(Graphics g) {
        super.paint(g);
        if (isToday && todayIcon != null && isEnabled()) {
          int x = (this.getWidth() - todayIcon.getIconWidth()) / 2;
          int y = (this.getHeight() - todayIcon.getIconHeight()) / 2;
          todayIcon.paintIcon(this, g, x, y);
        }
      }
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -