我是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());
}
答案 0 :(得分:2)
也许您正在寻找JFormattedTextField?
http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
答案 1 :(得分: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();
}
}
});
答案 2 :(得分:1)
将JFormattedTextField与数据格式
一起使用见
http://www.kodejava.org/examples/234.html
和
http://www.java2s.com/Tutorial/Java/0240__Swing/JFormattedTextFieldwithSimpleDateFormat.htm
答案 3 :(得分:0)
您可以放置三个不同的下拉框来指定日,月和年。您无需检查其他验证。如果该月有31个日期,闰年检查和订单日期&lt;交货日期。
答案 4 :(得分:0)
基于Mohammod Hossain's answer,我写了这个简单的类。我希望它可以提供帮助。
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());
}
}