Bonjour à toutes et à tous,
Je me retourne vers vous après une réponse claire de l'un des participant, parce que un autre problème se présente à moi, je n'arrive pas à renvoyé un JLabel dans une JFrame préalablement construite.
Malgré plusieurs méthodes utilisées, une d'entre elle m'a été conseillée parmi les plusieurs testées.
Pourtant je n'arrive pas à renvoyer l'image, malgré toutes absence d'erreur.
Je ne suis que débutant et je ne vois pas pourquoi elle ne marche pas est ce que quelqu’un peut m'éclairer ?
Merci d'avance !

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
174
175
176
177
178
179
180
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class Frame extends JFrame {
 
	private static final long    serialVersionUID    = 4983727800375272097L;
	public static JLabel label;
	public static JFrame frame;
 
	public Frame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(1367, 703);
		setLocationRelativeTo(null);
		BufferedImage img = null;
		try {
			img = ImageIO.read(new File("Background0.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		setContentPane(new MyCanvas(img));
	}
 
 
 
	public static void main(String[] args) {
		new Frame().setVisible(true);
	}
 
	private static class MyCanvas extends JComponent implements MouseListener {
		//ici on défini toutes les zones clicables de l'écran 1
 
		private static final long    serialVersionUID    = 1L;
		// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=0, y=0 et s'étend sur width=10 et height=10)
		private static final Rectangle    ZONE_IMAGE_1 = new Rectangle(21, 10, 281, 50);
		// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=100, y=100 et s'étend sur width=30 et height=20)
		private static final Rectangle    ZONE_IMAGE_2 = new Rectangle(320, 10, 265, 50);
 
		private static final Rectangle    ZONE_IMAGE_3 = new Rectangle(602, 10, 290, 50);
		// position de la première zone de limage que l'on veut (ici elle se trouve au pixel x=100, y=100 et s'étend sur width=30 et height=20)
		private static final Rectangle    ZONE_IMAGE_4 = new Rectangle(135, 200, 170, 140);
		private static final Rectangle    ZONE_IMAGE_5 = new Rectangle(135, 352, 170, 140);
		private static final Rectangle    ZONE_IMAGE_6 = new Rectangle(135, 506, 170, 140);
 
 
		private static final Rectangle    ZONE_IMAGE_10 = new Rectangle(368, 200, 61, 45);
		private static final Rectangle    ZONE_IMAGE_11 = new Rectangle(368, 247, 61, 45);
		private static final Rectangle    ZONE_IMAGE_12 = new Rectangle(368, 294, 61, 44);
		private static final Rectangle    ZONE_IMAGE_13 = new Rectangle(368, 340, 60, 44);
		private static final Rectangle    ZONE_IMAGE_14 = new Rectangle(368, 386, 60, 44);
		private static final Rectangle    ZONE_IMAGE_15 = new Rectangle(368, 432, 60, 44);
		private static final Rectangle    ZONE_IMAGE_16 = new Rectangle(368, 478, 60, 46);
		private static final Rectangle    ZONE_IMAGE_17 = new Rectangle(368, 526, 60, 45);
		private static final Rectangle    ZONE_IMAGE_18 = new Rectangle(368, 573, 60, 54);
 
		private BufferedImage buff= null;
 
 
 
		public MyCanvas(BufferedImage img) {
			this.addMouseListener(this);
			this.buff = img;
		}
 
		public void paintComponent(Graphics g) {//zone images
			// dessine l'image
			g.drawImage(buff, 0, 0, buff.getWidth(), buff.getHeight(), this);
			// juste histoire de voir ou se trouvent les zones clicables 
			g.setColor(Color.GREEN);
			g.drawRect(ZONE_IMAGE_1.x, ZONE_IMAGE_1.y, ZONE_IMAGE_1.width, ZONE_IMAGE_1.height);
			g.drawRect(ZONE_IMAGE_2.x, ZONE_IMAGE_2.y, ZONE_IMAGE_2.width, ZONE_IMAGE_2.height);
			g.drawRect(ZONE_IMAGE_3.x, ZONE_IMAGE_3.y, ZONE_IMAGE_3.width, ZONE_IMAGE_3.height);
			g.drawRect(ZONE_IMAGE_4.x, ZONE_IMAGE_4.y, ZONE_IMAGE_4.width, ZONE_IMAGE_4.height);
			g.drawRect(ZONE_IMAGE_5.x, ZONE_IMAGE_5.y, ZONE_IMAGE_5.width, ZONE_IMAGE_5.height);
			g.drawRect(ZONE_IMAGE_6.x, ZONE_IMAGE_6.y, ZONE_IMAGE_6.width, ZONE_IMAGE_6.height);
			g.drawRect(ZONE_IMAGE_10.x, ZONE_IMAGE_10.y, ZONE_IMAGE_10.width, ZONE_IMAGE_10.height);
			g.drawRect(ZONE_IMAGE_11.x, ZONE_IMAGE_11.y, ZONE_IMAGE_11.width, ZONE_IMAGE_11.height);
			g.drawRect(ZONE_IMAGE_12.x, ZONE_IMAGE_12.y, ZONE_IMAGE_12.width, ZONE_IMAGE_12.height);
			g.drawRect(ZONE_IMAGE_13.x, ZONE_IMAGE_13.y, ZONE_IMAGE_13.width, ZONE_IMAGE_13.height);
			g.drawRect(ZONE_IMAGE_14.x, ZONE_IMAGE_14.y, ZONE_IMAGE_14.width, ZONE_IMAGE_14.height);
			g.drawRect(ZONE_IMAGE_15.x, ZONE_IMAGE_15.y, ZONE_IMAGE_15.width, ZONE_IMAGE_15.height);
			g.drawRect(ZONE_IMAGE_16.x, ZONE_IMAGE_16.y, ZONE_IMAGE_16.width, ZONE_IMAGE_16.height);
			g.drawRect(ZONE_IMAGE_17.x, ZONE_IMAGE_17.y, ZONE_IMAGE_17.width, ZONE_IMAGE_17.height);
			g.drawRect(ZONE_IMAGE_18.x, ZONE_IMAGE_18.y, ZONE_IMAGE_18.width, ZONE_IMAGE_18.height);
 
 
		}
 
		private Boolean testLocation(Point mouse, Rectangle area, String text) {//ici on a juste le test pour savoir si les zones de clics répondes 
			// test si la souris est dans les data de l'image
			if(area.contains(mouse)) {
				System.out.println(text + " - image");
				return true;
			}
			else {
				System.out.println(text + " - !image");
				return false;
			}
		}
 
		public void mouseClicked(MouseEvent e) {
		}
 
 
		public void mousePressed(MouseEvent e) 
		{            //récupération de la position de la souris
			Point p = e.getPoint();
			if (testLocation(p, ZONE_IMAGE_1, "mouseClicked - data 1"))
			{new Frame2().setVisible(true);// j'appelle ma nouvelle fenêtre 
			}
			testLocation(p, ZONE_IMAGE_2, "mouseClicked - data 2");
			if (testLocation( p, ZONE_IMAGE_2, "mouseClicked - data 2")){
				new Frame3().setVisible(true);// j'appelle ma nouvelle fenêtre 
			}
			testLocation(p, ZONE_IMAGE_3, "mouseClicked - data 3");
			if(testLocation(p, ZONE_IMAGE_3, "mouseClicked - data 3")){
				new Frame4().setVisible(true);// j'appelle ma nouvelle fenêtre 
			}
 
 
			testLocation(p, ZONE_IMAGE_4, "mouseClicked - data 4");//clapier1
			testLocation(p, ZONE_IMAGE_5, "mouseClicked - data 5");//clapier2
			testLocation(p, ZONE_IMAGE_6, "mouseClicked - data 6");//clapier3
 
 
			testLocation(p, ZONE_IMAGE_10, "mouseClicked - data 10");//case 1
			if (testLocation( p, ZONE_IMAGE_10, "mouseClicked - data 10")){
				try {																				
					///////////////////////////////
					///////////////////////////////////////
					JLabel imageLabel = new JLabel();
					ImageIcon ii = new ImageIcon(this.getClass().getResource("Essaye-lapin2.gif"));
					imageLabel.setIcon(ii);
					imageLabel.setBounds(0,0,ii.getIconWidth(),ii.getIconHeight());
					frame.getContentPane().add(imageLabel, BorderLayout.CENTER); 
					label.repaint(); 
					frame.repaint();
					frame.getContentPane().repaint();
					////////// c'est ici !//////////////////
					///////////////////////////////////////
					///"tudieu fichtre bigre ça ne marche pas !"
				} 
				catch (Exception exception) {
					exception.printStackTrace();
				}
 
			}
			testLocation(p, ZONE_IMAGE_11, "mouseClicked - data 11");//case 2
			testLocation(p, ZONE_IMAGE_12, "mouseClicked - data 12");//case 3
			testLocation(p, ZONE_IMAGE_13, "mouseClicked - data 13");//case 4
			testLocation(p, ZONE_IMAGE_14, "mouseClicked - data 14");//case 5
			testLocation(p, ZONE_IMAGE_15, "mouseClicked - data 15");//case 6
			testLocation(p, ZONE_IMAGE_16, "mouseClicked - data 16");//case 7
			testLocation(p, ZONE_IMAGE_17, "mouseClicked - data 17");//case 8
			testLocation(p, ZONE_IMAGE_18, "mouseClicked - data 18");//case 9
 
 
 
		}
		public void mouseReleased(MouseEvent e) {}//peut servir pour plus tard 
		public void mouseEntered(MouseEvent e) { }//peut servir pour plus tard 
		public void mouseExited(MouseEvent e) { }//peut servir pour plus tard 
 
	}
 
 
}