Mega Code Archive

 
Categories / Java Tutorial / Swing Event
 

ActionEvent Example with One Button That Demonstrates Sources, Events, and Their Listeners

import java.awt.BorderLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; class ButtonListener implements ActionListener {   public void actionPerformed(ActionEvent ae) {     Toolkit.getDefaultToolkit().beep();   } } public class UseActionListener {   public static void main(String[] a) {     JFrame frame = new JFrame("Popup JComboBox");     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     JButton source = new JButton("Ring the bell!");     source.addActionListener(new ButtonListener());     frame.add(source, BorderLayout.SOUTH);     source.addActionListener(new ButtonListener());     frame.setSize(300, 200);     frame.setVisible(true);   } }