【发布时间】:2021-08-07 15:15:56
【问题描述】:
我有一个JFormattedTextField,格式为dd/MM/yyyy
我想提取日期的年月日,格式为dd/MM/yyyy,时间日期“ES”(西班牙)。
我运行JFrame,如果我按button,JFormattedTextField 为空,为什么会出现以下错误?
错误(见图):
我该如何解决?我会展示我的代码...
代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
public class Ventana extends JFrame implements ActionListener {
public JLabel label_date, label_calculo;
public JFormattedTextField date;
public JButton button;
public Ventana() throws ParseException{
super();
configurarVentana();
inicializarComponentes();
}
private void configurarVentana() {
this.setTitle("Example");
this.setSize(600, 480);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void inicializarComponentes() throws ParseException{
label_date = new JLabel();
label_calculo = new JLabel();
date = new JFormattedTextField();
button = new JButton();
label_date.setText("Add date:");
label_date.setBounds(50, 50, 100, 25);
date.setBounds(150, 50, 160, 25);
label_calculo.setText("Age: ");
label_calculo.setBounds(50, 90, 300, 25);
button.setBounds(320, 50, 130, 25);
button.setText("Calculate age");
this.add(label_date);
this.add(date);
this.add(button);
this.add(label_calculo);
button.addActionListener(this);
//Format: JFormattedTextField dd-MM-yyyy
date.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##/##/####")));
}
public void actionPerformed(ActionEvent e) {
//JAVA 8.
LocalDate ahora = LocalDate.now();
int dia_hoy = ahora.getDayOfMonth();
int mes_hoy = ahora.getMonthValue();
int ano_hoy = ahora.getYear();
System.out.println(dia_hoy+" "+mes_hoy+" "+ano_hoy);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate fechaNac = LocalDate.parse(date.getText(), fmt);
int dia_nac = fechaNac.getDayOfMonth();
int mes_nac = fechaNac.getMonthValue();
int ano_nac = fechaNac.getYear();
Period periodo = Period.between(fechaNac, ahora);
if(date.getText().trim().isEmpty()){
JOptionPane.showMessageDialog(null, "Date empty", "Administrador", JOptionPane.WARNING_MESSAGE);
}else if(dia_nac<1 || dia_nac>31){
JOptionPane.showMessageDialog(null, "Day incorrect", "Administrador", JOptionPane.WARNING_MESSAGE);
}else if(mes_nac<1 || mes_nac>12){
JOptionPane.showMessageDialog(null, "Month incorrect.", "Administrador", JOptionPane.WARNING_MESSAGE);
}else if(ano_nac<1900 || ano_nac>ano_hoy){
JOptionPane.showMessageDialog(null, "Year incorrect.", "Administrador", JOptionPane.WARNING_MESSAGE);
}else{
label_calculo.setText("You're "+String.valueOf(periodo.getYears())+" years old.");
}
}
public static void main(String[] args) throws ParseException {
Ventana V = new Ventana();
V.setVisible(true);
}
}
【问题讨论】:
-
看起来您从文本字段中获得了`//`。所有日期字段均为空白
-
@Jens 感谢您的帮助,但是...如果我运行 JFrame,但没有填充 JFormattedTextField,我会遇到此错误。为什么?
-
因为它试图在这一行解析它
LocalDate fechaNac = LocalDate.parse(formattedtextfield.getText(), fmt); -
@Jens So.. 我该如何解决?
if(formattedtextfield.getText().trim().isEmpty()){这不起作用。 -
检查字符串中是否有除 blan 和 slash 以外的其他字符,是否将其分开,或者捕获异常并返回如果没有有效日期则显示错误消息
标签: java swing localdate jformattedtextfield