JLabel has an icon property that you can use to set an image in the label component.

Importlmage Icon from javax.swing.

>>> from javax.swing import ImageIcon

Create the image icon, and assign it to the icon property. (In this example, I used one of the images that ships with the JDK. You may have to adjust the file path if you installed tnr JDK somewhere else or if you're using a different JDK version.)

>>> label.icon = ImageIcon("c:\\jdk1.2.1\\demo\\jfc\\java2d\\images\\c >>> frame.pack()

Note that the Image Icon co /etnlCtor we're msing take s a file path to an image. You c an use any gif or jpeg image. You also can change 11i( position of the texe relative co the icon.

>>> label.horizontalTextPosition = JLabel.RIGHT >>> label.verticalTextPosition = JLabel.TOP

Look up setVerticalTextPosition and setHorizontalTextPosition for JLabel in the Java API documentation; elien move ohe label fexr to the bottom, to tnT right of ohe Duke icon.

Images make graphica)user interfaces gtaplhcah JLabel can ersfy nisolhy imrties ac icons, OouS for j ava . awt . Label image nsoiry is not so simple.

Was this article helpful?

0 0

Post a comment