Output:
Code:
/** * File: programJTextField.java * Tiltle: How to Program a JTextField * Author: http://java-program-sample.blogspot.com */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class programJTextField extends JFrame { //Initializing JTextField private JTextField field; //Setting up GUI public programJTextField() { //Setting up the Title of the Window super("How to Program a JTextField"); //Set Size of the Window (WIDTH, LENGTH) setSize(250,100); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextField with a size of 20 with a specified string "Enter Text Here..." field = new JTextField("Enter Text Here...",20); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Adding the JTextField component to the container pane.add(field, BorderLayout.NORTH); /**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) { programJTextField pjtf = new programJTextField(); } }
Important Part of the Program:
//Initializing JTextField private JTextField field; //Constructing JTextField with a size of 20 with a specified string "Enter Text Here..." field = new JTextField("Enter Text Here...",20);