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

3 つの異なるドロップダウン ボックスを配置して、日、月、年を指定できます。他の検証を確認する必要はありません。その月の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 に答える