JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus. By default labels are vertically centered but the user can change the alignment of label.
Constructor of the class are :
- JLabel() : creates a blank label with no text or image in it.
- JLabel(String s) : creates a new label with the string specified.
- JLabel(Icon i) : creates a new label with a image on it.
- JLabel(String s, Icon i, int align) : creates a new label with a string, an image and a specified horizontal alignment
Commonly used methods of the class are :
- getIcon() : returns the image that that the label displays
- setIcon(Icon i) : sets the icon that the label will display to image i
- getText() : returns the text that the label will display
- setText(String s) : sets the text that the label will display to string s
1. Program to create a blank label and add text to it.
2. Program to create a new label using constructor – JLabel(String s)
3. Program to create a label and add image to it .
4. Program to add a image and string to a label
Note : This programs might not run in an online compiler please use an offline IDE.
- Java Swing | Translucent and shaped Window in Java
- Java Swing | ToolTip
- Java Swing | JSlider
- Java Swing | JPasswordField
- Java Swing | JProgressBar
- Java Swing | JSpinner
- JRadioButton | Java Swing
- Java Swing | JPopupMenu
- Java Swing | MatteBorder
- Java Swing | JTable
- Java Swing | Look and Feel
- Java Swing | JTextArea
- Java Swing | JTextField
- Java Swing | JMenuBar
- Java Swing | JFileChooser
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.