Build Your First Desktop App: A Simple Java Calculator Guide
Creating your first desktop application is a major milestone for any programmer. If you are learning Java, moving from text-based console programs to a visual Graphical User Interface (GUI) is an exciting next step.
This guide will walk you through building a functional desktop calculator using Java’s built-in Swing framework. You will learn how to create a window, layout buttons, and handle user clicks to perform basic math operations. Prerequisites
Before diving into the code, ensure you have the following tools set up on your computer: Java Development Kit (JDK): Version 8 or higher installed.
Integrated Development Environment (IDE): IntelliJ IDEA, Eclipse, or NetBeans. Step 1: Set Up the Project Structure
Create a new Java project in your IDE and name it SimpleCalculator. Inside the project, create a single Java class file named Calculator.java. We will write the entire application inside this file to keep things simple and easy to track.
Open Calculator.java and start by importing the necessary GUI and event-handling libraries:
import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; Use code with caution. Step 2: Design the User Interface (UI)
We will create a class that implements ActionListener so it can respond when buttons are clicked. We need a window (JFrame), a text display (JTextField), and a grid to hold our buttons.
Add the following base structure and variables to your class:
public class Calculator implements ActionListener { // UI Components JFrame frame; JTextField display; JPanel panel; // Calculator buttons JButton[] numberButtons = new JButton[10]; JButton[] functionButtons = new JButton[8]; JButton addButton, subButton, mulButton, divButton; JButton decButton, equButton, delButton, clrButton; // Calculation variables double num1 = 0, num2 = 0, result = 0; char operator; // Font style for text Font myFont = new Font(“Arial”, Font.BOLD, 24); public Calculator() { // Constructor where we will build the UI } public static void main(String[] args) { // Launch the application new Calculator(); } @Override public void actionPerformed(ActionEvent e) { // Handle button clicks here } } Use code with caution. Step 3: Construct the Window and Grid
Now, let’s fill in the Calculator() constructor. We need to initialize the main window frame, format the display screen, and prepare the math buttons.
Inside the public Calculator() brackets, add this setup code:
public Calculator() { // 1. Create the main window frame frame = new JFrame(“Simple Calculator”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(420, 550); frame.setLayout(null); // 2. Create the top display screen display = new JTextField(); display.setBounds(50, 25, 300, 50); display.setFont(myFont); display.setEditable(false); // Prevents users from typing letters // 3. Initialize function buttons addButton = new JButton(“+”); subButton = new JButton(“-”); mulButton = new JButton(“*”); divButton = new JButton(“/”); decButton = new JButton(“.”); equButton = new JButton(“=”); delButton = new JButton(“Del”); clrButton = new JButton(“Clr”); functionButtons[0] = addButton; functionButtons[1] = subButton; functionButtons[2] = mulButton; functionButtons[3] = divButton; functionButtons[4] = decButton; functionButtons[5] = equButton; functionButtons[6] = delButton; functionButtons[7] = clrButton; // Format function buttons and attach click listeners for(int i = 0; i < 8; i++) { functionButtons[i].addActionListener(this); functionButtons[i].setFont(myFont); functionButtons[i].setFocusable(false); } // 4. Initialize number buttons (0-9) for(int i = 0; i < 10; i++) { numberButtons[i] = new JButton(String.valueOf(i)); numberButtons[i].addActionListener(this); numberButtons[i].setFont(myFont); numberButtons[i].setFocusable(false); } // Position Delete and Clear buttons outside the main grid delButton.setBounds(50, 430, 145, 50); clrButton.setBounds(205, 430, 145, 50); // 5. Create a 4x4 Grid Panel for math operations and numbers panel = new JPanel(); panel.setBounds(50, 100, 300, 300); panel.setLayout(new GridLayout(4, 4, 10, 10)); // Row 1 panel.add(numberButtons[1]); panel.add(numberButtons[2]); panel.add(numberButtons[3]); panel.add(addButton); // Row 2 panel.add(numberButtons[4]); panel.add(numberButtons[5]); panel.add(numberButtons[6]); panel.add(subButton); // Row 3 panel.add(numberButtons[7]); panel.add(numberButtons[8]); panel.add(numberButtons[9]); panel.add(mulButton); // Row 4 panel.add(decButton); panel.add(numberButtons[0]); panel.add(equButton); panel.add(divButton); // 6. Assemble components onto the main frame frame.add(panel); frame.add(delButton); frame.add(clrButton); frame.add(display); frame.setVisible(true); } Use code with caution. Step 4: Write the Calculation Logic
The app looks ready, but clicking the buttons does nothing yet. We need to write code inside the actionPerformed method to tell Java how to update the text screen and compute math results. Replace your empty actionPerformed method with this logic: Use code with caution. Step 5: Run Your Desktop Application
Save your file and run the main method in your IDE. A native window will pop up on your screen. Test it by clicking numbers and operators to ensure the logic works correctly. Conclusion and Next Steps
Congratulations! You have just built your very first Java desktop application. In just one file, you configured layouts, built a practical interface, and managed real-time user inputs.
If you want to keep expanding this project, try adding these features to practice your skills:
Keyboard Support: Modify the program to listen for physical keypresses on your keyboard.
Advanced Math: Add buttons for square roots, percentages, or negative numbers.
UI Customization: Experiment with panel.setBackground() and button colors to give your app a modern dark mode theme. If you’d like to expand this application, tell me:
Leave a Reply