13.14. The Button Class

The Button class creates push buttons with text labels. The Java programming language doesn't have a built-in class that supports buttons with images. The most common usage for a button is to simply create one with a specified label, drop it in a window, then watch for action events when the button is pressed. For instance, to create and add a button to the current window, you would perform the following two steps.

Button button = new Button("...");
add(button);

Constructors

The Button class has two constructors.

public Button()

public Button(String buttonLabel)

The first constructor creates a button with no label. You can use setLabel to add a label to the button later. The width reported by preferredSize is greater ...

Get Core Web Programming, Second 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.