Guest User

Untitled

a guest
Dec 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.HeadlessException;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7.  
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JTextField;
  14.  
  15. public class ajoutAdresse extends JFrame implements ActionListener, KeyListener {
  16.  
  17. JLabel label1 = new JLabel("Rue : ");
  18. JLabel label2 = new JLabel("Ville :");
  19. JLabel label3 = new JLabel("Code postal :");
  20. JTextField text1 = new JTextField();
  21. JTextField text2 = new JTextField();
  22. JTextField text3 = new JTextField();
  23. JButton bouton1 = new JButton("Quitter");
  24. JButton bouton2 = new JButton("Ajouter");
  25. JButton bouton3 = new JButton("Chercher");
  26.  
  27. public ajoutAdresse() throws HeadlessException {
  28. this.setVisible(true); // pour afficher la fenetre
  29. this.setSize(300, 500); // pour la taille de la fenetre
  30. this.setLocationRelativeTo(null); // pour centrer
  31.  
  32. /* Les positions */
  33. label1.setBounds(50, 100, 100, 20);
  34. label2.setBounds(50, 150, 100, 20);
  35. label3.setBounds(50, 200, 100, 20);
  36. text1.setBounds(150, 100, 100, 20);
  37. text2.setBounds(150, 150, 100, 20);
  38. text3.setBounds(150, 200, 100, 20);
  39. bouton1.setBounds(20, 250, 100, 50);
  40. bouton2.setBounds(170, 250, 100, 50);
  41. bouton3.setBounds(170, 350, 100, 50);
  42.  
  43. this.setLayout(null); // desactive le mode layout de l'interface, ici permet de ne pas avoir le bouton
  44. // en gros
  45. this.add(label1);
  46. this.add(label2);
  47. this.add(label3);
  48. this.add(text1);
  49. this.add(text2);
  50. this.add(text3);
  51. this.add(bouton1);
  52. this.add(bouton2);
  53. this.add(bouton3);
  54.  
  55. bouton1.addActionListener(this); // espion sur le bouton
  56. bouton2.addActionListener(this);
  57. bouton3.addActionListener(this);
  58.  
  59. text1.addKeyListener(this); // espion sur les champs de text
  60. text2.addKeyListener(this);
  61. text3.addKeyListener(this);
  62.  
  63. bouton1.setBackground(Color.red);
  64. bouton2.setBackground(Color.green);
  65.  
  66. bouton1.setEnabled(true);
  67. bouton2.setEnabled(true);
  68. bouton3.setEnabled(true);
  69.  
  70. }
  71.  
  72. @Override
  73. public void actionPerformed(ActionEvent x) { // methode d'action du bouton "e" envoi le bouton qui appel la methode
  74. if (x.getSource() == bouton1) {
  75.  
  76. dispose();
  77. }
  78.  
  79. if (x.getSource() == bouton2) {
  80.  
  81. Adresse a;
  82. try {
  83. a = new Adresse(text1.getText(), text2.getText(), text3.getText());
  84. a.EnregistrerAdresse();
  85. } catch (CodePostalException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. if (x.getSource() == bouton3) {
  90. JOptionPane job = new JOptionPane();
  91.  
  92. if(Adresse.ChercheCodePostal(text3.getText())==true) {
  93. JOptionPane.showMessageDialog(null,"Trouvé");
  94. }else {
  95. job.showMessageDialog(null,"Pas trouvé");
  96. }
  97.  
  98. }
  99. }
  100.  
  101. @Override
  102. public void keyPressed(KeyEvent arg0) {
  103. // TODO Auto-generated method stub
  104. if (text1.getText().isEmpty() || text2.getText().isEmpty()) {
  105. bouton1.setEnabled(false); // désactive le bouton
  106. bouton2.setEnabled(false);
  107.  
  108. } else {
  109. bouton1.setEnabled(true); // active le bouton
  110. bouton2.setEnabled(true);
  111.  
  112. }
  113.  
  114. }
  115.  
  116. @Override
  117. public void keyReleased(KeyEvent arg0) {
  118. // TODO Auto-generated method stub
  119.  
  120. if (text1.getText().isEmpty() || text2.getText().isEmpty() || text3.getText().isEmpty()) {
  121. bouton1.setEnabled(false); // désactive le bouton
  122. bouton2.setEnabled(false);
  123.  
  124. } else {
  125. bouton1.setEnabled(true); // active le bouton
  126. bouton2.setEnabled(true);
  127.  
  128. }
  129. }
  130.  
  131. @Override
  132. public void keyTyped(KeyEvent arg0) {
  133. // TODO Auto-generated method stub
  134. if (text1.getText().isEmpty() || text2.getText().isEmpty() || text3.getText().isEmpty()) {
  135. bouton1.setEnabled(false); // désactive le bouton
  136. bouton2.setEnabled(false);
  137.  
  138. } else {
  139. bouton1.setEnabled(true); // active le bouton
  140. bouton2.setEnabled(true);
  141.  
  142. }
  143. }
  144.  
  145. }
Add Comment
Please, Sign In to add comment