Yesterday I asked a question because I was making a small basic app that would open a jFrame with a text field and two buttons. If you wrote something and then clicked on the button, another jFrame would appear with a jPanel inside, that would turn green if you wrote a zero, or black with any other result. I didn't know how to make it work. I've finally gotten it to work properly, but it only does it the first time. The thing is, this is what happens when you press the button:
Taller2.opcion = jTextField2.getText();
Panel p;
p = new Panel();
if (Taller2.panelabierto == false) {
p.setVisible(true);
Taller2.panelabierto = true;
}
else {
// No hacer nada
}
It turns the Taller2.opcion String variable into what's written in the text field and opens the second jFrame. If it's already open, then it will just take the text. Then, on the other jFrame, I wrote this:
if ("0".equals(Taller2.opcion)) {
jPanel1.setBackground(java.awt.Color.GREEN);
}
else {
jPanel1.setBackground(java.awt.Color.BLACK);
}
Which will basically turn the jPanel green if you wrote a zero. The problem is that it will only work the first time you press it. If you write another number and press the button again, it will stay the same color. I tried using a "while (true)" loop around that if-else statement, but whenever I ran it and pressed the button, the program froze. How could I fix this? I'm fairly new at Java, so I'd appreciate rather simple answers. I made both jFrames with the Netbeans designer, so I don't really know the code behind them, only what I added to change the colors and the buttons. Thanks!
EDIT: This is the only part of code I haven't added, which is the main class:
public class Taller2 {
/**
* @param args the command line arguments
*/
public static String opcion;
public static boolean panelabierto;
public static void main(String[] args) {
Pregunta a = new Pregunta();
a.setVisible(true);
opcion = null;
panelabierto = false;
the rest has been generated by the Netbeans designer.