Use JButton

One of the most commonly used Swing controls is the push button. A push button is an instance of JButton. JButton inherits the abstract class AbstractButton, which defines the functionality common to all buttons. Swing push buttons can contain text, an image, or both, but this book uses only text-based buttons.

JButton supplies several constructors. The one used here is

JButton(String msg)

Here, msg specifies the string that will be displayed inside the button.

When a push button is pressed, it generates an ActionEvent. ActionEvent is defined by the AWT and also used by Swing. JButton provides the following methods, which are used to add or remove an action listener:

void addActionListener(ActionListener al)

void removeActionListener(ActionListener ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.