Précédent   Forum du club des développeurs et IT Pro > Java > Interfaces Graphiques en Java
Interfaces Graphiques en Java Forum d'entraide pour les interfaces graphiques en Java (Swing, AWT, JFace, SWT, Graphisme 2D et 3D, clients riches, ...). Avant de poster -> Les cours sur les Interfaces Graphiques - FAQ GUI Java
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 07/09/2012, 15h46   #1
houssine91
Invité de passage
 
Homme
Inscription : septembre 2012
Messages : 7
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : septembre 2012
Messages : 7
Points : 1
Points : 1
Par défaut Double affichage du menu dans JFrame

bonjours , je suis débutant dans la programation graphique avec JFrame , j'ai rencontré un problème énervant , c'est que lors de l'appel à la méthode paintComponent dans une classe héritant JPanel , j'aurais une dédoublement de l'affichage de la barre de menu dans ma fenêtre.

voila le code
Code :
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
174
175
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
 
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
 
public class Window extends JFrame{
   private Panneau pan = new Panneau();
 
   private JMenuBar menuBar = new JMenuBar();
	private JMenu mfichier = new JMenu("Fichier");
	private JMenu medition = new JMenu("Edition");
	private JMenu mforme = new JMenu("Forme du pointeur");
	private JMenu mcouleur = new JMenu("Couleur du pointeur");
	private JMenuItem meffacer = new JMenuItem("Effacer");
	private JMenuItem mquitter = new JMenuItem("Quitter");
	private JMenuItem  mrond = new JMenuItem("Rond");
	private JMenuItem  mcarre = new JMenuItem("Carré");
	private JMenuItem  mrouge = new JMenuItem("Rouge");
	private JMenuItem  mvert = new JMenuItem("Vert");
	private JMenuItem  mbleu = new JMenuItem("Bleu");
 
	private JToolBar toolBar = new JToolBar();
	private JButton brond = new JButton(new ImageIcon("brond.jpg"));
	private JButton bcarre = new JButton(new ImageIcon("bcarre.jpg"));
	private JButton bbleu = new JButton();
	private JButton brouge = new JButton();
	private JButton bvert = new JButton();
 
public Window(){        
      this.setTitle("Animation");
      this.setSize(800, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
      this.getContentPane().add(pan,BorderLayout.CENTER);
 
      this.initMenu();
 
        this.setVisible(true);
 
   }
     public void initMenu(){
 
    		this.mfichier.setMnemonic('f');
    		this.meffacer.setMnemonic('e');
    		this.meffacer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK));
    		this.mquitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_MASK));
 
    		this.mfichier.add(meffacer);
    		this.mforme.add(mrond);
    		this.mforme.add(mcarre);
    		this.mcouleur.add(mrouge);
    		this.mcouleur.add(mvert);
    		this.mcouleur.add(mbleu);
 
    		this.mfichier.add(meffacer);
    		this.mfichier.addSeparator();
    		this.mfichier.add(mquitter);
    		this.mforme.add(mcarre);
    		this.mforme.add(mcarre);
    		this.mcouleur.add(mrouge);
    		this.mcouleur.add(mvert);
    		this.mcouleur.add(mbleu);
    		this.medition.add(mforme);
    		this.medition.addSeparator();
    		this.medition.add(mcouleur);
 
    		this.menuBar.add(mfichier);
    		this.menuBar.add(medition);
    		this.setJMenuBar(menuBar);
 
 
//----------------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
 
import javax.swing.JPanel;
 
public class Panneau extends JPanel {
   private int posX = -50;
   private int posY = -50;
   private int x = -50;
   private int y = -50;
   private int  TAILLE =50;   
   private boolean dessin=false;
   private boolean effacer=true;
 
