StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Event Responder
Download
  1. Java
  2. Programs
The Layout Organizer : The Interaction Tracker
Programs
Initiative: To implement reactive logic by handling user interactions with ActionListener.
import javax.swing.*; // Import Swing for UI
import java.awt.*; // Import AWT for fonts and layout
import java.awt.event.*; // Import Event classes for listeners

public class EventResponder { // Define class to handle GUI events
    private static int counter = 0; // Declare a static counter to track clicks
    
    public static void main(String[] args) { // Main program entry point
        JFrame f = new JFrame("Event Interaction Lab"); // Create the frame
        f.setSize(300, 200); // Set window size
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Handle exit
        f.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20)); // Set center-aligned flow layout
        
        JLabel display = new JLabel("Count: 0"); // Create label to show the counter
        display.setFont(new Font("Serif", Font.BOLD, 24)); // Style the counter label
        f.add(display); // Add label to the frame
        
        JButton btn = new JButton("Increment Counter"); // Create a button for interaction
        
        // Add an anonymous inner class as the listener (Classic way)
        btn.addActionListener(new ActionListener() { // Attach a listener for click events
            @Override // Override the action performed method
            public void actionPerformed(ActionEvent e) { // Code to run on click
                counter++; // Increment the global counter variable
                display.setText("Count: " + counter); // Update the visual label with the new value
                System.out.println("Button clicked! Current count: " + counter); // Log click to console
            } // End of action logic
        }); // End of listener attachment
        
        f.add(btn); // Add the button to the frame
        f.setLocationRelativeTo(null); // Center window
        f.setVisible(true); // Show window
        
        System.out.println("Event Lab: Listener active."); // Final log for event lab
    } // End of main method
} // End of EventResponder class
The Layout Organizer The Interaction Tracker
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website