import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; public class SecondGUI extends JFrame { public static void main(String[] args) { // new SecondGUI().setVisible(true); new SecondGUI(); } public SecondGUI(){ super("SecondGUI"); JButton b1,b2,b3; getContentPane().setLayout(new FlowLayout()); getContentPane().add(b1 = new JButton("B1")); getContentPane().add(b2 = new JButton("B2")); getContentPane().add(b3 = new JButton("B3")); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(b1, "B1"); } }); b2.addActionListener( e -> JOptionPane.showMessageDialog(b1, e.getActionCommand()) ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); } }