0

我是 Java 新手,在获取Textfield. 我想将用户输入的日期输入数据库。我尝试过使用JCalender,但它不起作用,因为我未能配置调色板。除了 有没有其他选择JCalender?提前致谢。

这是我到目前为止所拥有的:

// Delivery Date Action

              tf3.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if (!((c >= '0') && (c <= '9') ||
         (c == KeyEvent.VK_BACK_SPACE) ||
         (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))        
      {
        JOptionPane.showMessageDialog(null, "Please Enter Valid");
        e.consume();
      }
    }
  });


        tf3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {
               int i = Integer.parseInt(tf3.getText());


            }
4

5 回答 5

2

也许您正在寻找 JFormattedTextField?

http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html

于 2012-12-18T06:51:00.037 回答
1
JFormattedTextField is a subclass of JTextField.

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
JFormattedTextField txtDate = new JFormattedTextField(df);

您可以在其上添加验证事件

txtDate .addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if (!((c >= '0') && (c <= '9') ||
         (c == KeyEvent.VK_BACK_SPACE) ||
         (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))        
      {
        JOptionPane.showMessageDialog(null, "Please Enter Valid");
        e.consume();
      }
    }
  });
于 2012-12-18T06:49:56.070 回答
1

将 JFormattedTextField 与数据格式一起使用

http://www.kodejava.org/examples/234.html

http://www.java2s.com/Tutorial/Java/0240__Swing/JFormattedTextFieldwithSimpleDateFormat.htm

于 2012-12-18T06:52:36.150 回答
0

您可以放置​​三个不同的下拉框来指定日、月和年。您无需检查其他验证。如果该月有 31 个日期,则闰年检查和订单日期 < 交货日期。

于 2012-12-18T06:46:54.053 回答
0

根据Mohammod Hossain 的回答,我编写了这个简单的课程。我希望它可以帮助。

public class JDateTextField extends JFormattedTextField{

private final String format;
private final char datesep;
private Component pare = null;

/**
 * Establece el componente padre para los mensajes de dialogo.
 * <p>
 * @param pare
 */
public void setPare(Component pare) {
    this.pare = pare;
}

public JDateTextField(final String format) {
    super(new SimpleDateFormat(format));
    this.format = format;
    setColumns(format.length());
    //if(format.contains("-")) datesep=KeyEvent.VK_MINUS;
    //else
    if (format.contains("/")) {
        datesep = KeyEvent.VK_SLASH;
    } else {
        datesep = KeyEvent.VK_MINUS;
    }

    addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            try {
                Date date = getDate();
            } catch(ParseException ex) {
                showError("Por favor introduzca una fecha válida.");
            }
        }
    });
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            String s = "" + c;
            if (s.matches("|[a-z]|i")){
                showError("Carácter no válido detectado " + s);
                e.consume();
            } else if(!((c >= '0') && (c <= '9')
                    ||(c == KeyEvent.VK_BACK_SPACE)
                    ||(c == KeyEvent.VK_DELETE)
                    ||(c == datesep)||(c == KeyEvent.VK_COLON))){
                showError("Carácter no válido detectado " + c);
                e.consume();
            }
            /*
             * else{
             * try{
             * Date date=getDate();
             * }catch(ParseException ex){
             * showError("Por favor introduzca una fecha válida.");
             * }
             * }
             */
        }
    });
    setValue(new Date());
}

private void showError(String error) {
    JOptionPane.showMessageDialog(pare, error + "\n\tEl patrón válido es: " + format, "Fecha NO válida", JOptionPane.ERROR_MESSAGE);
}

public Date getDate() throws ParseException {
    SimpleDateFormat frt = new SimpleDateFormat(format);
    return frt.parse(getText());
}

}

于 2014-10-29T09:41:01.027 回答