-1

I tried to implement this code that basically what it does is when I press the only button it has, it increments one unit in the label above the button. In other words, it shows how many times has the user pressed the button. But My problem is that I can't seem to refresh the label when I press the button. The variable that holds the number of "clicks" is incremented but in the label it remains with the initial declaration value, 0. Can someone help?

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



public class ContadorCliques extends JFrame {

    private TrataEvento trataEvento;
    private JButton buttonClick;
    private int clickCont = 0;
    private JLabel l1;

    public static void main(String[] args) {

        ContadorCliques contador1 = new ContadorCliques("Hello!");

    }

    public ContadorCliques(String titulo) {

        super(titulo);
        Container c = getContentPane();


        BorderLayout bl = new BorderLayout();
        c.setLayout(bl);

        l1=new JLabel(String.valueOf(clickCont));


        JPanel pBotoes = criarPainelBotao();
        c.add(pBotoes, BorderLayout.SOUTH);
        //add(pBotoes, BorderLayout.SOUTH);

        JPanel pCliques = criarPainelCliques();
        c.add(pCliques, BorderLayout.CENTER);
        //add(pCliques, BorderLayout.CENTER);



        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setMinimumSize(new Dimension(getWidth(), getHeight()));
        setLocationRelativeTo(null);
        setVisible(true);

    }

    private JPanel criarPainelCliques() {

        JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());


        JPanel p = new JPanel();

        lbl.setFont(lbl.getFont().deriveFont(20.0f));
        p.add(lbl, BorderLayout.CENTER);


        return p;
    }

    private JPanel criarPainelBotao() {

        trataEvento = new TrataEvento();

        buttonClick = criarBotao();

        JPanel p = new JPanel();

        p.add(buttonClick);

        return p;
    }

    private JButton criarBotao() {

        JButton btn = new JButton("Click Here!");
        btn.addActionListener(trataEvento);
        return btn;
    }

    private class TrataEvento implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            String obj = (String) e.getActionCommand();
            if (obj.compareTo("Click Here!")==0) {
                clickCont++;
                System.out.println("Funciona");
                System.out.println(clickCont);
                l1.setText(String.valueOf(clickCont)); 

            }

        }
    }

}
camickr
  • 321,443
  • 19
  • 166
  • 288

3 Answers3

0

You never add the JLabel named l1 to anything, and so its text will never be displayed. I suggest you fix this by adding it to the GUI.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

try adding

l1.revalidate();

or

l1.repaint();

or both in that order after setText()

misterti
  • 615
  • 6
  • 15
0

Change your method criarPainelCliques() to look like this:

    private JPanel criarPainelCliques() {

       JLabel lbl = new JLabel("Número de Cliques: ");
       JPanel p = new JPanel();
       lbl.setFont(lbl.getFont().deriveFont(20.0f));
       p.add(lbl, BorderLayout.CENTER);
       p.add(l1);

       return p;
    }

Currently you are only reading your clickvalue once here:

        JLabel lbl = new JLabel("Número de Cliques: " + l1.getText());

l1 is never read again and so your click counter stays on 0.

AlexanderW
  • 123
  • 8