GamerBhai02

11. Event Handling

Jan 16th, 2025
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | Source Code | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. public class EventHandling {
  5.     public static void main(String[] args) {
  6.         JFrame frame = new JFrame("Event Handling");
  7.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8.         frame.setSize(300,200);
  9.         frame.setLayout(null);
  10.         JLabel label = new JLabel("Click the button");
  11.         label.setBounds(80, 30, 150, 30);
  12.         frame.add(label);
  13.         JButton button = new JButton("Click Me");
  14.         button.setBounds(90, 80, 120, 30);
  15.         frame.add(button);
  16.         button.addActionListener(new ActionListener() {
  17.             public void actionPerformed(ActionEvent e) {
  18.                 label.setText("Button Clicked!");
  19.             }
  20.         });
  21.         frame.setVisible(true);
  22.     }
  23. }
Add Comment
Please, Sign In to add comment