# Java program to create a window using swing / JFrame

- Link: https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/
- Published: 2018-11-07T14:05:44+05:30
- Author: Ashishkumar Vishwakarma

In this we will learn a Java program to create a window using swing / JFrame

Write a java program to create a window with four text fields for the name, street,
city, and Pin code with suitable labels also windows contain a button my info when
the user types the name his street city, and pin code and then clicks the button
the details of the type must appear in Arial font with size 20, italics

Also, you can use the Java [compiler](https://www.onlinegdb.com/online_java_compiler)
to compile a program.

Read more: – [Registration Form in Windows Form Using Swing in Java](https://technotaught.com/registration-form-in-windows-form-using-swing-in-java/)

## Java program to create a window using swing/JFrame

### Source code:

    ```language-dotnet
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;    // importing event package for event listener
    class myinfo {

     //Creating Static variables
    static JTextField name_txt ;
    static JTextField sname_txt ;
    static JTextField cname_txt ;
    static JTextField pincode_txt;
    static JButton submit_btn;
    static JTextArea output_txtArea;


    public static void main(String args[])
    {
    		/* ----------------------- Creating JFrame ----------- 
               ------------------------------- */
    		// Step 1 :  Creating a frame using JFrame class	
    	JFrame frame=new JFrame("MY INFORMATION");  
    	frame.setVisible(true);      
    	frame.setBounds(700,700,700,700 );    
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    	// Step 2 : setting background color of Frame. 
    	Container c=frame.getContentPane();   
    	c.setLayout(null);    
    	c.setBackground(Color.white);     

    	/*-------------------- Creating JLabel for Heading Text ------ 
            ----------------- */
    	Font f=new Font("Arial",Font.ITALIC,32);   // Creating font 
            style and size for heading
    	// step 3 : creating JLabel for Heading		
    	JLabel heading_lbl=new JLabel();
    	heading_lbl.setBounds(250,70,400,40);
    	heading_lbl.setText("MY INFORMATION"); 
    	// applying font on  heading Label
    	heading_lbl.setFont(f);



    	/* ------------- Creating Global Font style for all components 
             ----------- */
    	Font f1=new Font("Arial",Font.ITALIC,20);
    	/* ------------- Creating components for Registration details 
             -------------- */
    	// Step 4 : Creating JLabel for Name
    	JLabel name_lbl=new JLabel("Name : ");
    	name_lbl.setBounds(50,150,200,30); 
    	// Creating JTextField for Name
    	name_txt=new JTextField();
    	name_txt.setBounds(180,150,250,30);  
    	// Creating JLabel for Street
    	JLabel sname_lbl=new JLabel("Street : ");
    	sname_lbl.setBounds(50,230,200,30);  
    	// Creating JTextField for Street
    	sname_txt=new JTextField();
    	sname_txt.setBounds(180,230,250,30);
    	// Creating JLabel for the City
    	JLabel cname_lbl=new JLabel("City : ");
    	cname_lbl.setBounds(50,310,200,30); 
    	// Creating JTextArea for the City
    	cname_txt= new JTextField();
    	cname_txt.setBounds(180,310,250,30);
    	// Setting Cursor for components
    	Cursor cur=new Cursor(Cursor.HAND_CURSOR); 
    	//CreatingJLabel for the pincode
    	JLabel pincode_lbl=new JLabel("Pincode: ");
    	pincode_lbl.setBounds(50,390,200,30);
    	// CreatingJTextField for the pincode
    	pincode_txt=new JTextField();
    	pincode_txt.setBounds(180,390,250,30);
    	// CreatingJButton for submit the details
    	submit_btn=new JButton("MyInfo");
    	submit_btn.setBounds(300,450,160,40);
    	submit_btn.setCursor(cur);  // Applying hand cursor on the 
            button
    	// Adding ActionListener on submit button
    	submit_btn.addActionListener(new ActionListener()
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			submit_action(event); 
    		} 
    	});
    	// CreatingJTextArea for output
    	output_txtArea=new JTextArea();
    	output_txtArea.setBounds(500,200,500,500);
    	// Applying Global Font on all the JLabels	
    	name_lbl.setFont(f1);
    	sname_lbl.setFont(f1);
    	pincode_lbl.setFont(f1);
    	cname_lbl.setFont(f1);
    	// Applying Font on all JTextFields
    	name_txt.setFont(f1);
    	sname_txt.setFont(f1);
    	cname_txt.setFont(f1);
    	pincode_txt.setFont(f1);
    	submit_btn.setFont(f1);
    	output_txtArea.setFont(f1);
    	// Adding label components to the container 
    	c.add(heading_lbl); 
    	c.add(name_lbl); 
    	c.add(sname_lbl);
    	c.add(cname_lbl);
    	c.add(pincode_lbl);
    	// AddingJTextField to the container
    	c.add(cname_txt);
    	c.add(sname_txt);
    	c.add(name_txt);
    	c.add(pincode_txt);
    	c.add(submit_btn);
    	c.add(output_txtArea); 
     }
     // Reading value from the MY INFORMATION
    public static void submit_action(ActionEvent event)
    {
    	String name=name_txt.getText();
    	String cname=cname_txt.getText();
    	String sname=sname_txt.getText();
    	String pincode=pincode_txt.getText();
    // displaying value in the JTextArea
    	output_txtArea.setText(" Name  : " +name + "\n\n Street  :  " +sname + "\n\n City     : "+cname +" \n\n Pincode : "+pincode + "\n ");
      }
    }//End of class
    ```

Related: – [Java applet program for calculator](https://technotaught.com/java-applet-program-for-calculator/)

### Output.

> C:\A>javac myinfo.java
> C:\A>java myinfo

![java create window](https://technotaught.com/wp-content/uploads/2018/11/sd.png)

Related: –

### Share this:

 * [  X ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=twitter)
 * [  Facebook ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=facebook)
 * [More](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/#)

 * [  WhatsApp ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=linkedin)
 * [  Email ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?subject=%5BShared%20Post%5D%20Java%20program%20to%20create%20a%20window%20using%20swing%20%2F%20JFrame&body=https%3A%2F%2Ftechnotaught.com%2Fjava-program-to-create-a-window-using-swing-jframe%2F&share=email)
 * [  Reddit ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=reddit)
 * [  Tumblr ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=pinterest)
 * [  Telegram ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/?share=telegram)
 * [  Print ](https://technotaught.com/java-program-to-create-a-window-using-swing-jframe/#print?share=print)
