Saturday, October 29, 2011

JLabel Set Font

   package org.best.example;
    /*
            JLabel Set Font Example
            This java example shows how to set custom font for JLabel using
            Java Swing JLabel class.
    */   
    
    import java.awt.Font;
    
    import javax.swing.JApplet;
    import javax.swing.JLabel;
     
    public class JLabelSetFontExample extends JApplet{
    
            public void init(){
                  
                    /*
                     * To create JLabel use
                     * JLabel (String caption) constructor
                     * of JLabel class.
                     */
                  
                    JLabel label1 = new JLabel("JLabel Set Font Example.");
                          
                    /*
                     * To set custom font for JLabel use,
                     * void setFont(Font font)
                     * method.
                     */
                  
                    //create new Font
                    Font font = new Font("Courier", Font.BOLD,12);
                  
                    //set font for JLabel
                    label1.setFont(font);
                  
                    //add label to applet
                    add(label1);
            }
    }

No comments: