Using Image Icons and Toolbars
One of the easiest ways to improve the visual appeal of a GUI is to use icons, small images used to identify buttons and other parts of an interface.
With many of the components in the Swing class library, you can label a component with an image instead of text by using the ImageIcon
class in the javax.swing
package.
Caution
Although some operating systems use the \
character to separate folders and filenames, the ImageIcon
constructor requires the /
character as a separator.
You can create an ImageIcon
from a file on your computer by calling the ImageIcon(
String)
constructor method. The argument to the method is either the name of the file or its location and name, as in these examples:
ImageIcon stopSign = ...
Get Sams Teach Yourself Java™ in 24 Hours, Sixth 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.