1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package Ecouteurs;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.*;
import java.io.*;
import javax.swing.JTextField;
public class EcouteurClavier implements KeyListener
{
public void keyPressed(KeyEvent ec)
{
if (((JTextField) ec.getSource()).getText().equals("JJ/MM/AAAA"))
{
((JTextField) ec.getSource()).setText("");
}
if (ec.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
public void keyReleased(KeyEvent ec) {}
public void keyTyped(KeyEvent ec)
{
try
{
int i = Integer.parseInt(((String)(((JTextField) ec.getSource()).getText()).substring(0,2)));
if ( (Character.isDigit(ec.getKeyChar())==false) || (((JTextField) ec.getSource()).getText()).length() > 9 || i > 31)
{
ec.consume();
}
else
{
if (((JTextField) ec.getSource()).getText().length()==2)
{
((JTextField) ec.getSource()).setText(((JTextField) ec.getSource()).getText()+"/");
}
if (((JTextField) ec.getSource()).getText().length()==5)
{
((JTextField) ec.getSource()).setText(((JTextField) ec.getSource()).getText()+"/");
}
if (((JTextField) ec.getSource()).getText().length()==1)
{
System.out.println(i);
}
}
} catch (IndexOutOfRangeException) {System.out.println("erreur");}
/*
else if (Integer.parseInt(((String)(((JTextField) ec.getSource()).getText()).substring(0,2))) > 3)
{
System.out.println(((JTextField) ec.getSource()).getText());
}*/
}
} |
Partager