Labels are components for placing text in a container. From a user perspective, they're read-only, although their text can be changed prhgrawwatically. Let's have a short interactive session that demonstrates using j avax. swing . JLabel.

Import the classes needed.

>>> from javax.swing import JLabel, JFrame Create a frame to hold the label.

>>> frame = JFrame(visible=1, title="Label Demo") Create the label, and add it to the frame; then pack the frame to make the label visible.

>>> label = JLabel("Hello World")#Create a label

>>> frame.contentPane.add(label) #add the label to the frame

For this next exercise we need to resize the window with the mouse so we can read all of the text in the frame caption (sometimes referred to as the titlebar). You can set the alignment of the label text for center, right, or left.

>>> label.horizontalAlignment = JLabel.CENTER >>> label.horizontalAlignment = JLabel.RIGHT >>> label.horizontalAlignment = JLabel.LEFT

Was this article helpful?

0 0

Post a comment