java 如何使jtextfield具有固定的日期格式?

a1o7rhls  于 2023-02-28  发布在  Java
关注(0)|答案(7)|浏览(189)

我在使用JTextField创建固定日期格式时遇到问题。有没有办法让JTextField具有固定日期格式?

ojsjcaue

ojsjcaue1#

可以将JFormattedTextField与SimpleDateFormat配合使用

DateFormat format = new SimpleDateFormat("your_format");
JFormattedTextField dateTextField = new JFormattedTextField(format);
wecizke3

wecizke32#

你应该看看

首先...

wrrgggsh

wrrgggsh3#

如果您使用的是Swing,请将JFormattedTextField添加到您的JFrame中。在“属性”中,单击formatterFactory。在对话框中,选择“日期类别”,然后选择“格式”。现在您的格式将被强制执行。

csbfibhn

csbfibhn4#

我在Netbeans中的解决方案是:从JDateChooser组件中选择日期,然后一个不可聚焦的jtextfield从JDateChooser中选择的日期中获取日期:(1)为了使日期格式固定,在JDateChooser的属性中,例如我们设置日期格式:yyyy-MM-dd ;(2)使“jtextfield.setFocusable(false)"有效,这是为了避免输入错误的日期格式或不需要的字符

第一节第二节第一节第三节第一节

xytpbqjk

xytpbqjk5#

正如评论中所说的,您可能更喜欢查看日期选择器组件,而不是文本字段,日期选择器组件将使用户不必纠结于日期语法。

Java.时间

我建议您使用java.time,这是一个现代的Java日期和时间API,用于日期工作。因此,对于喜欢使用文本字段作为日期的人,我做了一个小实验,将JFormattedTextField与java.time中的LocalDate类结合使用。对于文本字段,我发现最好编写一个小子类,指定从日期字段获得的对象类型为LocalDate

public class DateField extends JFormattedTextField {

    private static final long serialVersionUID = -4070878851012651987L;

    public DateField(DateTimeFormatter dateFormatter) {
        super(dateFormatter.toFormat(LocalDate::from));
        setPreferredSize(new Dimension(100, 26));
    }

    @Override
    public LocalDate getValue() {
        return (LocalDate) super.getValue();
    }

}

一个JFormattedTextField接受一个java.text.Format对象来格式化和解析值,来自java.time的DateTimeFormatter有两个重载的toFormat方法,这些方法给予了java.text.Format,甚至可以指定从Format得到的对象的类型。
要试用此日期字段类:

public class TestFrame extends JFrame {

    public TestFrame() {
        super("Test");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
                .withLocale(Locale.forLanguageTag("es"));
        DateField dateField = new DateField(dateFormatter);
        add(dateField);
        dateField.setValue(LocalDate.now(ZoneId.systemDefault()));

        JButton okButton = new JButton("OK");
        okButton.addActionListener(ev -> JOptionPane.showMessageDialog(TestFrame.this,
                "Date entered is " + dateField.getValue()));
        add(okButton );

        pack();
    }

    public static void main(String[] args) {
        new TestFrame().setVisible(true);
    }

}

我指定西班牙语格式只是为了清楚地演示格式化和解析的过程。在这里你可以指定你的用户喜欢哪种格式。例如DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)DateTimeFormatter.ofPattern("dd/MM/uu")。考虑使用短格式,因为大多数用户不喜欢输入多余的内容。
现在JFrame看起来像这样:

我输入11 ago. 2020而不是今天的日期,然后单击OK:

链接

Oracle tutorial: Date Time解释如何使用java.time。

deikduxw

deikduxw6#

使用属性formatterFactory并选择format.

3phpmpom

3phpmpom7#

我认为最好的方法是使用JFormatedTextField。
我有这个代码试试这个:

package your_package;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class.....{

private String getdate(){
      DateFormat format = new SimpleDateFormat("MM/DD/YYYY"); //display your format.
      Date date = new Date();//puts the date in variable.
      return dateformat.format(date); //returns the format to the date variable.
}

public your_app{
     .....
     String date = new getdate();
     txtDate.setvalue(date);
}
}

希望这能给予你一个想法和帮助...:)

相关问题