- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement a program to count the number in Java?
The program uses a JLabel to hold a count label, a JTextField component to hold the number count, JButton component to create add, remove and reset buttons. When we click the add button, the count in the JTextField will get incremented by '1' and by clicking the remove button the count will be decremented by '1'. If we click the Reset button, it will reset the count to '0'.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CounterTest extends JFrame implements ActionListener { private JLabel label; private JTextField text; private JButton addBtn, removeBtn, resetBtn; private int count; public CounterTest() { setTitle("Counter Test"); setLayout(new FlowLayout()); count = 0; label = new JLabel("Count:"); text = new JTextField("0", 4); addBtn = new JButton("Add"); removeBtn = new JButton("Remove"); resetBtn = new JButton("Reset"); addBtn.addActionListener(this); removeBtn.addActionListener(this); resetBtn.addActionListener(this); add(label); add(text); add(addBtn); add(removeBtn); add(resetBtn); setSize(375, 250); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == addBtn) { count++; // increment the coiunt by 1 text.setText(String.valueOf(count)); repaint(); } else if (ae.getSource() == removeBtn) { count--; // decrement the count by 1 text.setText(String.valueOf(count)); repaint(); } else if (ae.getSource() == resetBtn) { count = 0; // reset the count to 0 text.setText(String.valueOf(count)); repaint(); } } public static void main(String[] args) { new CounterTest(); } }
Output
- Related Articles
- Java program to count total bits in a number
- Java Program to count the number of words in a String
- How to count the number characters in a Java string?
- Java program to count the number of consonants in a given sentence
- Java program to count the number of vowels in a given sentence
- Java program to Count the number of digits in a given integer
- Java Program to Count trailing zeroes in factorial of a number
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- How to implement Count (*) as variable from MySQL to display the number of records in a table?
- Java Program to Count Number of Digits in an Integer
- Golang Program to Count the Number of Digits in a Number
- Java Program to Implement LinkedList
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- Java Program to count letters in a String

Advertisements