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("...");


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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.