actionPerformed之外的标签不刷新

时间:2015-04-22 16:19:28

标签: java swing actionevent

我试图实现这个代码,基本上它的作用就是当我按下它所拥有的唯一按钮时,它会增加按钮上方标签中的一个单元。换句话说,它显示用户按下按钮的次数。 但我的问题是,当我按下按钮时,我似乎无法刷新标签。保存"点击数量的变量"增加但在标签中它保留了初始声明值0。有人可以帮忙吗?

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)); 

            }

        }
    }

}

3 个答案:

答案 0 :(得分:0)

您永远不会将名为JLabel的{​​{1}}添加到任何内容中,因此永远不会显示其文本。我建议您通过将其添加到GUI来解决此问题。

答案 1 :(得分:0)

尝试添加

l1.revalidate();

l1.repaint();

或在setText()

之后的那个顺序

答案 2 :(得分:0)

将您的方法criarPainelCliques()更改为如下所示:

    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;
    }

目前,您只需在此处阅读一次点击值:

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

l1永远不会再次读取,因此您的点击计数器保持为0。