gpt4 book ai didi

java - 使用 SpinDateModel 在 JSpinner 中处理日期格式

转载 作者:行者123 更新时间:2023-11-30 06:17:31 26 4
gpt4 key购买 nike

我有以下几行代码:

    SpinnerDateModel spinMod=new SpinnerDateModel();
JSpinner spin=new JSpinner(spinMod);
//spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));

当我显示微调器时,我可以编辑它并在月份字段中写入我想要的任何两位数。

如果我单击 JSpinner 端,我只能增加和减少月份或日期数字,以便它们的值是合法的。但是,如果我添加最后一条取消注释的行,这也行不通。

问题是如何让 JSpinner 只接受具有自定义格式的合法日期。

此外,如果我将 ActionListener 添加到获取日期的按钮,我会通过将 getValue() 直接应用于 JSpinner 来获得“正确的日期”,但是如果我将日期编辑器的格式应用于从 JSpinner 获得的值,则会出现错误(输入的那个)。我希望 Action 监听器以我的自定义格式获取正确的值,并且我希望 JSpinner 仅接受自定义格式的输入。这可能吗?

我的测试代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.text.*; //for the date format

public class TestDates{

JLabel jl=new JLabel("Date:");
JFrame jf=new JFrame("Test SpinnerDateFormat");

JButton jb=new JButton("Get Date");
SpinnerDateModel spinMod=new SpinnerDateModel();
JSpinner spin=new JSpinner(spinMod);

class jbHandler implements ActionListener{
public void actionPerformed(ActionEvent evt){
JSpinner.DateEditor de=(JSpinner.DateEditor)spin.getEditor();
String wrongDate=de.getFormat().format(spin.getValue());
Date okDate=(Date)spin.getValue();
System.out.println(">>>\n"+wrongDate+"\n"+okDate);
}
}

JPanel pane=new JPanel();

public TestDates(){

jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pane.add(jl);

jb.addActionListener(new jbHandler());
pane.add(jb);

spin.setValue(new Date());//this doesn't work properly with the custom format
spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));
pane.add(spin);

jf.add(pane);
jf.pack();
jf.setVisible(true);
}

public static void main(String[] args){

SwingUtilities.invokeLater(new Runnable(){
public void run(){
TestDates tdate=new TestDates();
}
});
}
}

最佳答案

If I click on the JSpinner side I can only increase and decrease the month or day numbers so that their values are legal. But, if I add the last outcommented line, this wouldn't work, either.

问题是您在这一行的日期模式中的一个小错误(但很常见):

spin.setEditor(new JSpinner.DateEditor(spin,"dd.mm.yyyy"));

在此模式中,“mm”指的是分钟而不是月,因此会导致不同的输出。正确的模式是:

spin.setEditor(new JSpinner.DateEditor(spin,"dd.MM.yyyy"));

The question is how to make the JSpinner accept only legal dates with the custom format.

如果你想阻止用户输入无效日期,那么你需要使用编辑器的格式化程序并禁止无效输入。请参阅以下代码段:

SpinnerDateModel model = new SpinnerDateModel();
JSpinner spinner = new JSpinner(model);

JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "dd.MM.yyyy");
DateFormatter formatter = (DateFormatter)editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false); // this makes what you want
formatter.setOverwriteMode(true);

自定义日期时间格式

参见 Customizing Formats跟踪。

另请参阅下表,摘自 SimpleDateFormat 中的日期和时间模式部分文档:

enter image description here

关于java - 使用 SpinDateModel 在 JSpinner 中处理日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com