Advertisement
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 | 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. |
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);
}
}
Advertisement
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 -:
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement