【问题标题】:JavaFX - TextField input validation of multiple statementsJavaFX - 多个语句的文本字段输入验证
【发布时间】:2016-12-07 19:13:29
【问题描述】:

我试图弄清楚,当我需要例如日期输入时,如何对文本字段进行输入验证。它需要采用类似 31.8.2016 的格式,并且必须是今天或更晚的日期。所以这是两个不同的检查。

我能想到的唯一方法是创建布尔方法来检查日期是过去还是未来。但我需要创建一个新方法来检查它是否采用正确的格式,这会更难,但我认为这不是正确的方法。

【问题讨论】:

  • 为什么我的 Scene Builder 库中没有 Date Picker 标签?
  • 你有哪个版本的 SceneBuilder?
  • 是的,这就是问题所在.. 我有 1.1.. 我已经有 Scene Builder 2.0,现在可以了,谢谢

标签: java validation input javafx textfield


【解决方案1】:

我通常会创建一个如下所示的日期类:

公开课日期{

    private boolean validDay = false;
    private boolean validMonth = false;
    private boolean validYear = false;
    private int day;
    private int month;
    private int year;
    private Calendar cal = Calendar.getInstance();

    public Date(int day, int month, int year){
        /*
         *All of these conditions can also be individually used to check the conditions you 
         *want to check. simply create 3 texfields and put them in a HBox all values must be entered
         *separately !
         */
        if(day>=cal.get(Calendar.DAY_OF_MONTH) && day<=cal.getActualMaximum(Calendar.DAY_OF_MONTH)){
            this.day = day;
            this.validDay = true;
        }
        else{
            //TODO: handle
        }
        if(month>=cal.get(Calendar.MONTH) && month<=12){
            this.month = month;
            this.validMonth= true;
        }
        else{
            //TODO: handle
        }
        if(year>=cal.get(Calendar.YEAR)){
            this.year = year;
            this.validYear = true;
        }
        else{
            //TODO: handle
        }
    }
    public boolean isDateValid(){
        return validDay && validMonth && validYear;
    }
    public String getDate(){
        return day+"."+month+"."+year;
    }
    public int getDay() {
        return day;
    }
    public int getMonth() {
        return month;
    }
    public int getYear() {
        return year;
    }
}

您可以使用字符串拆分器和板条箱日期拆分输入! 您还可以添加设置方法! 有很多方法可以做到这一点! 我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多