String text; Icon image; int alignment; //JLabel.LEFT
,JLabel.Center
, orJLabel.RIGHT
.
JLabel yourLabel = new JLabel(text); JLabel yourLabel = new JLabel(text, alignment); JLabel yourLabel = new JLabel(image); JLabel yourLabel = new JLabel(image, alignment); JLabel yourLabel = new JLabel(text, image, alignment);
p.add(new JLabel("Enter your ID:", JLabel.RIGHT));is the same as
JLabel idLabel = new JLabel("Enter your ID:", JLabel.RIGHT); . . . p.add(idLabel);
<html>
and end with </html>
.
The most user-friendly interfaces are usually obtained by using the default appearance. Altho the default is the most common, you might want to change the font size in some situations. Look at JLabel font and color if you really must make changes. A more common way to control the appearance of a JLabel is using HTML (see above).
It's also possible to dynamically change the text of a JLabel, ie, use it for output. This is not generally a good idea because it may change the size of the JLabel and therefore require a change in the layout of components. Using a JTextField is much better. JLabel setting text has information for how to do it tho.