The javax.swing package library provides JLabel class, which is a basic user interface component in Java Swing. The paint() method of JLabel class is used to draw or paint the label onto a graphical user interface (GUI) component.
Example 1:
import javax.swing.*; import java.awt.*;
public class MyLabel extends JLabel { public void paint(Graphics g) { super.paint(g); g.setColor(Color.RED); g.drawLine(0, 0, getWidth(), getHeight()); g.drawLine(0, getHeight(), getWidth(), 0); } }
This example creates a custom label component (MyLabel) that overrides the paint() method to draw two diagonal lines in red color. The label is added to a JFrame or JPanel container in the usual way.
Example 2:
import javax.swing.*; import java.awt.*;
public class ImageLabel extends JLabel { private Image image;
public ImageLabel(Image image) { super(); this.image = image; }
This example creates a label component (ImageLabel) that displays an image. The constructor takes an Image object as a parameter and sets it to a private variable. The paint() method is overridden to draw the image onto the label using the drawImage() method of Graphics class.
In summary, the javax.swing.JLabel paint() method is used to customize the appearance of a label component by drawing or painting its contents onto a GUI. This method is useful for creating custom labels with images, graphics, text, etc.
Java JLabel.paint - 19 examples found. These are the top rated real world Java examples of javax.swing.JLabel.paint extracted from open source projects. You can rate examples to help us improve the quality of examples.