Revision: 72304
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 5, 2017 20:37 by syedhussim
Initial Code
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; class Main implements ActionListener{ public static void main(String args[]){ Main main = new Main(); } public Main(){ JFrame frame = new JFrame(); JPanel panel = new JPanel(); JButton button1 = new JButton(); button1.setText("Button 1"); button1.addActionListener(this); JButton button2 = new JButton(); button2.setText("Button 2"); button2.addActionListener(this); panel.add(button1); panel.add(button2); frame.getContentPane().add(panel); frame.setSize(200,200); frame.setVisible(true); } public void actionPerformed(ActionEvent e){ switch(e.getActionCommand()){ case "Button 1": System.out.println("You clicked Button 1"); break; case "Button 2": System.out.println("You clicked Button 2"); break; } } }
Initial URL
http://www.snippetcentral.net/java/multiple-jbuttons-with-a-single-actionlistener.html
Initial Description
This code snippet shows how to handle multiple JButtons with a single ActionListener.
Initial Title
Multiple JButtons With A Single ActionListener
Initial Tags
Initial Language
Java