O'Reilly logo

Just Java™ 2 by Peter van der Linden

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required