
Parpadeo de JFrame al ser creado.
Publicado por Jordi (135 intervenciones) el 15/04/2017 15:48:53
Buneas tardes a todos.
Se trata de lo siguiente... Estoy acabando un minijuego muy sencillo y me he dado cuenta al hacer la parte final, donde debe aparecer un jframe de 450x150 con 150 líneas de colores aleatorios que al crearlo, pese a haber indicado setVisible(true); al final de todo, se ve durante un segundo el jframe en otra parte de la pantalla y luego aparece en su lugar. La verdad es que me tiene desconcertado este comportamiento del JFrame.
Aquí está el código completo. La pate donde se crea la ventana final es el método finestraVictoria() de la línea 195.
Gracias de antemano.
Se trata de lo siguiente... Estoy acabando un minijuego muy sencillo y me he dado cuenta al hacer la parte final, donde debe aparecer un jframe de 450x150 con 150 líneas de colores aleatorios que al crearlo, pese a haber indicado setVisible(true); al final de todo, se ve durante un segundo el jframe en otra parte de la pantalla y luego aparece en su lugar. La verdad es que me tiene desconcertado este comportamiento del JFrame.
Aquí está el código completo. La pate donde se crea la ventana final es el método finestraVictoria() de la línea 195.
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package Codig;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Laberint implements KeyListener{
int x = 0;
int y = 0;
int valors[][] = new int[10][10];
JPanel[][] panel = new JPanel[10][10];
JLabel[][] label = new JLabel[10][10];
GridBagConstraints gbc = new GridBagConstraints();
ImatgeBandera bandera = new ImatgeBandera();
JFrame finestra = new JFrame("Laberint");
JFrame finestraERROR;
JFrame finestraVictoria;
public Laberint(){
creacioFinestra();
creacioGraella();
finestra.validate();
}
public void creacioFinestra(){
finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
finestra.setSize(350, 350);
finestra.setLocationRelativeTo(finestra);
finestra.setLayout(new GridBagLayout());
finestra.addKeyListener(this);
finestra.setVisible(true);
}
public void creacioGraella(){
for(int x = 0; x < 10; x++){
for(int y = 0; y < 10; y++){
valors[x][y] = (int)(Math.random()* 3 + 1);
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
panel[x][y] = new JPanel();
finestra.getContentPane().add(panel[x][y], gbc);
label[x][y] = new JLabel((Integer.toString(valors[x][y])));
panel[x][y].setLayout(new GridBagLayout());
panel[x][y].add(label[x][y]);
}
}
valors[0][0] = 1;
panel[0][0].remove(label[0][0]);
label[0][0].setText("1");
panel[0][0].add(label[0][0]);
valors[9][9] = 0;
panel[9][9].remove(label[9][9]);
label[9][9].setText(null);
//panel[9][9].add(bandera, gbc);
colorar(x, y);
}
public void moviment(KeyEvent arg0){
if(finestraERROR != null){
tancarFinestra(finestraERROR);
}
if(finestraVictoria != null){
tancarFinestra(finestraVictoria);
finestraVictoria = null;//No vull parlar-ne...
}
if(arg0.getKeyCode() == 37){
if(x - valors[x][y] < 0){
finestraError("No pots sortir del mapa...");
} else if (valors[x - valors[x][y]][y] == 3){
finestraError("No pots trepitjar el nombre 3...");
} else {
decolorar(x, y);
x -= valors[x][y];
colorar(x, y);
}
}
if(arg0.getKeyCode() == 38){
if(y - valors[x][y] < 0){
finestraError("No pots sortir del mapa...");
} else if(valors[x][y - valors[x][y]] == 3) {
finestraError("No pots trepitjar el nombre 3...");
} else {
decolorar(x, y);
y -= valors[x][y];
colorar(x, y);
}
}
if(arg0.getKeyCode() == 39){
if(x + valors[x][y] > 9){
finestraError("No pots sortir del mapa...");
} else if(valors[x + valors[x][y]][y] == 3){
finestraError("No pots trepitjar el nombre 3...");
} else {
decolorar(x, y);
x += valors[x][y];
colorar(x, y);
}
}
if(arg0.getKeyCode() == 40){
if(y + valors[x][y] > 9){
finestraError("No pots sortir del mapa...");
} else if(valors[x][y + valors[x][y]] == 3){
finestraError("No pots trepitjar el nombre 3...");
} else {
decolorar(x, y);
y += valors[x][y];
colorar(x, y);
}
}
System.out.println("1.2");
if(x == 9 && y == 9 && finestraVictoria == null){
System.out.println("nova");
finestraVictoria();
}
}
public void colorar(int x, int y){
panel[x][y].setBackground(Color.BLACK);
label[x][y].setForeground(Color.WHITE);
}
public void decolorar(int x, int y){
panel[x][y].setBackground(null);
label[x][y].setForeground(Color.BLACK);
}
public void finestraVictoria(){
finestraVictoria = new JFrame("VICTORIA");
finestraVictoria.setLocationRelativeTo(null);
finestraVictoria.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
finestraVictoria.addKeyListener(this);
finestraVictoria.setSize(450, 150);
finestraVictoria.setResizable(false);
//finestraVictoria.add(new FonsFinestraV());
finestraVictoria.validate();
finestraVictoria.setVisible(true);
}
public void finestraError(String s){
finestraERROR = new JFrame("ERROR");
finestraERROR.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
finestraERROR.addKeyListener(this);
finestraERROR.add(new JLabel(s));
finestraERROR.setSize(240, 50);
finestraERROR.setLocationRelativeTo(finestra);
finestraERROR.setVisible(true);
}
public void reiniciar() {
if(finestraERROR != null){
tancarFinestra(finestraERROR);
}
if(finestraVictoria != null){
tancarFinestra(finestraVictoria);
}
x = 0;
y = 0;
System.out.println(finestra.getSize());
finestra.getContentPane().removeAll();
creacioGraella();
finestra.validate();
finestra.repaint();
}
public void tancarFinestra(JFrame j){
j.dispose();
j = null;
}
@Override
public void keyPressed(KeyEvent arg0) {}
@Override
public void keyReleased(KeyEvent arg0) {
System.out.println(arg0.getKeyCode());
if(arg0.getKeyCode() == 82){
reiniciar();
}
else moviment(arg0);
}
@Override
public void keyTyped(KeyEvent arg0) {}
public static void main (String[] args){
new Laberint();
}
}
Gracias de antemano.
Valora esta pregunta


0