Java #55: How to change a JLabel Font Size and Style

——————Output—————-

//——Font Code (Full Source Code Below)———————————————

//Creating the second JLabel
JLabel secondLabel = new JLabel(“Spot 2”);

//Creating the font Style + Bold,Plain,Etc, + Size
Font format = new Font(“Impact”, Font.ITALIC, 40);

//setting secondLabel font to our new font
secondLabel.setFont(format);

//adding secondLabel to the frame
sampleFrame.add(secondLabel);

//——–End of Font Code(Full Source Code Below)———————————–
—————–Full Source Code————

//Programmer: Akinwale Owi
//Company: Dagba Computers
//Program Purpose: Changing JLabel Font in Java
package dagba.computers.font;

//imports
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.Dimension;

//Beginning of Only Class
public class DagbaComputersFont {

//Beginning of main
public static void main(String[] args)
{
//Creating the JFrame
JFrame sampleFrame = new JFrame(“Dagba Computers”);

//Setting default close operation
sampleFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Setting JFrame size
sampleFrame.setPreferredSize(new Dimension (400,400));

//Creating the JFrame Layout
sampleFrame.setLayout(new GridLayout(1,1));

//——————JLabels———————–

//Creating first JLabel
JLabel firstLabel = new JLabel(“Spot 1”);

//Adding firstLabel to jFrame
sampleFrame.add(firstLabel);

//Creating the second JLabel
JLabel secondLabel = new JLabel(“Spot 2”);

//Creating the font Style + Bold,Plain,Etc, + Size
Font format = new Font(“Impact”, Font.ITALIC, 40);

//setting secondLabel font to our new font
secondLabel.setFont(format);

//adding secondLabel to the frame
sampleFrame.add(secondLabel);

//KFrame pack and Visibility
sampleFrame.pack();
sampleFrame.setVisible(true);
}//End of the Main Function

}//End of the Only Class DagbaComputersFont Class(Contains the Main Function)

//End of Program
—————-End of Source Code————————–

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: