How To Change Font Size And Font Style Of A JLabel - StackHowTo

In this tutorial, we are going to see how to change the font size and font style of a JLabel in Java Swing. JLabel is an area to display short text or an image, or both, it is a basic GUI component defined in the Java Swing library. A label does not react to input events. Therefore, it cannot get keyboard focus.

How to change font size of the JLabel
JLabel label = new JLabel("This is a label!"); label.setFont(new Font("Serif", Font.BOLD, 20)); JFrame frame = new JFrame(); frame.add(label); frame.setVisible(true);

Output:

How to change the color of the JLabel
label.setForeground(Color.RED);

Output:

How to change the background color of the JLabel
label.setBackground(Color.ORANGE); label.setOpaque(true);

Output:

How to center the text in a JLabel
label = new JLabel("This is a label!", SwingConstants.CENTER);

Output:

Complete Example: How to Change Font Size and Font Style of a JLabel
import java.awt.*; import javax.swing.*; public class StyleJLabel { StyleJLabel() { JFrame frame = new JFrame(); frame.setLayout(new GridLayout(4,1)); JLabel label = new JLabel("This is a label!", SwingConstants.CENTER); label.setFont(new Font("Serif", Font.BOLD, 20)); label.setForeground(Color.RED); label.setBackground(Color.ORANGE); label.setOpaque(true); frame.add(label); frame.setSize(300,150); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new StyleJLabel(); } }

Output: mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

  • JFrame Exit on Close Java Swing
  • How to Set JFrame in Center of the Screen
  • How to Fix the Size of JFrame in Java
  • How to Set the Title of a JFrame in Java
  • How to Change the Size of a JFrame(window) in Java
  • JFrame – Java Swing – Example
  • JPanel – Java Swing – Example
  • JLabel – Java Swing – Example
  • JButton – Java Swing – Example
  • JTextField – Java Swing – Example
  • JTextArea – Java Swing – Example
  • JCheckBox – Java Swing – Example
  • JRadioButton – Java Swing – Example
  • JComboBox – Java Swing – Example
  • JMenu, JMenuBar and JMenuItem – Java Swing – Example
  • JDialog – Java Swing – Example
  • Dialog boxes – JOptionPane – Java Swing – Example
  • JProgressBar – Java Swing – Example
  • JPasswordField – Java Swing – Example
  • JFileChooser – Java Swing – Example
  • JSlider – Java Swing – Example
  • JSpinner – Java Swing – Example
  • JTree – Java Swing – Example
  • JToolBar – Java Swing – Example
  • JList – Java Swing – Example
  • JToggleButton – Java Swing – Example
  • JSeparator – Java Swing – Example
  • JColorChooser – Java Swing – Example
  • FlowLayout – Java Swing – Example
  • GridLayout – Java Swing – Example
  • BorderLayout – Java Swing – Example
  • BoxLayout – Java Swing – Example
  • CardLayout – Java Swing – Example
  • GridBagLayout – Java Swing – Example
  • JComponent – Java Swing – Example
  • GroupLayout – Java Swing – Example
  • SpringLayout – Java Swing – Example
  • JLayeredPane – Java Swing – Example
  • JSplitPane – Java Swing – Example
  • ScrollPaneLayout – Java Swing – Example
  • Event and Listener – Java Swing – Example
  • ActionListener – Java Swing – Example
  • MouseListener – Java Swing – Example
  • KeyListener – Java Swing – Example
  • How to Change Java Icon in JFrame
  • How to Change Font Size and Font Style of a JLabel
  • How to Count the Clicks on a Button in Java
  • How to Get Mouse Position on Click Relative to JFrame
  • How to Change Look and Feel of Swing Application
  • How to display an image on JFrame in Java Swing
  • How to Add an Image to a JPanel in Java Swing
  • How to Change Font Color and Font Size of a JTextField in Java Swing
  • How to Display Multiple Lines in Tooltip
  • How to dynamically filter JTable from textfield in Java
  • How to get Value of Selected JRadioButton in Java
  • How to get the selected item of a JComboBox in Java
  • How to Populate JTable from Database
  • How to insert and retrieve an image from MySQL database using Java
  • How to Create a Vertical Menu Bar in Java Swing
  • How to add real-time date and time in JFrame
  • Use Enter key to press JButton instead of mouse click
  • How to add text to an image in Java
  • How to Clear JTextArea by Clicking JButton
  • How to use JFileChooser to display image in a JFrame
  • How to Get the State of JCheckBox in Java Swing
  • How to link two JComboBox together in Java Swing
  • How to Display Multiple Images in a JFrame
  • How to draw lines, rectangles, and circles in JFrame
  • How to Display a Webpage Inside a Swing Application
  • Difference between JTextField and JFormattedTextField in Java
  • How to Make JTextField Accept Only Alphabet
  • How to Make JTextField Accept Only Numbers
  • How To Limit the Number of Characters in JTextField
  • How to Capitalize First Letters in a JTextField in Java
  • Convert to Uppercase while Writing in JTextField
  • How to Add a Listener for JTextField when it Changing
  • How to Create Rounded JButton in Java
  • How to Disable JButton when JTextField is Empty
  • How to Make JButton with Transparent Background
  • How to Change JButton Text on Click
  • How to Change the Border of a JFrame in Java
  • How to Change Font Size in a JButton
  • How to Remove Border Around JButton in Java
  • How to Remove Border Around Text in JButton
  • How to Change Border Color of a JButton in Java Swing
  • How to Change Button Color on Click
  • How to Change JButton Text Color
  • How to Change the Background Color of a JButton
  • How to Change the Position of JButton in Java
  • How to Add JSpinner to JTable
  • How to Update a Row in JTable
  • How to Populate Jtable with a Vector
  • How to Print a JTable with Image in Header
  • How to Delete a Row in JTable using JButton​
  • How to Add a JComboBox to a JTable Cell
  • How to Get Selected Value from JTable in Java
  • How to Hide a Column in JTable
  • How to Sort JTable Column in Java [2 Methods]
  • How to Change Color of Column in JTable
  • How to Alternate Row Color of JTable in Java
  • How to Change Background Color of JTable Cell on Mouse Click
  • How to Count Number of Rows and Columns of a JTable
  • How to Add Button in JTable
  • How to add JCheckBox in JTable
  • How to Add Row Dynamically in JTable Java
  • How to Display Image in JTable in Java
  • How to Create Multi-Line Header for JTable
  • How to Set Column Width in JTable in Java
  • How to Increase Row Height in JTable
  • How to Remove Jtable Header in Java
  • How to Use setBounds() Method in Java
  • How to Know Which Button is Clicked in Java Swing
  • How to Close a JFrame in Java by a Button
  • How to add onclick event to JButton using ActionListener in Java Swing
  • How to add checkbox in menuItem of jMenu in Java Swing
  • How to create a right-click context menu in Java Swing
  • How to Customize JComboBox in Java
  • How to Create Hyperlink with JLabel in Java
  • How to add an object to a JComboBox in Java
  • How to add and remove items in JComboBox in Java
  • How to Add Image Icon to JButton in Java Swing
  • How to Disable JTextArea and JtextField
  • How to Create Multiple Tabs in Java Swing
  • How to create a custom cursor in Java
  • How to Set Background Image in Java Swing
  • How to Delete a Selected Row from JTable in Java
  • How to Change Background Color of a Jbutton on Mouse Hover
  • Detect Left, Middle, and Right Mouse Click – Java
  • How to Create Executable JAR File in Java
  • Export Data from JTable to Excel in Java

Tag » How To Change Jlabel Text