Swing Components


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


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

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