JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.", "Egg Warning", JOptionPane.WARNING_MESSAGE);This command specifies that the dialog box is associated with the JFrame pointed to by frame, that the warning message is "Eggs are not supposed to be green.", that the title of the window displaying the message will be "Egg Warning", and that the icon in the message window will denote a warning.
class WidgetCreater implements ActionListener { ... JTextField name; ... public widgetCreater() { name = JTextField(); name.addActionListener(this); } public void actionPerformed(ActionEvent e) { ... } }The drawback of this approach is that you can only easily define one listener per event and if you need to create multiple event listeners for different widgets that generate the same event, then you cannot easily use this approach.
class WidgetCreater { ... JTextField name; ... public widgetCreater() { name = JTextField(); name.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ... } }); } }This approach allows you to define a listener for each widget that generates that type of event and it also allows someone reading your code to immediately see what action is being taken in response to the event.