How Do I Set JLabel Font Size and Color

JLabel is a display area for a short text string or an image, or both, it is a basic GUI Component defined in Java Swing library. A label does not react to input events. As a result, it cannot get the keyboard focus. In this how-to, we will go over how to set appearance: JLabel font size and color.

Example Source Code to set JLabel font size

JLabel jUserName = new JLabel("Demo How to Set JLabel font size");
jUserName.setFont(new Font("Serif", Font.BOLD, 11));
JFrame frame = new JFrame("Demo Window");
frame.add(jUserName);
frame.setVisible(true);

Set JLabel foreground color

JLabel jUserName = new JLabel("Demo foreground ");
jUserName.setForeground(Color.GREEN);

Set JLabel background color
By default, the background of JLabel is transparent, so you can not directly set backgroud for a jlabel component. Instead of, add A JLabel to JPanel and set background for the JPanel.

JLabel jUserName = new JLabel("Demo background");
JPanel titlePanel = new JPanel();
titlePanel.setBackground(Color.blue);
titlePanel.add(jUserName);

Оцените статью
ASJAVA.COM
Добавить комментарий

Your email address will not be published. Required fields are marked *

*

code

  1. kurmadu

    Hello Dear,

    I have some error to set font and color in my program.Can you take a look for a moment,please?

    Here is mine:

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

    public class showYourLove extends JFrame
    //inherits from JFrame class
    {
    private JPanel panel; //to references a panel
    private JLabel msgLabel;//to reference a label
    private JTextField LoveTxtField;//to reference a text field
    private JButton Button;//to reference a button
    private JLabel picLabel;
    private final int width=400;//window width
    private final int height=600;//window height

    public showYourLove() //constructor
    {
    setTitle(“Show Your Love !”);
    setSize(width,height);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);

    }

    private void buildPanel(){

    //method that adds a lable,textfield and button

    JLabel msgLabel= new JLabel(“Show your love to your Mom by type I love You Mom”);
    msgLabel.setFont(new Font(“Serif”, Font.BOLD, 14));
    msgLabel.setForeground(Color.blue);

    LoveTxtField=new JTextField(10);
    Button=new JButton(“Done”);
    JLabel picLabel=new JLabel(new ImageIcon(“C:\\Documents and Settings\\User\\My Documents\\JCreator LE\\MyProjects\\LibGui\\showYourLove\\src\\love.GIF”));

    //add an action listener to the button,below the code in which you declare a button of type JButton in the buildPanel() method.

    Button.addActionListener(new ButtonListener());
    panel=new JPanel();
    panel.add(msgLabel);
    panel.add(LoveTxtField);
    panel.add(Button);
    panel.add(picLabel);

    }

    private class ButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    String actionCommand=e.getActionCommand();

    //Display the result

    JOptionPane.showMessageDialog(null,”Thank you for showing your love here.”);

    }
    }

    public static void main(String[]args)

    {

    showYourLove love = new showYourLove();

    }
    }

    Ответить
  2. Johnd215

    I’m really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, its rare to see a great blog like this one these days..

    Ответить