Well as an example I made the following code, I tried to make it easier to understand, however it is very different from what was proposed in the consultation but complies with the proposed function as I understand.
In short, it is a JFrame that has an if (true) {} condition inside it, and that depending on the result, it will be printed on a "new" JFrame, defined within the same class but as a new object (nested class), with globle attributes shared with the class that contains it and with its own attributes as well.
Create a class called "Panel" in any package of a java project that you have, then copy the code, import everything that you have to import and give it "run as java application". Then in the JTextField place numbers under 5 and touch the button, then over 5 and I touched the button again, at the end look carefully how it is done, there are things that are not recommended and it is better to work in layers, however for the consultation I think is fine.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/* Creamos un JFrame sin extender de JFrame que implementará ActionListener */
public class Panel implements ActionListener {
public JFrame frame;
public JPanel panel;
public JLabel label;
public JTextField input;
public JButton button;
public boolean booleanResult;
/* Creamos el constructor con el setVisible() incluido */
public Panel() {
frame = new JFrame();
panel = new JPanel();
label = new JLabel("Escreba némero y compare con 5");
input = new JTextField(10);
button = new JButton("Abri JFrame nuevo");
panel.setLayout(new BorderLayout());
frame.add(panel);
frame.setBounds(100, 100, 240, 110);
frame.setVisible(true);
panel.add(label, BorderLayout.NORTH);
panel.add(input, BorderLayout.CENTER);
panel.add(button, BorderLayout.SOUTH);
this.button.addActionListener(this);
/*
* Terminamos de definir el Frame Principal.
*/
}
/*
* Creamos el método que sobreescribe en la interfaz que estamos
* implementando AcionListener
*/
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
/* Si se toca el botón definido que pase tal evento */
if (button.equals(obj)) {
/*
* Ponemos una condición de prueba solamente para probar el booleano
* y asignarle un valor dependiendo la condición
*/
/*
* Acabamos de parsear la respuesta que viene del input ya que
* getText() devuelve sólo String y necesitamos un entero para
* comparar
*/
if (Integer.parseInt(input.getText()) > 5) {
booleanResult = true;
} else {
booleanResult = false;
}
/*
* Abriría este nuevo panel con el resultado nuevo del booleano
* incluído
*/
PanelResult pr = new PanelResult();
}
}
/*
* Creamos una clase PanelResult dentro de la clase Panel la cual compartirá
* sus atributos globales y podrá definir propios tmb
*/
class PanelResult {
public JLabel label1, label2;
public PanelResult() {
frame = new JFrame();
panel = new JPanel();
label1 = new JLabel("Resultado Booleano: ");
label2 = new JLabel();
//Este es el boolean del JFrame anterior
if (booleanResult) {
label2.setForeground(Color.BLUE);
label2.setText("Esta es la variable booleana de JFrame anterior: " + booleanResult);
} else {
label2.setForeground(Color.RED);
label2.setText("Esta es la variable booleana de JFrame anterior: " + booleanResult);
}
panel.setLayout(new BorderLayout());
frame.add(panel);
frame.setBounds(150, 150, 350, 80);
frame.setVisible(true);
panel.add(label1, BorderLayout.NORTH);
panel.add(label2, BorderLayout.SOUTH);
}
}
/* Creamos el método arrancador para hacer la prueba */
public static void main(String[] args) {
Panel p = new Panel();
}
/*
* Finalizado el ejemplo comento que a modo de ejemplo únicamente coloqué
* una clase anidada a la otra hay otras formas más recomendadas de hacer
* esto mismo, pero como todo el código no llegó a 100 lineas me pareció
* apropiado hacerla toda dentro de una misma clase.
*/
}
When the condition is false:
When the condition is true: