在我的项目中,我有一个JTextField
来输入日期。我有一个InputVerifier
来确保输入正确的信息并且也可以使用它进行一些工作。我需要的一件事就是从输入日期获取DAY_OF_WEEK
。我是这样做的
Calendar calTest = Calendar.getInstance();
calTest.setTime(primer);
int day = calTest.get(Calendar.DAY_OF_WEEK);
然后我将其打印到屏幕上,无论输入的是什么日期,显示的数字都是4
,但是当我单击该字段并单击返回而不进行更改时,正确的数字将打印到屏幕上。我无法弄清楚为什么会这样?特别是在字段失去焦点后立即正确打印字符串形式的日期。
以下是发生这种情况的方法:
private void updatePrimer(String dateString) {
Calendar calTest = Calendar.getInstance();
calTest.setTime(primer);
int day = calTest.get(Calendar.DAY_OF_WEEK);
try {
primer = simpleFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(day);
shortString = shortFormat.format(primer);
System.out.println(shortString);
labelTrip = true;
stringTrip = dateString;
updateLabels(labelTrip, stringTrip);
}
这是一个小的可运行程序,我一直使用它作为沙箱:
编辑这只是我在实现主程序之前一直使用的“沙箱”程序,因此我可以测试基本功能。我需要的最终结果是让JTextField
接受一个字符串,然后将其格式化为MM/dd/yyyy
格式的日期。使用此日期,JLabel
将使用相同的日期进行更新,但格式为MM/dd
。此外,它旁边的JLabel
将更新日期减去上一日期的一天,然后与剩余的JLabel进行相同的处理。颜色的变化用于帮助我可视化程序的操作顺序。 textarea只是为了让我点击一些东西来改变焦点。该按钮只打印在JTextField
重要的是,这里有什么问题是为什么在updatePrimer(String dateString)
中,只有在第二次JTextField获得焦点时才能正确打印一周的正确日期
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.MaskFormatter;
public class Hello implements ActionListener{
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private Date endingDate = new Date();
private Date primer = new Date();
private String endingString = null;
private SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy");
private SimpleDateFormat shortFormat = new SimpleDateFormat("MM/dd");
private JFormattedTextField weekEndingData = null;
private JLabel dateData[] = new JLabel[3];
private JTextArea ta = new JTextArea(20, 10);
private JButton b = new JButton("click");
private String shortString = null;
private boolean labelTrip = false;
private String stringTrip = null;
private Calendar cal = Calendar.getInstance();
private Color blank = Color.LIGHT_GRAY;
public Hello() {
b.addActionListener(this);
weekEndingData = createFormattedTextField();
simpleFormat.setLenient(false);
panel.add(weekEndingData);
for (int i=0; i<3; i++){
dateData[i] = new JLabel("12/34");
dateData[i].setForeground(blank);
panel.add(dateData[i]);
}
panel.add(ta);
panel.add(b);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Hello();
}
});
System.out.println("Hello, World");
}
private JFormattedTextField createFormattedTextField() {
JFormattedTextField formattedField = null;
try {
MaskFormatter dateMask = new MaskFormatter("##/##/####");
formattedField = new JFormattedTextField(dateMask);
} catch (ParseException ex) {
Logger.getLogger(Hello.class.getName()).log(Level.SEVERE, null, ex);
}
formattedField.setColumns(10);
formattedField.setInputVerifier(getInputVerifier());
return formattedField;
}
private InputVerifier getInputVerifier() {
InputVerifier verifier = new InputVerifier() {
@Override
public boolean verify(JComponent input) {
JFormattedTextField field = (JFormattedTextField) input;
String text = field.getText();
return isValidDate(text);
}
@Override
public boolean shouldYieldFocus(JComponent input) {
boolean valid = verify(input);
if (!valid) {
JOptionPane.showMessageDialog(null, "Please enter a valid date in format dd/mm/yyyy");
weekEndingData.setText("");
}
return valid;
}
};
return verifier;
}
public boolean isValidDate(String dateString) {
if (dateString.equals(" / / ")){
resetLabels();
return true; //returns true in order to allow user to accidentally click field and still yield focus
}
else{
try {
simpleFormat.parse(dateString);
updatePrimer(dateString);
return true;
} catch (ParseException ex) {
resetLabels();
return false;
}
}
}
private void updatePrimer(String dateString) {
Calendar calTest = Calendar.getInstance();
calTest.setTime(primer);
int day = calTest.get(Calendar.DAY_OF_WEEK);
try {
primer = simpleFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(day);
shortString = shortFormat.format(primer);
System.out.println(shortString);
labelTrip = true;
stringTrip = dateString;
updateLabels(labelTrip, stringTrip);
}
private void resetLabels() {
weekEndingData.setValue(null);
labelTrip = false;
updateLabels(labelTrip, stringTrip);
}
private void updateLabels(boolean trip, String date) {
boolean validUpdate = trip;
if (validUpdate == false){
for (int i = 0; i < 3; i++){
dateData[i].setText("12/34");
dateData[i].setForeground(blank);
}
}
else if (validUpdate == true){
try {
cal.setTime(simpleFormat.parse(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int day = -1;
for (int i = 0; i < 3; i++){
cal.add(Calendar.DATE, day);
dateData[i].setText(shortFormat.format(cal.getTime()));
dateData[i].setForeground(Color.GREEN);
}
}
}
public void actionPerformed(ActionEvent e) {
/*System.out.println("Action performed");
System.out.println(formattedField);
endingDate = (Date) formattedField.getValue();
System.out.println(endingDate);
endingString = format.format(endingDate);
System.out.println(endingString);*/
System.out.println(weekEndingData.getText());
}
}
答案 0 :(得分:0)
打印出我的代码并通过程序跟踪我的数据后,我发现了我的问题。
在
private void updatePrimer(String dateString) {
Calendar calTest = Calendar.getInstance();
calTest.setTime(primer);
int day = calTest.get(Calendar.DAY_OF_WEEK);
try {
primer = simpleFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(day);
shortString = shortFormat.format(primer);
System.out.println(shortString);
labelTrip = true;
stringTrip = dateString;
updateLabels(labelTrip, stringTrip);
}
行:
calTest.setTime(primer);
int day = calTest.get(Calendar.DAY_OF_WEEK);
需要在<{strong> primer = simpleFormat.parse(dateString);
try-catch块之后移动。我的问题是我在设定日期之前设定了时间。