   public void paintComponent(Graphics g){
	   g.setColor(Color.white);
	   if(effacer)
		   g.fillRect(0, 0, this.getWidth(), this.getHeight());
     	   effacer=false;
 
	      g.setColor(Color.red);
	      g.fillOval(posX, posY, 50, 50);         
 
	      if(dessin){
	    	  if(this.forme=="Rond")
	    	g.fillOval(x, y, TAILLE, TAILLE);
	    	  else if(this.forme=="Carré")
	    	g.fillRect(x, y, TAILLE, TAILLE);  
	      }
 
	      dessin=false;
 
	      this.addMouseMotionListener(new MouseMotionListener(){
				public void mouseDragged(MouseEvent ev) {
				//	points.add(new Point(e.getX() - (pointerSize / 2), e.getY() - (pointerSize / 2), pointerSize, pointerColor, pointerType));
					 x=ev.getX()-TAILLE/2;
					 y=ev.getY()-TAILLE/2;
					 dessin=true;
 
 
 
					repaint();
				}
				public void mouseMoved(MouseEvent e) {}
 
	});
	  	this.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent ev){
				x=ev.getX()-TAILLE/2;
				y=ev.getY()-TAILLE/2;
				dessin=true;
				repaint();
 
			}
		});	   
 
   }
 
   public boolean isEffacer() {
	return effacer;
}
 
public void setEffacer(boolean effacer) {
	this.effacer = effacer;
}
 
public String getForme() {
	return forme;
}
 
public void setForme(String forme) {
	this.forme = forme;
}
 
public Color getCouleur() {
	return couleur;
}
 
public void setCouleur(Color couleur) {
	this.couleur = couleur;
}
 
private String forme="Rond";
   private Color couleur =Color.orange;
 
}
 
}
Images attachées
Type de fichier : jpg Capture2.JPG (16,9 Ko, 6 affichages)
Type de fichier : jpg Capture1.JPG (13,4 Ko, 1 affichages)
houssine91 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/09/2012, 16h30   #2
wax78
Modérateur
 
Avatar de wax78
 
Homme Renaud Warnotte
Développeur informatique
Inscription : août 2006
Messages : 2 138
Détails du profil
Informations personnelles :
Nom : Homme Renaud Warnotte
Âge : 32
Localisation : Belgique

Informations professionnelles :
Activité : Développeur informatique
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : août 2006
Messages : 2 138
Points : 4 088
Points : 4 088
Envoyer un message via MSN à wax78
Ça ne résoudra pas directement ton problème mais tu as oublié des accolade ou alors carrément un morceau de code dans ta classe Window (problème de copier coller ?).

Mais en rajoutant des accolades dans window , et bien je ne remarque pas de problème de dedoublement.
wax78 est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/09/2012, 16h37   #3
houssine91
Invité de passage
 
Homme
Inscription : septembre 2012
Messages : 7
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : septembre 2012
Messages : 7
Points : 1
Points : 1
voici le code complet de mon projet :

Code :
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
 
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
 
public class Window extends JFrame{
   private Panneau pan = new Panneau();
 
   private JMenuBar menuBar = new JMenuBar();
	private JMenu mfichier = new JMenu("Fichier");
	private JMenu medition = new JMenu("Edition");
	private JMenu mforme = new JMenu("Forme du pointeur");
	private JMenu mcouleur = new JMenu("Couleur du pointeur");
	private JMenuItem meffacer = new JMenuItem("Effacer");
	private JMenuItem mquitter = new JMenuItem("Quitter");
	private JMenuItem  mrond = new JMenuItem("Rond");
	private JMenuItem  mcarre = new JMenuItem("Carré");
	private JMenuItem  mrouge = new JMenuItem("Rouge");
	private JMenuItem  mvert = new JMenuItem("Vert");
	private JMenuItem  mbleu = new JMenuItem("Bleu");
 
	private JToolBar toolBar = new JToolBar();
	private JButton brond = new JButton(new ImageIcon("brond.jpg"));
	private JButton bcarre = new JButton(new ImageIcon("bcarre.jpg"));
	private JButton bbleu = new JButton();
	private JButton brouge = new JButton();
	private JButton bvert = new JButton();
 
	private formeListener fListener = new formeListener();
	private couleurListener cListener = new couleurListener();
 
     public Window(){        
      this.setTitle("Animation");
      this.setSize(800, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
      this.getContentPane().add(pan,BorderLayout.CENTER);
 
      this.initMenu();
 
        this.setVisible(true);
 
   }
     public void initMenu(){
 
    		this.mfichier.setMnemonic('f');
    		this.meffacer.setMnemonic('e');
    		this.meffacer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_MASK));
    		this.mquitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,KeyEvent.CTRL_MASK));
 
    		this.mfichier.add(meffacer);
    		this.mforme.add(mrond);
    		this.mforme.add(mcarre);
    		this.mcouleur.add(mrouge);
    		this.mcouleur.add(mvert);
    		this.mcouleur.add(mbleu);
 
    		this.mfichier.add(meffacer);
    		this.mfichier.addSeparator();
    		this.mfichier.add(mquitter);
    		this.mforme.add(mcarre);
    		this.mforme.add(mcarre);
    		this.mcouleur.add(mrouge);
    		this.mcouleur.add(mvert);
    		this.mcouleur.add(mbleu);
    		this.medition.add(mforme);
    		this.medition.addSeparator();
    		this.medition.add(mcouleur);
 
    		this.menuBar.add(mfichier);
    		this.menuBar.add(medition);
    		this.setJMenuBar(menuBar);
 
    		//les listener
    		this.meffacer.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent event){
    			//pan.setEffacer(true);
    			pan.repaint();
    			}
    		});
    		this.mquitter.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent event){
    				System.exit(0);
    			}
    		});
    		this.mrouge.addActionListener(cListener);
    		this.mbleu.addActionListener(cListener);
    		this.mvert.addActionListener(cListener);
    		this.mrond.addActionListener(fListener);
    		this.mcarre.addActionListener(fListener);
    		this.menuBar.setBorder(BorderFactory.createTitledBorder(""));
    		//menuBar.setPreferredSize(new Dimension(this.getWidth(),300));
    	}
 
 
 
     class formeListener implements ActionListener{
 
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
 
    			pan.setForme(((AbstractButton) e.getSource()).getText());
 
    			if((AbstractButton) e.getSource()==brond )
    				pan.setForme("Rond");
 
    			else if((AbstractButton) e.getSource()== bcarre)
    				pan.setForme("Carré");
 
    		}
 
    	}
    	class couleurListener implements ActionListener {
 
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
 
    		Color c = Color.white;
    		switch(((AbstractButton) e.getSource()).getText() ){
    		case "Rouge":
    			c=Color.red;
    			pan.setCouleur(c);
    			break;
    		case "Vert":
    			 c = Color.green;	
    			pan.setCouleur(c);
    			break;
    		case "Bleu":
    			 c = Color.blue;	
    			pan.setCouleur(c);
    			break;
    		default: break;
    		}
    		//les buttons toolBar
    	if(e.getSource()==brouge){
    	c=Color.red;
    			pan.setCouleur(c);	
    	}
    	else if(e.getSource()==bvert){
    	c = Color.green;	
    			pan.setCouleur(c);	
    	}
    	else if(e.getSource()==bbleu){
    	c = Color.blue;	
    			pan.setCouleur(c);	
    	}
 
//    		switch( e.getSource() ) !!!!
    		}
    	}
 
}


Code :
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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
 
import javax.swing.JPanel;
 
public class Panneau extends JPanel {
   private int posX = -50;
   private int posY = -50;
   private int x = -50;
   private int y = -50;
   private int  TAILLE =50;   
   private boolean dessin=false;
   private boolean effacer=true;
 
   public void paintComponent(Graphics g){
	   g.setColor(Color.white);
 
		   if(effacer)
		   g.fillRect(0, 0, this.getWidth(), this.getHeight());
	   effacer=false;
 
	      g.setColor(Color.red);
	      g.fillOval(posX, posY, 50, 50);         
 
	      if(dessin){
	    	  if(this.forme=="Rond")
	    	g.fillOval(x, y, TAILLE, TAILLE);
	    	  else if(this.forme=="Carré")
	    	g.fillRect(x, y, TAILLE, TAILLE);  
	      }
 
	      dessin=false;
 
	      this.addMouseMotionListener(new MouseMotionListener(){
				public void mouseDragged(MouseEvent ev) {
				//	points.add(new Point(e.getX() - (pointerSize / 2), e.getY() - (pointerSize / 2), pointerSize, pointerColor, pointerType));
					 x=ev.getX()-TAILLE/2;
					 y=ev.getY()-TAILLE/2;
					 dessin=true;
 
 
 
					repaint();
				}
				public void mouseMoved(MouseEvent e) {}
 
	});
	  	this.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent ev){
				x=ev.getX()-TAILLE/2;
				y=ev.getY()-TAILLE/2;
				dessin=true;
				repaint();
 
			}
		});	   
 
   }
 
   public boolean isEffacer() {
	return effacer;
}
 
