IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

test Jbutton java


Sujet :

Langage Java

  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut test Jbutton java
    bonsoir,
    j'ai un petit problème avec les bouton!!
    Comment tester si une bouton existe ou non sachant que j'ai un tableau de bouton ??
    et merci d'avance!!

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    R&D - Palefrenier programmeur
    Inscrit en
    Août 2006
    Messages
    4 096
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : R&D - Palefrenier programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 096
    Par défaut
    Et bien tu regardes dans ton tableau, et si tu trouves le bouton, c'est que tu l'as trouvé
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre éclairé
    Homme Profil pro
    Ingénieur de construction de réseaux
    Inscrit en
    Août 2012
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Ingénieur de construction de réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2012
    Messages : 406
    Par défaut
    Tout dépend de ta déclaration. Si tu déclare ton tableau en initialisant les boutons tous seront existant. De plus ta question n'est pas précise de plus regarde la méthode isValid

  4. #4
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut
    merci pour vos réponses je vais essayer d'être plus clair!!

    j'ai un projet en java,mon projet est un jeu qui consiste à supprimer tout les bouton adjacents de même couleur si je clique sur une d'eux!!

    mon problème est que si des boutons ont été supprimés leurs places restent vide!!il faut que les boutons au dessus descend dans les places vide!!cette jeu s'appelle same game ou bubble je pense!!!le problème est comment faire descendre ces bouton dans les places vides!!

    voila mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
    	ArrayList<JButton> list = new ArrayList<JButton>();
    	int[][] grid = new int[10][10];
    	public int n = 10;
    	public int m = 10;
     
    	public Fenetre() {
    		this.setTitle("bubble");
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		b = new JButton[n][m];
    		setLayout(new GridLayout(n, m));
    		for (int y = 0; y < m; y++) {
    			for (int x = 0; x < n; x++) {
    				Random rd = new Random();
    				int r = rd.nextInt(5);
    				switch (r) {
    				case 0:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.YELLOW);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					b[x][y].setActionCommand(getName());
    					break;
    				case 1:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.MAGENTA);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 2:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.BLUE);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 3:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.GREEN);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 4:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.RED);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 5:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.ORANGE);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				}
    			}
    		}
    		pack();
    		setVisible(true);
    		start();
    	}
     
    	public int getN() {
    		return (n);
    	}
     
    	public int getM() {
    		return (m);
    	}
     
    	public void start() {
    		Component temporaryLostComponent = null;
    	}
     
    	public static void main(String[] args) {
    		Fenetre window = new Fenetre();
    		window.setSize(650, 500);
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		list.clear();
    		JButton c = (JButton) e.getSource();
    		rec(c);
    		for (int i = 0; i < list.size(); i++) {
    			if (list.size() >= 3)
    				remove(list.get(i));
    		}
    		for (int i = 0; i < m ; i++) {
    			for (int j = 0; j < n-1 ; j++) {
    				System.out.println("avan if");
    				if(b[i+1][j].isVisible()==false){
    					System.out.println("après if");
    					b[i+1][j]=new JButton(i+" "+j);
    					b[i+1][j]=b[i][j];
    					remove(b[i][j]);
    					validate();
    					repaint();
    				}
    			}
    		}
    		repaint();
    	}
     
    	public void rec(JButton c) {
    		list.add(c);
    		System.out.println(c.getText());
    		String[] xy = c.getText().split(" ");
    		int x = Integer.parseInt(xy[0]);
    		int y = Integer.parseInt(xy[1]);
    		if (x < 9 && b[x][y].getBackground() == b[x + 1][y].getBackground()
    				&& !list.contains(b[x + 1][y])) {
    			rec(b[x + 1][y]);
    		}
    		if (x > 0 && b[x][y].getBackground() == b[x - 1][y].getBackground()
    				&& !list.contains(b[x - 1][y])) {
    			rec(b[x - 1][y]);
    		}
    		if (y < 9 && b[x][y].getBackground() == b[x][y + 1].getBackground()
    				&& !list.contains(b[x][y + 1])) {
    			rec(b[x][y + 1]);
    		}
    		if (y > 0 && b[x][y].getBackground() == b[x][y - 1].getBackground()
    				&& !list.contains(b[x][y - 1])) {
    			rec(b[x][y - 1]);
    		}
    	}
    }
    je pense que le problème est dans la méthode actionPerformed après remove(list.get(i))

    et merci d'avance!!!

  5. #5
    Membre éclairé
    Homme Profil pro
    Ingénieur de construction de réseaux
    Inscrit en
    Août 2012
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Ingénieur de construction de réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2012
    Messages : 406
    Par défaut
    J'ai regardé ton code (brièvement) mais déjà je trouve ton approche mauvaise ce que tu dois faire c'est ceci:
    - créer une classe pièce qui est ton bouton avec des propriétés
    - Ensuite tu craie une classe qui hérite de JComponent qui va contenir tes pièces dans une arraylist et tu redéfinit la methode paincomponent pourqu'elle dessine tes pièces
    - Quand c'est fait tu poste ton code et on avance ensemble

  6. #6
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 12
    Par défaut
    Citation Envoyé par junior222 Voir le message
    J'ai regardé ton code (brièvement) mais déjà je trouve ton approche mauvaise ce que tu dois faire c'est ceci:
    - créer une classe pièce qui est ton bouton avec des propriétés
    - Ensuite tu craie une classe qui hérite de JComponent qui va contenir tes pièces dans une arraylist et tu redéfinit la methode paincomponent pourqu'elle dessine tes pièces
    - Quand c'est fait tu poste ton code et on avance ensemble
    J'ai pas très bien compris votre méthode mais j'ai essayé une autre solution dans la méthode actionPerformed: j'ai déclaré une matrice d'entier et j'ai l'initialiser à 0 puis lorsqu’il y a une bouton supprimer il met a sa place un 1
    et après s'il trouve des case vide il les remplis par les bouton qui sont au dessus mais malheureusement elle ne marche pas!!!

    voila mon nouveau code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class Fenetre extends JFrame implements ActionListener {
     
    	JButton b[][];
    	int vide[][];
    	ArrayList<JButton> list = new ArrayList<JButton>();
    	int[][] grid = new int[10][10];
    	public int n = 10;
    	public int m = 10;
     
    	public Fenetre() {
     
    		this.setTitle("bubble");
    		this.setLocationRelativeTo(null);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		vide=new int[10][10];
    		for (int i = 0; i < m; i++) {
     
    			for (int j = 0; j < n; j++) {
    				vide[i][j]=0;
    //				System.out.println(i+" "+j);
    			}
    		}
    		b = new JButton[n][m];
    		setLayout(new GridLayout(n, m));
    		for (int y = 0; y < m; y++) {
    			for (int x = 0; x < n; x++) {
    				Random rd = new Random();
    				int r = rd.nextInt(5);
    				switch (r) {
    				case 0:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.YELLOW);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					b[x][y].setActionCommand(getName());
    					break;
    				case 1:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.MAGENTA);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 2:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.BLUE);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 3:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.GREEN);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 4:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.RED);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				case 5:
    					b[x][y] = new JButton(x + " " + y);
    					b[x][y].setOpaque(true);
    					b[x][y].setBackground(Color.ORANGE);
    					b[x][y].addActionListener(this);
    					add(b[x][y]);
    					b[x][y].setEnabled(true);
    					break;
    				}
    			}
    		}
    		pack();
    		setVisible(true);
    		start();
    	}
     
    	public int getN() {
    		return (n);
    	}
     
    	public int getM() {
    		return (m);
    	}
     
    	public void start() {
    		Component temporaryLostComponent = null;
    	}
     
    	public static void main(String[] args) {
    		Fenetre window = new Fenetre();
    		window.setSize(650, 500);
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		list.clear();
     
    		JButton c = (JButton) e.getSource();
    		rec(c);
    		for (int i = 0; i < list.size(); i++) {
    			if (list.size() >= 3)
    				remove(list.get(i));
    		}
    		for (int i = 0; i < m-1 ; i++) {
    			for (int j = 0; j < n-2 ; j++) {
    				System.out.println("avant if");
    				if(vide[i+1][j]==1){
    					System.out.println("après if");
    					b[i+1][j]=new JButton(i+" "+j);
    //					b[i+1][j]=b[i][j];
    					b[i+1][j].setOpaque(true);
    					b[i+1][j].setBackground(b[i][j].getBackground());
    					b[i+1][j].addActionListener(this);
    					add(b[i+1][j]);
    					b[i+1][j].setEnabled(true);
    					b[i+1][j].setActionCommand(getName());
    					remove(b[i][j]);
    //					validate();
    					repaint();
    				}
    			}
    		}
    		repaint();
    	}
     
    	public void rec(JButton c) {
    		list.add(c);
    		System.out.println(c.getText());
    		String[] xy = c.getText().split(" ");
    		int x = Integer.parseInt(xy[0]);
    		int y = Integer.parseInt(xy[1]);
    		if (x < 9 && b[x][y].getBackground() == b[x + 1][y].getBackground()
    				&& !list.contains(b[x + 1][y])) {
    			vide[x + 1][y]=1;
    			rec(b[x + 1][y]);
    		}
    		if (x > 0 && b[x][y].getBackground() == b[x - 1][y].getBackground()
    				&& !list.contains(b[x - 1][y])) {
    			vide[x + 1][y]=1;
    			rec(b[x - 1][y]);
    		}
    		if (y < 9 && b[x][y].getBackground() == b[x][y + 1].getBackground()
    				&& !list.contains(b[x][y + 1])) {
    			vide[x + 1][y]=1;
    			rec(b[x][y + 1]);
    		}
    		if (y > 0 && b[x][y].getBackground() == b[x][y - 1].getBackground()
    				&& !list.contains(b[x][y - 1])) {
    			vide[x + 1][y]=1;
    			rec(b[x][y - 1]);
    		}
    	}
    }
    J'espère que vous avez compris ma solution!!
    et merci d'avance

  7. #7
    Membre éclairé
    Homme Profil pro
    Ingénieur de construction de réseaux
    Inscrit en
    Août 2012
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Ingénieur de construction de réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2012
    Messages : 406
    Par défaut
    Si tu comprend mon principe pourquoi tu ne l'utilise pas.
    Dans la classe pièce on devrait avoir les indices a savoir ligne et colonne et des méthodes pour les modifier essaie comme sa je crois que c'est mieux. De plus j'ai pas le temps pour mieux étudier ton code

  8. #8
    Membre éclairé
    Homme Profil pro
    Ingénieur de construction de réseaux
    Inscrit en
    Août 2012
    Messages
    406
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Ingénieur de construction de réseaux
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2012
    Messages : 406
    Par défaut
    De plus j'ai constaté dans l'exécution de ton code qu'il génère des exceptions du type:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
    	at tp.Fenetre.rec(Fenetre.java:165)
    	at tp.Fenetre.actionPerformed(Fenetre.java:120)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    donc tu devrais regarder ta façon d'utiliser tes tableaux et une fois de plus je te conseille d'utiliser ma méthode c'est mieux approprié et mieux maintenable

Discussions similaires

  1. Test technique JAVA/J2EE
    Par leila92 dans le forum Emploi
    Réponses: 7
    Dernier message: 15/03/2007, 00h13
  2. Image dans JButton (java 1.1)
    Par djanos dans le forum AWT/Swing
    Réponses: 6
    Dernier message: 05/03/2007, 13h58
  3. [JUnit] demande d'info concernant les differents tests en java.
    Par LESOLEIL dans le forum Tests et Performance
    Réponses: 5
    Dernier message: 08/05/2006, 13h55
  4. [Emploi] Les tests techniques Java/J2EE c'est quoi ?
    Par dedeloux dans le forum Emploi
    Réponses: 6
    Dernier message: 27/04/2006, 16h35
  5. [Stratégie] Ant pour les tests en Java ?
    Par franckR dans le forum Tests et Performance
    Réponses: 5
    Dernier message: 08/03/2004, 09h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo