14.5. The JButton Component

Simple uses of JButton are similar to those for the AWT Button. You create a JButton with a String as a label and then drop the button into a window. Events are normally handled just as with a Button: you attach an ActionListener through the addActionListener method.

New Features: Icons, Alignment, and Mnemonics

The most obvious new feature is the ability to associate images with buttons. Many Swing controls allow the inclusion of icons. Swing introduced a utility class called ImageIcon that lets you easily specify an image file (JPEG or GIF, including animated GIF) for the icon. The simplest way to associate an image with a JButton is to pass the ImageIcon to the constructor, either in place of the text or in addition ...

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.