Swing Components

JLabel

What it is:

JLabel is the simplest JComponent. It is a string, image, or both that appears onscreen. The contents can be left-, right-, or center-aligned according to an argument to the constructor. The default is left-aligned. JLabel is a cheap, fast way to get a picture or text on the screen.

How it appears onscreen:

Figure 21-2. How JLabel appears onscreen

image

The code to create it:

//  remember, we are only showing relevant statements from main()     ImageIcon icon = new ImageIcon("star.jpg");     JLabel jl = new JLabel("You are a star", icon, JLabel.CENTER);     frame.add( jl );     frame.pack(); // size the JFrame to fit its contents ...

Get Just Java™ 2 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.