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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| import java.awt.*;
import java.io.*;
import javax.swing.*; // gerer l'interface graphique
import java.awt.event.*;// gérer les evenements
import java.lang.*;
public class GestionTextField extends JFrame
{ //déclaration des variables
private JFrame frameGestion;
private JPanel panelGestion;
private Container cGestion;
private JButton btGestion;
private JLabel lblGestion;
private JTextField txtGestion;
private int i=20, j=100; // les coordonnées i= l'axe des x / j= L'axe des y
//private JScrollPane scrolGestion;
/*<*><*><*><*><*><*><*><*>< - Constructeur - <*><*><*><*><*><*><*><*><*/
public GestionTextField()
{ //////////Instancier les variables ////////////
frameGestion = new JFrame("Gestion des TextField");
cGestion = frameGestion.getContentPane();
panelGestion = new JPanel();
btGestion = new JButton("Fermer");
lblGestion=new JLabel("Les TextField :");
txtGestion = new JTextField();
//scrol = new JScrollPane(panelGestion);
//////// Configurer les composantes /////////
cGestion.setLayout(null);
frameGestion.setSize(600, 600);
panelGestion.setLayout(null);
panelGestion.setBounds(0,0,600,600);
lblGestion.setBounds(20,20,140,30);
lblGestion.setFont(new Font("Dialog",Font.BOLD,18));
panelGestion.add(lblGestion);
btGestion.setBounds(230,520,100,30);
btGestion.setForeground(Color.black);
panelGestion.add(btGestion);
txtGestion.setBounds(20,60,500,30);
panelGestion.add(txtGestion);
panelGestion.setVisible(true);
frameGestion.getContentPane().add(panelGestion);
frameGestion.setVisible(true);
////La fermeture de la fenêtre
frameGestion.addWindowListener(new WindowListener()
{ public void windowOpened(WindowEvent e) { }
public void windowClosing(WindowEvent e)
{ frameGestion.setVisible(false);
}
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
});
////ecouteur des évenements de boutons
btGestion.addActionListener(boutonAct); //pour fermer la fenetre
txtGestion.addActionListener(ajouterTxt); //écouteur de saisie sur le TextField
}
/*<*><*><*><*> L'ecouteur sur le btGestion pr fermer la fenetre <*><*><*><*>*/
private ActionListener boutonAct = new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ if (e.getSource() == btGestion)
{ frameGestion.setVisible(false);
}
}
};
/*<*><*><*><* L'ecouteur sur le txtGestion pr ajouter TextField *><*><*><*>*/
private ActionListener ajouterTxt = new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ if (e.getSource() == txtGestion)
{ JTextField txtGestion1=new JTextField();
txtGestion.setBounds(i,j,500,30);
j=j+40;
panelGestion.add(txtGestion);
panelGestion.setVisible(true);
}
}
};
/*<*><*><*><*><*><*><*><*> La fonction Main <*><*><*><*><*><*><*><*><*><*>*/
public static void main(String[] args)
{
GestionTextField essaye = new GestionTextField();
}
} |
Partager