Java Swing GUI

How to Change Font Color and Font Size of a JTextField in Java Swing

In this tutorial, we are going to see how to change the font color and font size of a JTextField in Java Swing. JTextField is a lightweight component that allows editing a single line of text.


 

Java Program to Change Font Color and Font Size of a JTextField:
import java.awt.*;
import javax.swing.*;

public class TextStyle
{
  TextStyle()
  {
    JFrame frame = new JFrame();
    frame.setLayout(new GridLayout(4,1));
  
    JTextField text = new JTextField();
    // Change text font size
    text.setFont(new Font("Serif",Font.BOLD,30));
    // Change text font color
    text.setForeground(Color.RED);
  
    frame.add(text); 
    frame.setSize(300,200);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
   public static void main(String[] args)
   {
     new TextStyle();
   }
}

Output:


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

Leave a Reply

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