<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">

// Example: the frame is the listener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ListenerExample1 extends JFrame implements ActionListener {

   private int count = 0;
   private JButton b = new JButton("Push Me!");
   private JLabel label = new JLabel(generateLabel());

   public static void main(String[] args) {
      new ListenerExample1();
   }

   public ListenerExample1() {
      setLayout(new FlowLayout(FlowLayout.LEFT));
      add(b);
      add(label);
      b.addActionListener(this);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {
      count++;
      label.setText(generateLabel());
      pack(); // label might have grown, need to repack
   }

   private String generateLabel() {
      return "Count: " + count;
   }
}
</pre></body></html>