How to move a Boolean variable created in a Jframe and be able to use it in a different Jframe?

0

My problem is that I have this in a Jframe The idea is that the user write 2 propositions (can be true or false) to be able to make operations with these

 private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        String Prop1 = "", Prop2 = "";
        Prop1 = this.Prop1.getText();
        Prop2 = this.Prop2.getText();
        boolean Prop1Bo = Boolean.parseBoolean(Prop1);
        boolean Prop2Bo = Boolean.parseBoolean(Prop2);
        OperadoresLogicos ir = new OperadoresLogicos();
        ir.setVisible(true);
        this.dispose();
    }           
public static boolean Prop1Bo, Prop2Bo;

Then, when doing this, it opens another frame where I can choose what operation to do, for now I have only programmed the conjunction

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        boolean Prop1Bo = Proposiciones2.Prop1Bo;
        boolean Prop2Bo = Proposiciones2.Prop2Bo;
        boolean Conjuncion1 = Prop1Bo && Prop2Bo;
        String Con = Boolean.toString(Conjuncion1);
        JOptionPane.showMessageDialog(null, "La conjunción es : " + Con)

In doing this, the answer you give me is always false and that does not work for me, and I really do not know what the problem is, please help urgently.

    
asked by Sebastian Lopez 18.11.2017 в 04:02
source

2 answers

0

As I understand it, you try to use the Boolean static variable Propositions2.Prop1Bo and Propositions2.Prop2Bo in the new JFrame ( OperadoresLogicos ) according to the value that you have added to the JFrame Propositions2 .

You should know that any variable of the class will have a default value (if it is a variable of integer type, by default its value is 0 , if it is a boolean its default value is false , if it is an object, it would be null ). Once this is cleared, by declaring this in your class Propositions2 :

public static boolean Prop1Bo, Prop2Bo;

The value of both variables is ... false!

And when you do this:

boolean Prop1Bo = Boolean.parseBoolean(Prop1);
boolean Prop2Bo = Boolean.parseBoolean(Prop2);

You are creating new variables, which are totally different from the static variables, so it should look something like this:

Proposiciones2.Prop1Bo = Boolean.parseBoolean(Prop1);
Proposiciones2.Prop2Bo = Boolean.parseBoolean(Prop2);

There you would be assigning the Boolean values to the variables! Otherwise, you would simply be ignoring the new values and the default values ( false ) would be used.

Greetings! :)

    
answered by 18.11.2017 / 06:24
source
0

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:

    
answered by 18.11.2017 в 13:54