public void setEffacer(boolean effacer) {
	this.effacer = effacer;
}
 
public String getForme() {
	return forme;
}
 
public void setForme(String forme) {
	this.forme = forme;
}
 
public Color getCouleur() {
	return couleur;
}
 
public void setCouleur(Color couleur) {
	this.couleur = couleur;
}
 
private String forme="Rond";
   private Color couleur =Color.orange;
 
 
 
   public int getPosX() {
      return posX;
   }
 
   public void setPosX(int posX) {
      this.posX = posX;
   }
 
   public int getPosY() {
      return posY;
   }
 
   public void setPosY(int posY) {
      this.posY = posY;
   }        
}
houssine91 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/09/2012, 16h52   #4
wax78
Modérateur
 
Avatar de wax78
 
Homme Renaud Warnotte
Développeur informatique
Inscription : août 2006
Messages : 2 138
Détails du profil
Informations personnelles :
Nom : Homme Renaud Warnotte
Âge : 32
Localisation : Belgique

Informations professionnelles :
Activité : Développeur informatique
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : août 2006
Messages : 2 138
Points : 4 088
Points : 4 088
Envoyer un message via MSN à wax78
Personnelement quand j'éxecute ton code je ne vois aucun dédoublement.

Par contre ta phrase me laisse un doute :

Code :
c'est que lors de l'appel à la méthode paintComponent  dans une classe héritant JPanel
Tu appèles vraiment la méthode quelques part ? (ailleurs que dans le code fournis) Car en théorie tu ne devrais pas il me semble... (les repaint ca c'est correct).

Edit : Par contre j'ai remarqué que si je dessinais et que je déplace la fenêtre d'un ecran a l'autre (j'ai 2 ecran) alors je perds le dessin. (mais c'est un autre problème supplementaire)
wax78 est actuellement connecté   Envoyer un message privé Réponse avec citation 10
Vieux 07/09/2012, 17h01   #5
Ceddoc
Membre éprouvé
 
Avatar de Ceddoc
 
Homme Cédric
Développeur Java / Web / .NET
Inscription : janvier 2009
Messages : 357
Détails du profil
Informations personnelles :
Nom : Homme Cédric
Âge : 24
Localisation : France, Rhône (Rhône Alpes)

Informations professionnelles :
Activité : Développeur Java / Web / .NET
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : janvier 2009
Messages : 357
Points : 417
Points : 417
J'ai réussi à reproduire le bug avec le code donné, par contre je n'ai pas encore trouvé comment le résoudre.

http://stackoverflow.com/questions/8...ubar-to-appear
Ceddoc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/09/2012, 19h13   #6
houssine91
Invité de passage
 
Homme
Inscription : septembre 2012
Messages : 7
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Tunisie

Informations forums :
Inscription : septembre 2012
Messages : 7
Points : 1
Points : 1
Citation:
Envoyé par wax78 Voir le message
Personnelement quand j'éxecute ton code je ne vois aucun dédoublement.

Par contre ta phrase me laisse un doute :

Code :
c'est que lors de l'appel à la méthode paintComponent  dans une classe héritant JPanel
Tu appèles vraiment la méthode quelques part ? (ailleurs que dans le code fournis) Car en théorie tu ne devrais pas il me semble... (les repaint ca c'est correct).
oui c'est ça , je parle des repaint .
houssine91 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 13h15.


 
 
 
 
Partenaires

Hébergement Web