Learn by easy steps


The way of creating form with a button by using Java swing control has been shown in this tutorial to start designing form using Java. Follow the steps properly to create the java application.


01. Create a Java application with the class named JavaForm.java.

02. Add the following lines before the class declaration to import necessary packages for the application

import java.awt.event.*;
import javax.swing.*;

03. Add the following lines to the main method to create a Form with a button by using JFrame and JButton.

//Create a JFrame Object
JFrame frm = new JFrame();
//Create button
JButton btn = new JButton("Close");
//Set button position
//Add button to frame

04. Add the following lines to set the form size and layout, and display the form in the center of the screen.

  //Set the size of the form
  frm.setSize(400, 150);  
  //Set the title of the form
  frm.setTitle("Welecome to fahmidasclassroom");
  //set form in the center of the screen
  //Display the form 

05. Add the following lines to add event handler code for the button that will close the application.

btn.addActionListener((ActionEvent e) -> {

06. Now, you can run the application.

You can check the video to do the steps properly shown in this tutorial.