Java program to create a window using swing / JFrame

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 to compile a program.

Read more: – Registration Form in Windows Form Using Swing in Java

Java program to create a window using swing/JFrame

Source code:

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

Output.

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

java create window

Related: –

About Ashishkumar Vishwakarma

I am Ashish- a Developer live in Mumbai.

View all posts by Ashishkumar Vishwakarma →

Leave a Reply