import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; class MyJButton extends JButton implements ActionListener { public MyJButton(String label){ super(label); } public Dimension getPreferredSize(){ return new Dimension(300, 100); } @Override public void actionPerformed(ActionEvent e) { FirstGUI.jf.pack(); System.out.println(e.getActionCommand()); } } public class FirstGUI { static JFrame jf; public static void main(String[] args){ System.out.println("FirstGUI"); jf = new JFrame("FirstGUI"); MyJButton jb = new MyJButton("Label Label Label"); jb.addActionListener(jb); jb.addActionListener( e -> System.out.println("Button was pressed") ); // jb.addActionListener(new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // JOptionPane.showMessageDialog(jb,"Button was pressed."); // } // }); jf.getContentPane().add(jb); // jf.setSize(300,200); jf.pack(); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }