Advertisement



< Prev
Next >



JLabel class




JLabel class is used to create a label, which can be used to display an information to the user or a text before the textfield or an image. JLabel is a component which extends JComponent class and it can be added to the container.




Constructors of JLabel


Constructor Description
public JLabel() Creates a JLabel with an empty text.
public JLabel(String text) Creates a JLabel with an specified text.
public JLabel(Icon image) Creates a JLabel with an icon.





Methods of JLabel class


Methods Description
public void setText(String text) Sets a String message on the JLabel.
public String getText() Gets a String message of JLabel.
public void setIcon(Icon icon) Sets an icon or image over the JLabel.
public Icon getIcon() Gets the icon or image of the JLabel.
void setHorizontalTextPosition(int textPosition) Sets the JLabel message on the LEFT/RIGHT of its icon or image.
void setVerticalTextPosition(int textPosition) Sets the JLabel message on the TOP/BOTTOM of its icon or image.





An example of JLabel


import javax.swing.*;
import java.awt.*;

public class Swing2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

} //Closing the main method
} //Closing the class Swing1

class A
{
JFrame jf;

JLabel label1, label2, label3;


A()
{
jf = new JFrame("JLabel example");

label1 = new JLabel();
label2 = new JLabel("Hello, how are you?");
label3 = new JLabel("How is your day going on?");


label1.setText("Welcome"); 	//Setting the text of a JLabal
jf.setLayout(new FlowLayout());
jf.add(label1);			//Adding label1 to the Jframe
jf.add(label2);			//Adding label2 to the Jframe
jf.add(label3);			//Adding label3 to the Jframe
jf.setVisible(true);
jf.setSize(300,300);
}
}

When you run the code, you are presented a window shown below -:

Figure 1



Advertisement




Setting an icon image on the JLabel


import javax.swing.*;
import java.awt.*;

public class Swing3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

} //Closing the main method
} //Closing the class Swing1

class A
{
JFrame jf;

JLabel label1, label2;


A()
{
jf = new JFrame("JLabel example");

label1 = new JLabel();
label2 = new JLabel();


label1.setText("A sunny day");
label1.setHorizontalTextPosition(SwingConstants.LEFT);
label1.setIcon(new ImageIcon("Smiley.gif"));

label2.setText("A rainy day");
label2.setHorizontalTextPosition(SwingConstants.RIGHT);
label2.setIcon(new ImageIcon("Smiley2.gif"));

jf.setLayout(new FlowLayout());
jf.add(label1);
jf.add(label2);

jf.setVisible(true);
jf.setSize(300,300);
}
}
When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2




Please share this article -





< Prev
Next >
< JDialog
JTextField >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement