Program Description:
Output:
Changing JTextField’s font size is one of the useful features in java where you can change the size of the JTextField text using integer numbers. The program below demonstrates on how to this by using the Class Font.
Output:
Code:
/** * File: jtextfieldFontSize.java * Tiltle: How to Change JTextField Text Font Size * Author: http://java-program-sample.blogspot.com */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class jtextfieldFontSize extends JFrame { //Initializing JTextField and Class Font private JTextField s10, s12, s14, s16; private Font s10lAtt, s12Att, s14Att, s16Att; //Setting up GUI public jtextfieldFontSize() { //Setting up the Title of the Window super("How to Change JTextField Text Font Size"); //Set Size of the Window (WIDTH, LENGTH) setSize(350,140); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextField with it's specified strings s10 = new JTextField("10 Text Font Size",22); s12 = new JTextField("12 Text Font Size",18); s14 = new JTextField("14 Text Font Size",18); s16 = new JTextField("16 Text Font Size",15); //Setting the properties of each Font reference s10lAtt = new Font("Arial", Font.PLAIN, 10); s12Att = new Font("Arial", Font.PLAIN, 12); s14Att = new Font("Arial", Font.PLAIN, 14); s16Att = new Font("Arial", Font.PLAIN, 16); //Changing the JTextField Font Size using Font class references s10.setFont(s10lAtt); s12.setFont(s12Att); s14.setFont(s14Att); s16.setFont(s16Att); //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.LEFT); pane.setLayout(flow); //Adding the JTextField components to the container pane.add(s10); pane.add(s12); pane.add(s14); pane.add(s16); /**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) { jtextfieldFontSize jtfs = new jtextfieldFontSize(); } }
Important Part of the Program:
//Initializing JTextField and Class Font private JTextField s10, s12, s14, s16; private Font s10lAtt, s12Att, s14Att, s16Att; //Constructing JTextField with it's specified strings s10 = new JTextField("10 Text Font Size",22); s12 = new JTextField("12 Text Font Size",18); s14 = new JTextField("14 Text Font Size",18); s16 = new JTextField("16 Text Font Size",15); //Setting the properties of each Font reference s10lAtt = new Font("Arial", Font.PLAIN, 10); s12Att = new Font("Arial", Font.PLAIN, 12); s14Att = new Font("Arial", Font.PLAIN, 14); s16Att = new Font("Arial", Font.PLAIN, 16); //Changing the JTextField Font Size using Font class references s10.setFont(s10lAtt); s12.setFont(s12Att); s14.setFont(s14Att); s16.setFont(s16Att);