Bonjour, dans ma fonction "public void actionPerformed(ActoinEvent evt)", se trouvant dans ma classe Gui1,
je voudrais savoir si cette condition ci dessous, est-t-elle correcte?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 if (Lcouleur.getActionCommand()==("Rouge")) if (Lfigure.getActionCommand().equals("Rectangle"))
Voici mon code dans lequel, il est utilisé:
Avec la condition "if" utilisé ci dessus, j'arrive pas à afficher.
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 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.String; class LFigure extends JComboBox implements ActionListener { JPanel ardoise; LFigure(JPanel ardoise) { super(); String[] libelleFigure={"Rectangle","Circle"}; for (int i=0;i<libelleFigure.length;i++) {this.addItem(libelleFigure[i]);} ardoise.add(this); } public String getFigure() { return (this.getActionCommand()); } } class LCouleur extends JComboBox implements ActionListener { JPanel ardoise; LCouleur(JPanel ardoise) { super(); String[] libelleCouleurs={"Bleue", "Rouge", "Jaune", "Vert"}; for (int i=0;i<libelleCouleurs.length;i++) {this.addItem(libelleCouleurs[i]);} ardoise.add(this); } public String getCouleur() { return (this.getActionCommand()); } } class Gui1 extends JPanel implements ActionListener { JPanel ardoise = new JPanel(); JPanel lesboutons = new JPanel(); LFigure Lfigure = new LFigure(ardoise); LCouleur Lcouleur = new LCouleur(ardoise); JButton affiche=new JButton("affiche"); JPanel pano = new JPanel(); Gui1() { setLayout(new BorderLayout(50,10)); // ardoise.setSize(new Dimension(15,15)); affiche.addActionListener(this); ardoise.add(affiche); add("North",ardoise); add("Center",pano); } public void actionPerformed(ActionEvent evt) { Graphics g = pano.getGraphics(); if (Lcouleur.getActionCommand().equals("Rouge")) {g.setColor(Color.red);} if (Lcouleur.getCouleur().equals("Bleue")) g.setColor(Color.blue); if (Lcouleur.getCouleur().equals("Jaune")) g.setColor(Color.yellow); if (Lcouleur.getCouleur().equals("Vert")) g.setColor(Color.green); if (Lfigure.getActionCommand().equals("Rectangle")) {Rectangle r1 = new Rectangle("Rectangle"); r1.draw(g);} if (Lfigure.getFigure().equals("Circle")) {Circle r2 = new Circle("Circle"); r2.draw(g);} } public static void main(String[] argv) { JFrame monCadre = new JFrame(); monCadre.setContentPane(new Gui1()); monCadre.pack(); monCadre.setVisible(true); monCadre.show(); } }
Par contre,quand je retire la condition du "if", par exemple:
cela m'affiche bien un rectangle en rouge.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 public void actionPerformed(ActionEvent evt) { Graphics g = pano.getGraphics(); {g.setColor(Color.red); Rectangle r1 = new Rectangle("Rectangle"); r1.draw(g);}
Forcement, j'ai un probleme avec mes conditions "if".
Je vous remercie d'avance,
diditin![]()
Partager