简体   繁体   English

每当我停止点击时,Java点击计数器都会重置

[英]Java click counter resets every time when I stop clicking

I wanted to make a very simple click counter in Java. 我想用Java做一个非常简单的点击计数器。 It works, but every time when I stop clicking the Click Me button, number of clicks resets. 它有效,但是每次我停止单击“单击我”按钮时,都会重置点击次数。 I tried to solve this problem using static variable called clicks. 我试图使用称为clicks的静态变量来解决此问题。 I know this might sound like a dumb question, but how do you actually prevent the variable from reseting itself. 我知道这听起来像是一个愚蠢的问题,但是您实际上如何防止变量自行重置。

here is the code I wrote. 这是我写的代码。

    package clickcounter;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ClickCounter extends JFrame implements MouseListener{
    private JButton b1 = new JButton("Click me");
    private static int clicks;
    private JLabel info = new JLabel();

    public ClickCounter()
    {
        super("Click counter");
        setSize(250, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b1.addMouseListener(this);
        BorderLayout bor = new BorderLayout();
        setLayout(bor);
        info.setEnabled(false);
        add(BorderLayout.NORTH, b1);
        add(BorderLayout.CENTER, info);
        setVisible(true);
    }


    public static void main(String[] args) {
        ClickCounter cc = new ClickCounter();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        clicks = e.getClickCount();
        info.setText("Number of clicks " + clicks);
        info.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // ignore
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //ignore
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // ignore
    }

    @Override
    public void mouseExited(MouseEvent e) {
       // ignore
    }

}

e.getClickCount() is used to provide details about the 'click'. e.getClickCount()用于提供有关“点击”的详细信息。 It helps applications to respond on double, triped, et.c clicks. 它帮助应用程序响应两次,跳闸等。 So when the user stops clicking it is reset again. 因此,当用户停止单击时,它将再次重置。

Replace 更换

clicks = e.getClickCount();

with

// *Add* the number of clicks that occurred to the click variable
clicks += e.getClickCount();

And your counter is not resetting anymore. 而且您的计数器不再重置。

Note: Making clicks static is not required in this situation. 注意:在这种情况下,不需要使clicks静态。

You should write 你应该写

clicks += e.getClickCount();

instead of 代替

clicks = e.getClickCount();

and init clicks with 和初始化点击

private static int clicks = 0;

thats all. 就这样。

You are not incrementing clicks . 您不会增加clicks You should do this, to count the clicks. 您应该执行此操作以计算点击次数。 (or sum them up) (或总结一下)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM