Java Basic Problems

Java: How to add TextField (JTextField)in java?

Suppose you are trying to prepare a Calculator using java programming language and also you are trying to prepare your calculator using JTextField (javax.swing.JTextField).

Here I have given a little example how to use TextField (JTextField) of java in JFrame.

Using the following code you will be able to learn how to put or use or show ūüôā TextField (JTextField) in java ¬†and how to position the TextField in JFrame.


 Here is the Code Example:

import javax.swing.*;

import java.awt.Dimension;

import java.awt.Toolkit;

public class Calc extends JFrame


   JTextField ques, answer;

   private Toolkit toolkit;

        public Calc()


                        JPanel panel =new JPanel();



                        // UIManager.LookAndFeelInfo laf[]=UIManager.getInstalledLookAndFeels();


¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† ¬†¬†¬† setTitle(“Easy Calculator”);



                Dimension size=toolkit.getScreenSize();

                 setLocation(size.width/2-getWidth()/2, size.height/2-getHeight()/2);

                 ques=new JTextField();

                 answer=new JTextField();

¬†¬†¬†¬†¬†¬†¬† JButton calculate=new JButton(“Calculate”);


        ques.setBounds(10,10,220,30);     // Positioning of TextField(JTextField)

        answer.setBounds(10,100,220,30); //Positioning  of TextField(JTextField)


        panel.add(ques );    // Adding TextField(JTextField) in JPanel

        panel.add(answer );  // Adding TextField(JTextField) in JPanel


          public static void main(String args[])


              Calc  cal=new Calc();




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s