Buttons do not appear in Windowbuilder and problems to stop adding button in an arrayList

Asked

Viewed 57 times

0

I’m having a problem:I can’t figure out the phrase that’s on the given button the moment that "color" is going through it. I wanted to know the phrase on the button when it changes color. another QUESTION: if I put this code piece inside the student class:

for (int i = 0; i < botoes.size(); i++) { 
System.out.println(botoes.get(i).getValor()); 
System.out.println(botoes.size());
 } 

In the addBotão method, of the class Alunocliente, I am not able to understand why the output is repeating values. I wonder why the output repeats the name of the buttons? Because it does not appear once and in this way: I DIDN’T UNDERSTAND; YOU CAN REPEAT; YOU CAN SPEAK MORE SLOWLY; I UNDERSTAND; NO; MORE OR LESS. Why? He repeats the names, the output is this way: I DID NOT UNDERSTAND; DID NOT UNDERSTAND (REPEATED), So on.

package;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class AlunoCliente extends JFrame {

private JPanel jPanel1;
private ArrayList<Botao> botoes = new ArrayList<>();
private ArrayList<BtnOp> opcoes = new ArrayList<>();

public AlunoCliente() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(1500, 800);

jPanel1 = new JPanel();
setContentPane(jPanel1);
jPanel1.setLayout(null);
jPanel1.setLayout(null);

// DO JEITO QUE ESTÁ AQUI NÃO APARECE NO WINDOWBUILDER
addBotao("frase1", 57, 569, 306, 41);
addBotao("frase2", 392, 627, 306, 41);
addBotao("frase3", 57, 627, 306, 41);
addBotao("frase4", 392, 569, 306, 41);

setVisible(true);
}

private void addBotao(String frase, int x, int y, int largura, int altura) {
Botao b = new Botao(frase, jPanel1);
b.getBotao().setBounds(x, y, largura, altura);
botoes.add(b);
jPanel1.add(b.getBotao());
 for (int i = 0; i < botoes.size(); i++) { 
System.out.println(botoes.get(i).getValor()); 
System.out.println(botoes.size());
 } 

}
public ArrayList<Botao> getBotoes() {
return botoes;
}
public ArrayList<BtnOp> getOpcoes() {
return opcoes;
}
}

package;

import javax.swing.JButton;
import javax.swing.JPanel;


public class Botao {
private JButton botao;
private String valor;
private JPanel painel;

public Botao(String frase, JPanel painel){
    this.painel = painel;
    this.botao = new JButton(frase);
    this.botao.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 25));
    this.valor = frase;
}

public JButton getBotao() {
    return botao;
}
public String getValor() {
    return valor;
}
}

package;

import java.awt.Button;

public class BtnOp {
private Button btn;
private int op;

public BtnOp(Button btn, int op) {
    this.btn = btn;
    this.op = op;
}

public Button getBtn() {
    return btn;
}

public int getOp() {
    return op;
}
}

package control;

import visao.Botao;
import visao.BtnOp;

import java.awt.Button;
import java.awt.Color;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;

public class ControllerPainel extends Thread {

    private ArrayList<Botao> botoes = new ArrayList<>();
    private ArrayList<BtnOp> opcoes = new ArrayList<>();
    private int totalBotoes;
    private boolean piscou;

    public ControllerPainel(boolean piscou, ArrayList<Botao> botoes, ArrayList<BtnOp> op) {
        this.botoes = botoes;
        this.piscou = piscou;
        this.totalBotoes = this.botoes.size();
        this.opcoes = op;
    }

    @Override
    public void run() {
        super.run();

        // inicia a varredura dos botões
        for (int indice = 0; indice < this.totalBotoes; indice++) {
            this.limpaBotoes();
            this.limpaBtnOp();

            // Botão da vez
            JButton botao = botoes.get(indice).getBotao();
            // altera a cor do botão
            botao.setBackground(Color.YELLOW);

            try {
                // para no botão
                Thread.sleep(1500);
            } catch (InterruptedException ex) {
                Logger.getLogger(ControllerPainel.class.getName()).log(Level.SEVERE, null, ex);
            }

            // Verificação do último valor
            if (indice == (totalBotoes - 1)) {
                indice = -1;
            }
        }
    }

    /* ALTERANDO TODOS OS BOTÕES PARA PADRÃO */
    private void limpaBotoes() {
        for (int i = 0; i < totalBotoes; i++) {
            JButton b = botoes.get(i).getBotao();
            b.setBackground(Color.CYAN);
        }
    }

    // limpa os botões de opções
    private void limpaBtnOp() {
        for (int i = 0; i < opcoes.size(); i++) {
            Button b = opcoes.get(i).getBtn();
            b.setBackground(new Color(32, 185, 232));
        }
    }

}

package control;

import visao.AlunoCliente;

public class ControleAlunoCliente {

private AlunoCliente ac;
private ControllerPainel painelController;
private boolean piscou;

public ControleAlunoCliente(AlunoCliente ac) {
    this.ac = ac;

    this.painelController = new ControllerPainel(piscou, this.ac.getBotoes(), ac.getOpcoes());
    this.painelController.start();
}

public static void main(String[] args) {
    ControleAlunoCliente c = new ControleAlunoCliente(new AlunoCliente());
}
}
  • Any dynamically generated component will not appear in windowbuilder. Don’t depend on his visualization, it’s just to give you a sense of how the screen looks.

  • I don’t understand what the doubt is. If you want to add the buttons without arraylist, simply remove all the functions that involve the arraylist and create the buttons directly on AlunoCliente. However, you will have more difficulty navigating the buttons, since their reference will not be grouped anywhere in your code.

  • If I add directly, I can’t get the color to go through each button. because the way it is, every second interval the color of the button changes. But if I add one to one, I can’t make it change color

  • Then leave it at that. I especially think that this shape you made is great, even more so if the amount of buttons you will use are many and generated dynamically. Using the lists you can keep the reference of each and manipulate them separately and quickly when necessary :)

  • For example, I’m having a problem:I can’t figure out the phrase that’s on the button given the time that "color" is going through it. wanted to know the phrase that’s on the button when it changes color.

  • and another, if I put this code snippet inside the student class addotao: is (int i = 0; i < buttons.size(); i++) { System.out.println(buttons.get(i).getValor()); System.out.println(buttons.size(); } PQ THE OUTPUT OF THE BUTTONS AS IF THEY HAD DUPLICATED?

  • 1

    I recommend that you rephrase the question with this new doubt and edit the code too that simulates this problem for us to try to help you.

  • I REPHRASED THE QUESTION

Show 3 more comments
No answers

Browser other questions tagged

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