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

📄 dscomboxdatepopup.java

📁 用JAVA编写的绘图程序 功能简介: 支持存储
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package drawsmart.itsv.basic;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import javax.swing.border.*;
import com.sun.java.swing.plaf.motif.*;
import com.sun.java.swing.plaf.windows.*;
import java.net.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
public abstract class DSComBoxDatePopup
    extends JComboBox {
  /**
   * 年月日格式
   */
  public static final SimpleDateFormat dateFormat
      = new SimpleDateFormat("yyyy-MM-dd");

  /**
   * 年月格式
   */
  public static final SimpleDateFormat monthFormat
      = new SimpleDateFormat("yyyy-MM");

  /**
   * 日格式
   */
  public static final SimpleDateFormat dayFormat
      = new SimpleDateFormat("d");

  /**
   * 构造式
   */
  public DSComBoxDatePopup() {
    setEditable(true);
    JTextField textField = ( (JTextField)this.getEditor().getEditorComponent());
    textField.setDocument(new JDateDocument(textField));
    setSelectedItem(dateFormat.format(new Date()));
  }

  /**
   * 取得当前选择的日期
   * @return Date
   */
  public Date getSelectedDate() {
    try {
      return dateFormat.parse(this.getSelectedItem().toString());
    }
    catch (Exception ex) {
      return new Date();
    }
  }

  /**
   * 设置当前选取的日期
   * @param item Object
   */
  public void setSelectedItem(Object item) {
    removeAllItems();
    addItem(item);
    super.setSelectedItem(item);
  }

  /**
   * 更新UI
   */
  public void updateUI() {
    ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
    if (cui instanceof MetalComboBoxUI) {
      cui = new MetalDateComboBoxUI();
    }
    else if (cui instanceof MotifComboBoxUI) {
      cui = new MotifDateComboBoxUI();
    }
    else if (cui instanceof WindowsComboBoxUI) {
      cui = new WindowsDateComboBoxUI();
    }
    setUI(cui);
  }

  // UI Inner classes -- one for each supported Look and Feel
  /**
   * <p>Title: OpenSwing</p>
   * <p>Description: MetalDateComboBoxUI</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: </p>
   * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
   * @version 1.0
   */
  class MetalDateComboBoxUI
      extends MetalComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup(comboBox);
    }
  }

  /**
   *
   * <p>Title: OpenSwing</p>
   * <p>Description: WindowsDateComboBoxUI</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: </p>
   * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
   * @version 1.0
   */
  class WindowsDateComboBoxUI
      extends WindowsComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup(comboBox);
    }
  }

  /**
   *
   * <p>Title: OpenSwing</p>
   * <p>Description: MotifDateComboBoxUI</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: </p>
   * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
   * @version 1.0
   */
  class MotifDateComboBoxUI
      extends MotifComboBoxUI {
    protected ComboPopup createPopup() {
      return new DatePopup(comboBox);
    }
  }

  /**
   * <p>Title:JDateDocument </p>
   * <p>Description: JDateDocument 实现日期的输入格式限制</p>
   * <p>Copyright: Copyright (c) 2004</p>
   * <p>Company: </p>
   * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>,recoon
   * @version 1.0
   */
  public static class JDateDocument
      extends PlainDocument {
    private JTextComponent textComponent; //日期输入文本框
    private int newOffset; //新的插入位置

    /******************************************************************
     ** 函数名称:public JDateDocument(JTextComponent tc)
     ** 功能描述:设置此文本框显示的默认值和格式限制
     ** 入口参数:tc : JTextComponent类型,当前操作的文本框
     ** 返回值:无
     ** 调用者:类JDateDocument
     *******************************************************************/
    public JDateDocument(JTextComponent tc) {
      //保存操作的文本框
      textComponent = tc;
      //获取当前日期
      String strCurrentDate = getCurrentDate();
      //设置显示为当前日期,同时完成显示的格式化
      try {
        insertString(0, strCurrentDate, null);
      }
      catch (Exception ex) {
        System.out.println(ex);
      }
    }

    /******************************************************************
     ** 函数名称:public void insertString(int offset, String s,
     **             AttributeSet attributeSet) throws BadLocationException
     ** 功能描述:重载原方法,限制用户插入格式为日期格式
     ** 入口参数:offset: int型,插入位置
     **            s: String型,插入字符串
     **            attributeSet: AttributeSet型,属性集
     ** 返回值:无
     ** 调用者:类JDateDocument
     *******************************************************************/
    public void insertString(int offset, String s,
                             AttributeSet attributeSet) throws
        BadLocationException {
      String toTest; //用于测试输入合法性的字符串
      //判断插入字符串长度
      if (s.length() == 1) {
        //长度为1
        try {
          //限制输入为整数
          Integer.parseInt(s);
        }
        catch (Exception ex) {
          //错误则提示并返回
          Toolkit.getDefaultToolkit().beep();
          return;
        }
        //取得原始插入位置
        newOffset = offset;
        //如果插入位置为"-"符号的前面,则移动到其后面插入(改变newOffset的值)
        if (offset == 4 || offset == 7) {
          newOffset++;
          textComponent.setCaretPosition(newOffset);
        }
        //如果插入位置为最后,则不插入
        if (offset == 10)return;
        //取得显示的时间,处理后得到要显示的字符串
        toTest = textComponent.getText();
        toTest = toTest.substring(0, newOffset) + s +
            toTest.substring(newOffset + 1, 10);
        //如果要显示的字符串合法,则显示,否则给出提示并退出
        if (!isLegalDate(toTest)) {
          Toolkit.getDefaultToolkit().beep();
          return;
        }
        //插入字符串
        super.remove(newOffset, 1);
        super.insertString(newOffset, s, attributeSet);
      }
      //如果插入长度10
      else if (s.length() == 10) {
        //合法则显示,否则给出提示退出
        if (!isLegalDate(s)) {
          Toolkit.getDefaultToolkit().beep();
          return;
        }
        //插入字符串
        super.remove(0, getLength());
        super.insertString(0, s, attributeSet);
      }
    }

    /**********************************************************************************
     ** 函数名称:public void remove(int offset, int length) throws BadLocationException
     ** 功能描述:重载原方法,删除合适位置的字符串
     ** 入口参数:offset: int型,插入位置
     **            length: int型,删除长度
     ** 返回值:无
     ** 调用者:insertString(int, String,AttributeSet)
     ***********************************************************************************/
    public void remove(int offset, int length) throws BadLocationException {
      //如果插入位置在"-"前,则回退一个光标位置
      if (offset == 4 || offset == 7)
        textComponent.setCaretPosition(offset - 1);
      else
        textComponent.setCaretPosition(offset);
    }

    /**********************************************************************************
     ** 函数名称:public boolean isLegalDate(String strDate)
     ** 功能描述:判断插入的长度为10的字符串是否合法
     ** 入口参数:intY: int型,年的值
     **            intM: int型,月的值
     **            intD: int型,日的值
     ** 返回值:boolean型,真,表示是合法的,假,表示不合法
     ** 调用者:insertString(int, String,AttributeSet)
     ***********************************************************************************/
    public boolean isLegalDate(String strDate) {
      int intY, intM, intD; //年,月,日的值
      int iCaretPosition; //光标位置
      //获取字符串
      strDate = strDate.trim();
      //如果为空,长度不对,则为非法,返回false
      if (strDate == null || strDate.trim().equals("") ||
          strDate.trim().length() != 10)
        return false;
      //如果是全角字符,则返回false
      for (int i = 0; i < 10; i++)
        if ( ( (int) strDate.charAt(i)) > 255)
          return false;
      //取年,月,日的值
      try {
        intY = Integer.parseInt(strDate.substring(0, 4));
        intM = Integer.parseInt(strDate.substring(5, 7));
        intD = Integer.parseInt(strDate.substring(8));
      }
      catch (Exception e) {
        //失败则返回false
        return false;
      }
      //数值越界,则修改至正确值
      //保存光标位置
      iCaretPosition = textComponent.getCaretPosition();
      //年越界
      if (intY > 2999) {
        //修改为合法值
        textComponent.setText("2999" + strDate.substring(5));
        //手动设置光标位置
        textComponent.setCaretPosition(iCaretPosition + 1);
        return false;
      }
      if (intY < 1000) {
        textComponent.setText("1000" + strDate.substring(5));
        textComponent.setCaretPosition(iCaretPosition + 1);
        return false;
      }
      //月越界
      if (intM > 12) {
        textComponent.setText(strDate.substring(0, 5) + "12" +
                              strDate.substring(7));
        textComponent.setCaretPosition(iCaretPosition + 1);
        return false;
      }
      if (intM < 1) {
        textComponent.setText(strDate.substring(0, 5) + "01" +
                              strDate.substring(7));
        textComponent.setCaretPosition(iCaretPosition + 1);
        return false;
      }
      //根据月份,判断日期输入,如越界,则修改
      switch (intM) {
        //最大天数为31天
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:

          //如果输入大于31,则修改为31
          if (intD > 31) {
            textComponent.setText(strDate.substring(0, 8) + "31");
            textComponent.setCaretPosition(iCaretPosition + 1);
          }
          if (intD < 1) {
            textComponent.setText(strDate.substring(0, 8) + "01");
            textComponent.setCaretPosition(iCaretPosition + 1);
          }
          return (intD <= 31 && intD > 0);
          //最大天数为30天
        case 4:
        case 6:
        case 9:
        case 11:

          //如果输入大于30,则修改为30
          if (intD > 30) {
            textComponent.setText(strDate.substring(0, 8) + "30");
            textComponent.setCaretPosition(iCaretPosition + 1);
          }
          if (intD < 1) {
            textComponent.setText(strDate.substring(0, 8) + "01");
            textComponent.setCaretPosition(iCaretPosition + 1);

⌨️ 快捷键说明

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