Problem with variable data manipulation

Asked

Viewed 43 times

-2

I’d like to pick it up, class Botao, the value that is stored in the variable aux and compare it with a string in the class Janela

class Botao extends JButton{
    public String aux;
    Botao(Menu menu, Janela janela) {
            this.setText(menu.lista[menu.i]);
            menu.i++;    
        this.setFocusable(false);

        this.addActionListener(evt ->{
            menu.setVisible(false);
            aux = this.getText().toString();
            new Janela(this, menu);
        });
    }
}
class Janela extends JFrame{    
    String[] lista2 = {"ID", "Name", "BDay", "Menu"};
    public javax.swing.JComboBox listaBox = new javax.swing.JComboBox(lista2);
    Janela(Botao botao, Menu menu){
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        Botao btn = new Botao(menu, this);
        this.setSize(500, 600);
        this.setLayout(new java.awt.GridBagLayout());

        JPanel topo = new JPanel();
        topo.setLayout(new GridLayout(1,3));
        topo.add(listaBox);
        topo.add(new JTextField());
        topo.add(new JButton("pesquisar"));
        JPanel meio = new JPanel();
        meio.setLayout(new GridLayout(6,2));

        if(btn.aux == "medico") {
            meio.add(new JLabel("Nome"));
            meio.add(new JTextField());
            meio.add(new JLabel("CPF"));
            meio.add(new JTextField());
            meio.add(new JLabel("DataNasc"));
            meio.add(new JTextField());
            meio.add(new JLabel("Telefone"));
            meio.add(new JTextField());
            meio.add(new JLabel("E-mail"));
            meio.add(new JTextField());
            meio.add(new JLabel("OBS"));
            meio.add(new JTextField());
        }

        JPanel baixo = new JPanel();
        baixo.setLayout(new GridLayout(1,3));
        baixo.add(new JButton());
        baixo.add(new JButton());
        baixo.add(new JButton());

        GridBagConstraints gbc = new GridBagConstraints();gbc.insets = new Insets(0, 0, 0, 0);
        gbc.gridy = 1;
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.BOTH;
        this.add(topo, gbc);

        gbc.gridy = 2;
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.weighty = 0.8;
        this.add(meio, gbc);

        gbc.gridy = 3;
        gbc.gridx = 1;
        gbc.weightx = 1;
        gbc.weighty = 0.1;
        this.add(baixo, gbc);



        this.setVisible(true);
    }
}

class Menu extends JFrame{
    int i = 0;
    String[] lista = {"medico", "remedio", "adm", "paciente", "enfermeira", "leito", "receita"};
    Janela janela;
    Botao medico = new Botao(this, janela);
    Botao remedio = new Botao(this, janela);
    Botao adm = new Botao(this, janela);
    Botao enfermeira = new Botao(this, janela);
    Botao paciente = new Botao(this, janela);
    Botao leito = new Botao(this, janela);
    Botao receita = new Botao(this, janela);
    Menu(){
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 600);
        this.setLayout(new GridLayout(7,1));
        this.add(medico);
        this.add(remedio);
        this.add(adm);
        this.add(paciente);
        this.add(enfermeira);
        this.add(leito);
        this.add(receita);
        this.setVisible(true);
    }

}
class Main{
    public static void main(String[] args){
        new Menu();
    }
}

1 answer

2

We can go by pieces to solve the problems.

  1. To name the buttons more easily, just pass the name by parameter:
    Botao medico = new Botao("medico", this, janela);
    Botao remedio = new Botao("remedio",this, janela);
    Botao adm = new Botao("adm", this, janela);
    Botao enfermeira = new Botao("enfermeira", this, janela);
    Botao paciente = new Botao("paciente", this, janela);
    Botao leito = new Botao("leito", this, janela);
    Botao receita = new Botao("receita", this, janela);

Thus, the vector can be deleted, since it was causing problem:

 - String[] lista = {"medico", "remedio", "adm", "paciente", "enfermeira", "leito", "receita"};
  1. Receiving the names by parameter and placing them as the name of each button:
    Botao(String name, Menu menu, Janela janela) {
            this.setText(name);  
            [...]
        }
    }

Now the variable aux may be excluded, as may its declaration:

- public String aux;
- aux = this.getText().toString();
  1. Bearing in mind that the class Knob is received by parameter by the Window class in your constructor:
    class Janela extends JFrame{    
        [...]
        Janela(Botao botao, Menu menu){
        [...]
    }
}

Just use this to make the comparison:

    if(botao.getText() == "medico") {
        [...]
    }
  • 1

    Thank you very much friend, you just saved my skin, I owe you my kidney, amen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.