Program Description:
Output:
Changing JTextField’s text Font is one of its cool abilities because it adds clarity to the text by making it BOLD, ITALIC, or BOLD-ITALIC. The program below is a Java Code that demonstrates on how to change JTextField fonts by using the Java Class Font. The program creates four JTextField with each specified fonts to make it more understandable in terms of properties and implementation of the Class Font.
Output:
Code:
/** * File: jtextfieldFonts.java * Tiltle: How to Change JTextField Text Font * Author: http://java-program-sample.blogspot.com */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class jtextfieldFonts extends JFrame { //Initializing JTextField and Class Font private JTextField bold, plain, italic, bolditalic; private Font boldAtt, plainAtt, italicAtt, bolditalicAtt; //Setting up GUI public jtextfieldFonts() { //Setting up the Title of the Window super("How to Change JTextField Text Font"); //Set Size of the Window (WIDTH, HEIGHT) setSize(300,140); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextField with a size of 20 with it's specified strings bold = new JTextField("Bold Text Font",20); plain = new JTextField("Plain Text Font",20); italic = new JTextField("Italic Text Font",20); bolditalic = new JTextField("Bold-Italic Text Font",20); //Setting the properties of each Font reference boldAtt = new Font("Arial", Font.BOLD, 14); plainAtt = new Font("Arial", Font.PLAIN, 14); italicAtt = new Font("Arial", Font.ITALIC, 14); bolditalicAtt = new Font("Arial", Font.BOLD+Font.ITALIC, 14); //Changing the JTextField Fonts bold.setFont(boldAtt); plain.setFont(plainAtt); italic.setFont(italicAtt); bolditalic.setFont(bolditalicAtt); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Setting up the container layout FlowLayout flow = new FlowLayout(FlowLayout.CENTER); pane.setLayout(flow); //Adding the JTextField components to the container pane.add(bold); pane.add(plain); pane.add(italic); pane.add(bolditalic); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); } //Main Method public static void main (String[] args) { jtextfieldFonts jtff = new jtextfieldFonts(); } }
Important Part of the Program:
//Initializing JTextField and Class Font private JTextField bold, plain, italic, bolditalic; private Font boldAtt, plainAtt, italicAtt, bolditalicAtt; //Constructing JTextField with a size of 20 with it's specified strings bold = new JTextField("Bold Text Font",20); plain = new JTextField("Plain Text Font",20); italic = new JTextField("Italic Text Font",20); bolditalic = new JTextField("Bold-Italic Text Font",20); //Setting the properties of each Font reference boldAtt = new Font("Arial", Font.BOLD, 14); plainAtt = new Font("Arial", Font.PLAIN, 14); italicAtt = new Font("Arial", Font.ITALIC, 14); bolditalicAtt = new Font("Arial", Font.BOLD+Font.ITALIC, 14); //Changing the JTextField Fonts bold.setFont(boldAtt); plain.setFont(plainAtt); italic.setFont(italicAtt); bolditalic.setFont(bolditalicAtt);