다른 날 저는 here이라는 질문을하고 적절한 대답을 받았습니다. 그러나 샘플 코드를 내 프로그램으로 변환하여 내 필요에 맞게 변형하려고 시도했을 때 제대로 작동하지 않았습니다.JFormatted 필드 포커스가 굴절되어 있지 않을 때
두 세트의 코드를 여러 번 살펴 봤는데 실제로 차이점은 내 코드가 상자 대신 패널을 사용한다는 것입니다. 그러나 성능 차이를 설명하는 설명서는 InputVerifier
. 내 프로그램은 관대 한 날을 허용해서는 안되지만 JFormattedField
이 포커스를 가져야 할 때 허용하도록 허용합니다. 내 문제는 어디에 있습니까?
내 현재 코드 : 이전 질문에서 대답에서
public class Hello implements ActionListener{
private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private Date endingDate = new Date();
private String endingString = null;
private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
private JFormattedTextField formattedField = null;
private JLabel label1 = new JLabel();
private JLabel label2 = new JLabel();
private TextArea ta = new TextArea();
private Button b = new Button("click");
public Hello() {
b.addActionListener(this);
label1 = new JLabel("test");
label2 = new JLabel("test");
formattedField = createFormattedTextField();
format.setLenient(false);
panel.add(formattedField);
panel.add(label1);
panel.add(label2);
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");
}
return valid;
}
};
return verifier;
}
public boolean isValidDate(String dateString) {
try {
format.parse(dateString);
return true;
} catch (ParseException ex) {
return false;
}
}
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);
}
}
코드 :
public class InputVerifyDate {
private SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
public InputVerifyDate() {
JFormattedTextField formattedField = createFormattedTextField();
JTextField field = new JTextField(10);
format.setLenient(false);
Box box = Box.createVerticalBox();
box.add(formattedField);
box.add(Box.createVerticalStrut(10));
box.add(field);
box.setBorder(new EmptyBorder(10, 10, 10, 10));
JFrame frame = new JFrame();
frame.add(box);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JFormattedTextField createFormattedTextField() {
JFormattedTextField formattedField = null;
try {
MaskFormatter dateMask = new MaskFormatter("##/##/####");
formattedField = new JFormattedTextField(dateMask);
} catch (ParseException ex) {
Logger.getLogger(InputVerifyDate.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 mm/dd/yyyy");
}
return valid;
}
};
return verifier;
}
public boolean isValidDate(String dateString) {
try {
format.parse(dateString);
return true;
} catch (ParseException ex) {
return false;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new InputVerifyDate();
}
});
}
}
관심이 있으신 경우 정확하게 복사하면 샘플 코드가 작동합니까? – Jamey
예, 수입해야 할 것을 가져온 후에는 공간을 줄이기 위해 퇴출 시켰습니다. –