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
| package ActionListeners;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class ListenerAnonyme {
private JFrame jf=null;
private Button btn1=null;
private JLabel lbl1=null;
private JTextField txt1=null;
public ListenerAnonyme()
{
initialiser();
layout();
addComponent();
}
public void initialiser()
{
jf=new JFrame();
btn1=new Button("Valider");
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lbl1.setText("j'ai bien clicer sur le boutton");
}
});
lbl1=new JLabel("OUI/NON?");
txt1=new JTextField();
txt1.setBounds(50, 30, 30, 40);
txt1.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent k) {
}
public void keyTyped(KeyEvent k) {
if(k.getKeyChar()=='a')
lbl1.setText("ne tapper pas la lettre a");
}
});
jf.setVisible(true);
jf.setSize(400, 300);
jf.validate();
}
public void layout()
{
FlowLayout fl=new FlowLayout(FlowLayout.LEFT);
jf.setLayout(fl);
}
public void addComponent()
{
jf.add(btn1);
jf.add(lbl1);
jf.add(txt1);
}
} |
Partager