Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1672 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  repaint() & BorderLayout : bug d'affichage...

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

repaint() & BorderLayout : bug d'affichage...

n°2241636
sebastien4​444
Posté le 29-10-2014 à 11:30:53  profilanswer
 

Bonjour,
 
J'ai actuellement une appli qui permet l'ouverture/fermeture de certains ports de pare-feu.
3 classes : Connexion.java qui gère les connexion Telnet, Controleur.java qui gère les fonctionnalités (ouverture,fermeture,gestion de fichier log, actualisation...) et Vue.java qui gère l'affichage des éléments (labels, boutons, voyant d'état de connexion).
D'abord, je n'utilisais pas de Layout et "empilait" mes composant les uns à la suite des autres, en faisant attention (via la taille de la fenêtre et le fait qu'elle soit non-resizable) à ce que chaque nouveau label change de ligne ; problème, en fonction de la taille des labels, tout n'était pas forcément aligné :
http://imageshack.com/a/img540/9631/W1DNTp.jpg
 
J'ai donc opté pour l'utilisation d'un BorderLayout pour le JPanel principal, insérer des JPanel dans chacune des partie du BorderLayout (pour plus de flexibilité sur la taille des composants à mettre dedans) et enfin utiliser un GridLayout pour les JPanel des parties WEST, CENTER et EAST qui vont contenir mes labels, boutons et voyants de connexion.
Je suis donc arrivé au résultat que je désirai :
http://imageshack.com/a/img661/760/R8Lv4E.jpg
 
Seulement voilà, depuis, j'ai des gros bug dans mon affichage lorsque j'actualise mon voyant d'état de connexion ; c'est à dire lorsque je clique sur un bouton pour changer l'état ou quand je clique sur le bouton d'actualisation [Note : je n'ai rien modifié dans ma class Controleur.java] :
Clic sur bouton radio pour changer d'état
http://imageshack.com/a/img908/8721/9NVca8.jpg
On peut voir au niveau de l'état modifié qu'un bouton radio avec le label "Fermé" apparait sous mon voyant d'état
 
Clic sur le bouton d'actualisation (de tous les boutons et voyants)
http://imageshack.com/a/img743/5294/3UrvA7.jpg
Là c'est l'anarchie : des boutons radio et actualiser qui s'affiche un peu partout sous mes voyants
 
Remarque : Si je rezise la fenêtre ou que je la diminue dans la barre de tâche pour la réagrandir : ces bugs d'affichage disparaissent...
 
Voici mes 2 classes, avec en gras, les parties qui concernent l'actualisation des voyants d'état :
 
Vue.java

Code :
  1. public class Vue extends JFrame {
  2. private static final long serialVersionUID = 1L;
  3. Controleur firewall = new Controleur();
  4. JPanel panel = new JPanel();
  5. JPanel paneln = new JPanel();
  6. JPanel panels = new JPanel();
  7. JPanel panelw = new JPanel();
  8. JPanel panelc = new JPanel();
  9. JPanel panele = new JPanel();
  10. JLabel image = new JLabel(new ImageIcon(this.getClass().getResource("logoVPN.png" )));
  11. JButton refresh = new JButton("Actualiser" );
  12. public Vue(){
  13.  Color background = new Color(255,228,202);
  14.  int labelMax = 0; // JLabel le + long pour adaptabilité de la largeur de l'appli
  15.  int nbrAcces = 0; // Nombre d'accès pour adaptabilité de la hauteur de l'appli
  16.  panel.setLayout(new BorderLayout(6,12));
  17.  panel.add(paneln, BorderLayout.NORTH);
  18.  panel.add(panelw, BorderLayout.WEST);
  19.  panel.add(panelc, BorderLayout.CENTER);
  20.  panel.add(panele, BorderLayout.EAST);
  21.  panel.add(panels, BorderLayout.SOUTH);
  22.  paneln.add(image);
  23.  panelw.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
  24.  panelc.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
  25.  panele.setLayout(new GridLayout(firewall.getListAcces().size(),1,0,3));
  26.  for(int j=0 ; j<firewall.getListAcces().size() ; j++){
  27.   Controleur.Acces acces = firewall.getListAcces().get(j);
  28.   boolean etatConnexion = firewall.getEtatAcces(acces);
  29.   panelw.add(new JLabel(acces.getLabel(),javax.swing.SwingConstants.CENTER));
  30.   for(int i=0 ; i<acces.getBoutons().length ; i++){
  31.    acces.getBoutons().addActionListener(new GestionActionListener(firewall,acces,acces.getBoutons()[i].getText()));
  32.    panelc.add(acces.getBoutons()[i]);
  33.   }
  34.   panele.add(acces.getEtat());
  35.   firewall.actualiserEtat(acces,etatConnexion);
  36.   firewall.actualiserBoutons(acces,etatConnexion);
  37.   if(acces.getLabel().length()>labelMax){
  38.    labelMax = acces.getLabel().length();
  39.   }
  40.   nbrAcces++;
  41.  }
  42.  refresh.addActionListener(new GestionActionRefreshListener(firewall));
  43.  panels.add(refresh);
  44.  /*if(firewall.getModeDefault()){
  45.  }*/
  46.  panelw.setPreferredSize(new Dimension(labelMax*7,300));
  47.  panel.setBackground(background);
  48.  paneln.setBackground(background);
  49.  panels.setBackground(background);
  50.  panele.setBackground(background);
  51.  panelw.setBackground(background);
  52.  panelc.setBackground(background);
  53.  this.setTitle("Gestion Accès VPN" );
  54.  this.setSize(190+(labelMax*7),125+(nbrAcces*31));
  55.  //this.setResizable(false);
  56.  this.addWindowListener(new GestionWindowListener(firewall));
  57.  this.setLocationRelativeTo(null);
  58.  this.setContentPane(panel);
  59.  this.setVisible(true);
  60. }
  61. public static void main(String[] args){
  62.  new Vue();
  63. }
  64. }


 
[i]Controleur.java

Code :
  1. public class Controleur {
  2. private Connexion telnet;
  3. private Connexion telnet2;
  4. private String cheminLog;
  5. private String cheminConf;
  6. private List<Acces> listAcces;
  7. private boolean mode_default;
  8. public Controleur() {
  9.  telnet = new Connexion( "IP_ACCES", "LOGIN", "MDP",'PROMPT');
  10.  telnet2 = new Connexion( "IP_FIREWALL", "LOGIN", "MDP", 'PROMPT1', 'PROMPT2');
  11.  cheminLog = "CHEMIN_FICHIER_LOG";
  12.  cheminConf = "CHEMIN_FICHIER_CONF";
  13.  mode_default = false;
  14.  try {
  15.   listAcces = getConfAcces();
  16.  } catch (IOException e) {
  17.   listAcces = getDefaultAcces();
  18.   mode_default = true;
  19.   e.printStackTrace();
  20.  }
  21. }
  22. public boolean getModeDefault(){
  23.  return this.mode_default;
  24. }
  25. public String getCheminLog(){
  26.  return this.cheminLog;
  27. }
  28. public List<Acces> getListAcces(){
  29.  return this.listAcces;
  30. }
  31. public class Acces{
  32.  private String label ="";
  33.  private String connexion ="";
  34.  private EtatVPN etat;
  35.  private String[] ports;
  36.  private JRadioButton[] boutons;
  37.  private ButtonGroup groupeBoutons;
  38.  public Acces(String l, String type_connexion, String... num_ports){
  39.   this.label = l;
  40.   this.etat = new EtatVPN();
  41.   this.connexion = type_connexion;
  42.   this.ports = num_ports;
  43.   this.boutons = new JRadioButton[2];
  44.   this.boutons[0]= new JRadioButton("Ouvert" );
  45.   this.boutons[1]= new JRadioButton("Fermé" );
  46.   this.boutons[0].setBackground(new Color(255,228,202));
  47.   this.boutons[1].setBackground(new Color(255,228,202));
  48.   this.groupeBoutons =new ButtonGroup();
  49.   this.groupeBoutons.add(this.boutons[0]);
  50.   this.groupeBoutons.add(this.boutons[1]);
  51.  }
  52.  public EtatVPN getEtat(){
  53.   return this.etat;
  54.  }
  55.  public String getLabel(){
  56.   return label;
  57.  }
  58.  public String getConnexion(){
  59.   return connexion;
  60.  }
  61.  public String[] getPorts(){
  62.   return ports;
  63.  }
  64.  public JRadioButton[] getBoutons(){
  65.   return boutons;
  66.  }
  67. }
  68. private List<Acces> getDefaultAcces(){
  69.  List<Acces> listeAcc = new ArrayList<Acces>();
  70.  listeAcc.add(new Acces("Acces1" , "ACCES" , "14","17" ));
  71.  listeAcc.add(new Acces("Acces2" , "ACCES" , "16","19" ));
  72.  listeAcc.add(new Acces("Acces3" , "ACCES" , "23" ));
  73.  listeAcc.add(new Acces("Acces4" , "ACCES" , "24" ));
  74.  listeAcc.add(new Acces("FirewallA" , "FIREWALL" , "26" ));
  75.  listeAcc.add(new Acces("FirewallB" , "FIREWALL" , "33" ));
  76.  listeAcc.add(new Acces("FirewallC" , "FIREWALL" , "25" ));
  77.  return listeAcc;
  78. }
  79. private List<Acces> getConfAcces() throws IOException{
  80.  List<Acces> listeAcc = new ArrayList<Acces>();
  81.  ArrayList<String> tmp = null;
  82.  String line = "";
  83.  BufferedReader bis = new BufferedReader(new FileReader(this.cheminConf));
  84.  while((line = bis.readLine()) != null){
  85.   tmp = new ArrayList<String>();
  86.   for(String s : line.split("," )){
  87.    tmp.add(s);
  88.   }
  89.   String[] lineTab = new String[tmp.size()];
  90.   lineTab = tmp.toArray(lineTab);
  91.   String[] portsTab = new String[lineTab.length-2];
  92.   for(int i=2 ; i<lineTab.length ; i++){
  93.    portsTab[i-2]=lineTab[i];
  94.   }
  95.   Acces acc = new Acces(lineTab[0],lineTab[1],portsTab);
  96.   listeAcc.add(acc);
  97.  }
  98.  bis.close();
  99.  return listeAcc;
  100. }
  101. private void envoiTelnetCommande(Acces acc, String statut){
  102.  switch(acc.getConnexion()){
  103.  case "ACCES":
  104.   for(int i=0 ; i<acc.getPorts().length ; i++){
  105.    telnet.sendCommand( "config firewall policy" );
  106.    telnet.sendCommand( "edit "+acc.getPorts()[i]);
  107.    telnet.sendCommand( "set status "+statut);
  108.    telnet.sendCommand( "end" );
  109.   }
  110.   break;
  111.  case "FIREWALL":
  112.   for(int i=0 ; i<acc.getPorts().length ; i++){
  113.    telnet2.sendCommand2( "configure terminal" );
  114.    telnet2.sendCommand2( "interface "+acc.getPorts()[i]);
  115.    telnet2.sendCommand2( statut);
  116.    telnet2.sendCommand2( "exit" );
  117.    telnet2.sendCommand2( "exit" );
  118.   }
  119.   break;
  120.  }
  121. }
  122. public boolean getEtatAcces(Acces acc){
  123.  switch(acc.getConnexion()){
  124.  case "ACCES":
  125.   for(int i=0 ; i<acc.getPorts().length ; i++){
  126.    String s = telnet.sendCommand( "show firewall policy "+ acc.getPorts()[i]);
  127.    if(s.indexOf("disable" )!= -1){
  128.     return false;
  129.    }
  130.   }
  131.   return true;
  132.  case "FIREWALL":
  133.   for(int i=0 ; i<acc.getPorts().length ; i++){
  134.    String s = telnet.sendCommand( "show interface "+ acc.getPorts()[i] + " status" );
  135.    if(s.indexOf("disable" )!= -1){
  136.     return false;
  137.    }
  138.   }
  139.   return true;
  140.  }
  141.  return false;
  142. }
  143. public void disconnect(){
  144.  telnet.disconnect();
  145.  telnet2.disconnect();
  146. }
  147. public void ouvrirAcces(Acces acc){
  148.  String statut = "";
  149.  String connexion = acc.getConnexion();;
  150.  switch(connexion){
  151.  case "ACCES":
  152.   statut = "enable";
  153.   envoiTelnetCommande(acc,statut);break;
  154.  case "FIREWALL":
  155.   statut = "no shutdown";
  156.   envoiTelnetCommande(acc,statut);break;
  157.  }
  158. }
  159. public void fermerAcces(Acces acc){
  160.  String statut = "";
  161.  String connexion = acc.getConnexion();
  162.  switch(connexion){
  163.  case "ACCES":
  164.   statut = "disable";
  165.   envoiTelnetCommande(acc,statut);break;
  166.  case "FIREWALL":
  167.   statut = "shutdown";
  168.   envoiTelnetCommande(acc,statut);break;
  169.  }
  170. }
  171.         public void actualiserEtat(Acces acc, boolean etatConnexion){
  172.  acc.etat.setEtat(etatConnexion);acc.etat.repaint();
  173. }
  174. public void actualiserBoutons(Acces acc, boolean etatConnexion){
  175.  acc.boutons[0].setSelected(etatConnexion == acc.boutons[0].getText().equalsIgnoreCase("Ouvert" ));
  176.  acc.boutons[1].setSelected(etatConnexion == acc.boutons[1].getText().equalsIgnoreCase("Ouvert" ));
  177. }
  178. }
  179. class GestionActionListener implements ActionListener{
  180. private Controleur firewall;
  181. private Controleur.Acces acces;
  182. private String textBouton;
  183. public GestionActionListener(Controleur gaf, Controleur.Acces gafa, String s){
  184.  firewall = gaf;
  185.  acces = gafa;
  186.  textBouton = s;
  187. }
  188. public void actionPerformed(ActionEvent e) {
  189.  switch(textBouton){
  190.  case "Ouvert" : firewall.ouvrirAcces(acces);
  191.      firewall.actualiserEtat(acces,true);
  192.      try {
  193.       ecrireLog();
  194.      } catch (IOException e1) {
  195.       e1.printStackTrace();
  196.      }
  197.      break;
  198.  case "Fermé" :  firewall.fermerAcces(acces);
  199.      firewall.actualiserEtat(acces,false);
  200.      try {
  201.       ecrireLog(
  202.         );
  203.      } catch (IOException e1) {
  204.       e1.printStackTrace();
  205.      }
  206.      break;
  207.  }
  208. }
  209. public void ecrireLog() throws IOException{
  210.   Calendar c = Calendar.getInstance();
  211.   DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss" );
  212.   String ip;
  213.   try {
  214.    ip = InetAddress.getLocalHost().getHostAddress();
  215.   } catch (UnknownHostException e) {
  216.    ip = "IP INCONNUE";
  217.   }
  218.   String acc = acces.getLabel();
  219.   PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(firewall.getCheminLog() , true)));
  220.   pw.println(ip+" - "+df.format(c.getTime())+" - "+acc+" "+textBouton);
  221.   pw.close();
  222. }
  223. }
  224. class GestionActionRefreshListener implements ActionListener{
  225. Controleur firewall;
  226. public GestionActionRefreshListener(Controleur gaf){
  227.  firewall = gaf;
  228. }
  229. public void actionPerformed(ActionEvent e) {
  230.  for(int i=0 ; i<firewall.getListAcces().size() ; i++){
  231.   boolean etatConnexion = firewall.getEtatAcces(firewall.getListAcces().get(i));
  232.   firewall.actualiserEtat(firewall.getListAcces().get(i),etatConnexion);
  233.   firewall.actualiserBoutons(firewall.getListAcces().get(i), etatConnexion);
  234.  }
  235. }
  236. }
  237. class GestionWindowListener implements WindowListener {
  238. Controleur firewall;
  239. public GestionWindowListener(Controleur gaf){
  240.  firewall = gaf;
  241. }
  242. public void windowActivated(WindowEvent arg0) {}
  243. public void windowClosed(WindowEvent arg0) {}
  244. public void windowClosing(WindowEvent arg0) {
  245.  firewall.disconnect();
  246. }
  247. public void windowDeactivated(WindowEvent arg0){}
  248. public void windowDeiconified(WindowEvent arg0){}
  249. public void windowIconified(WindowEvent arg0){}
  250. public void windowOpened(WindowEvent arg0){}
  251. }
  252. class EtatVPN extends JPanel{
  253. private static final long serialVersionUID = 1L;
  254. private boolean etatAcces;
  255. public void paintComponent(Graphics g){
  256.  g.setColor(Color.white);
  257.  g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);
  258.  if(etatAcces){
  259.   g.setColor(Color.GREEN);
  260.  }else{
  261.   g.setColor(Color.RED);
  262.  }
  263.  g.fillRect(5, 5, this.getWidth()/2, this.getHeight()/2);
  264. }
  265. public Dimension getPreferredSize(){
  266.  return new Dimension(30,30);
  267. }
  268. public void setEtat(boolean b){
  269.  etatAcces = b;
  270. }
  271. }


 
En espérant que vous puissiez trouver une solution à mon problème... (sans ça, adieu mes Layout !)
Merci d'avance !!


Message édité par sebastien4444 le 29-10-2014 à 11:36:24
mood
Publicité
Posté le 29-10-2014 à 11:30:53  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  repaint() & BorderLayout : bug d'affichage...

 

Sujets relatifs
Problème d'affichageProblème affichage balises sous IE
Script affichage taille fichieraffichage de fichier XML en JFrame (zone de texte)
affichage de résultat dans un interface JFramevba userform affichage de données instantané
servlet pour l'affichage d'une imagetemps d'exécution d'un programme et messages d'affichage
Affichage de la fenêtre word - VBA Excelaffichage du texte dans un tableau latex
Plus de sujets relatifs à : repaint() & BorderLayout : bug d'affichage...


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR