
how to get no. of times the 'button' is pressed

Here is a java swing code that counts the number of times the button clicked.
import javax.swing.*;
import java.awt.event.*;
public class CountButtonClicks extends JFrame{
public static void main(String[] args){
new CountButtonClicks();
}
JButton button1 = new JButton("Click Me!");
int clickCount = 0;
public CountButtonClicks(){
this.setSize(275, 100);
JPanel panel1 = new JPanel();
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
clickCount++;
button1.setText("clicked " + clickCount + " times!");
}
});
panel1.add(button1);
this.add(panel1);
this.setVisible(true);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.