gpt4 book ai didi

java - JFormattedTextField 与 MaskFormatter 和 DateFormat

转载 作者:行者123 更新时间:2023-11-29 05:44:15 26 4
gpt4 key购买 nike

看完this articlethis question我试图将两者结合起来:JFormattedTextField,它始终在正确的位置显示斜杠,并自动解析 Date 对象。

我想出的代码如下:

private DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
textField_DaRiassuntoIncassi = new JFormattedTextField(df);
textField_ARiassuntoIncassi = new JFormattedTextField(df);
textField_DaScadenze = new JFormattedTextField(df);
textField_AScadenze = new JFormattedTextField(df);
textField_DaRiassuntoIncassi.setColumns(10);
textField_ARiassuntoIncassi .setColumns(10);
textField_DaScadenze .setColumns(10);
textField_AScadenze .setColumns(10);

try
{
MaskFormatter dateMask = new MaskFormatter("##/##/####");
dateMask.install(textField_DaRiassuntoIncassi);
dateMask.install(textField_ARiassuntoIncassi);
dateMask.install(textField_DaScadenze);
dateMask.install(textField_AScadenze);
}
catch(ParseException ex)
{
ex.printStackTrace();
}

问题是,当我单击文本字段输入值时,当我键入时,两个斜杠会随着我的键入而移动,相反,我希望它们保持固定(就像键盘上的“Insert”键一样被按下)。如果我将 MaskFormatter 放在构造函数中,问题就会消失,但我可以在文本字段中输入任何我想要的数字,例如“99/00/9874”,组件会告诉我这是一个不错的值,因为我不知道在哪里插入 SimpleDateFormat。

我最后的办法是将 MaskFormatter 放在 JFormattedTextField 构造函数中,使用 getText() 方法获取文本,尝试使用 DateFormat 解析日期,并在出现错误的情况下做一些事情,但我认为有一个聪明的方法做这个。我尝试使用方法

textField_AScadenze.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(new SimpleDateFormat("dd/MM/yyyy"))));

但是只要我不插入任何内容并单击,斜杠就消失了。请帮忙。谢谢

最佳答案

  • 使用 JSpinner with SpinnerDateModel (根据记录,您可以删除两个 JButton,但非常适合优秀的编码人员)

  • 例如(不会采用这种方式,JFormattedTextField 在某些情况下可能会过大)

.

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.text.MaskFormatter;

public class TimeFormatter extends MaskFormatter {

private static final long serialVersionUID = 1L;

public TimeFormatter() { // set mask and placeholder
try {
setMask("##/##/####");
setPlaceholderCharacter('0');
setAllowsInvalid(false);
setOverwriteMode(true);
} catch (ParseException e) {
e.printStackTrace();
}
}

@Override
public Object stringToValue(String string) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
if (string == null) {
string = "00/00/0000";
}
return df.parse(string);
}

@Override
public String valueToString(Object value) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
if (value == null) {
value = new Date(0);
}
return df.format((Date) value);
}

private void MyGui() {
final MaskFormatter formatter = new TimeFormatter(); // textfield 1: create formatter and textfield
//formatter.setValueClass(java.util.Date.class);
final JFormattedTextField tf2 = new JFormattedTextField(formatter);// textfield 2: create formatter and textfield
tf2.setValue(new Date()); // no initial value
final JLabel label = new JLabel();
JButton bt = new JButton("Show Value");// button to show current value
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(" value 2 = " + tf2.getValue());
System.out.println(" value 2 = " + tf2.getText());
System.out.println("value class: " + formatter.getValueClass());
label.setText(tf2.getText());
}
});
JFrame f = new JFrame(); // main frame
f.getContentPane().setLayout(new GridLayout());
f.getContentPane().add(tf2);
f.getContentPane().add(label);
f.getContentPane().add(bt);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

public static void main(String[] args) throws Exception {
Runnable doRun = new Runnable() {
@Override
public void run() {
new TimeFormatter().MyGui();
}
};
SwingUtilities.invokeLater(doRun);
}
}

关于java - JFormattedTextField 与 MaskFormatter 和 DateFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16386661/

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