import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonClickExample { public static void main(String[] args) { JFrame frame = new JFrame("Button Click Example"); JButton button = new JButton("Click Me!"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button clicked!\n"+button.getText()); } }); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(button); frame.setVisible(true); }//main }