The Customer Form Application is a simple graphical user interface (GUI) application built in Java using Swing. It allows users to input customer details, such as name, customer number, age, and address. This project serves as an educational example for understanding basic GUI development in Java and handling form-based user inputs.
This project focuses on creating a form with four fields: Customer Name, Customer Number, Age, and Address. The form is displayed in a JFrame
window, and the layout is managed using BorderLayout
and GridLayout
. The goal is to capture user input in a user-friendly interface.
Use Case:
This type of form can be used in applications such as customer databases, registration systems, or surveys.
CustomerForm
: Main class of the application extending JFrame
. It handles the layout and user interface components.
import javax.swing.*;
import java.awt.*;
public class CustomerForm extends JFrame {
private JTextField nameField, numberField, ageField, addressField;
public CustomerForm() {
// Setting layout of the frame
setLayout(new BorderLayout());
// Heading Label at the top
JLabel headingLabel = new JLabel("Customer Information", JLabel.CENTER);
headingLabel.setFont(new Font("Arial", Font.BOLD, 24));
headingLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); // Add some padding
add(headingLabel, BorderLayout.NORTH);
// Creating form panel
JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10)); // 4 rows, 2 columns with 10px gaps
// Customer Name
JLabel nameLabel = new JLabel("Customer Name:");
nameField = new JTextField();
formPanel.add(nameLabel);
formPanel.add(nameField);
// Customer Number
JLabel numberLabel = new JLabel("Customer Number:");
numberField = new JTextField();
formPanel.add(numberLabel);
formPanel.add(numberField);
// Age
JLabel ageLabel = new JLabel("Age:");
ageField = new JTextField();
formPanel.add(ageLabel);
formPanel.add(ageField);
// Address
JLabel addressLabel = new JLabel("Address:");
addressField = new JTextField();
formPanel.add(addressLabel);
formPanel.add(addressField);
// Add the form panel to the center
add(formPanel, BorderLayout.CENTER);
// Setting frame size and closing behavior
setSize(400, 300); // Width x Height
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the application when the window is closed
}
public static void main(String[] args) {
// Run the application
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CustomerForm frame = new CustomerForm(); // Create the frame
frame.setVisible(true); // Make the frame visible
}
});
}
}
import javax.swing.*;
import java.awt.*;
public class CustomerForm extends JFrame {
private JTextField nameField, numberField, ageField, addressField;
public CustomerForm() {
// Setting layout of the frame
setLayout(new BorderLayout());
// Heading Label at the top
JLabel headingLabel = new JLabel("Customer Information", JLabel.CENTER);
headingLabel.setFont(new Font("Arial", Font.BOLD, 24));
headingLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); // Add some padding
add(headingLabel, BorderLayout.NORTH);
// Creating form panel
JPanel formPanel = new JPanel(new GridLayout(4, 2, 10, 10)); // 4 rows, 2 columns with 10px gaps
// Customer Name
JLabel nameLabel = new JLabel("Customer Name:");
nameField = new JTextField();
formPanel.add(nameLabel);
formPanel.add(nameField);
// Customer Number
JLabel numberLabel = new JLabel("Customer Number:");
numberField = new JTextField();
formPanel.add(numberLabel);
formPanel.add(numberField);
// Age
JLabel ageLabel = new JLabel("Age:");
ageField = new JTextField();
formPanel.add(ageLabel);
formPanel.add(ageField);
// Address
JLabel addressLabel = new JLabel("Address:");
addressField = new JTextField();
formPanel.add(addressLabel);
formPanel.add(addressField);
// Add the form panel to the center
add(formPanel, BorderLayout.CENTER);
// Setting frame size and closing behavior
setSize(400, 300); // Width x Height
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close the application when the window is closed
}
public static void main(String[] args) {
// Run the application
SwingUtilities.invokeLater(new Runnable() {
public void run() {
CustomerForm frame = new CustomerForm(); // Create the frame
frame.setVisible(true); // Make the frame visible
}
});
}
}
Using JFrame
instead of JApplet
:
JFrame
, which is better suited for standalone applications.JFrame
window is initialized in the constructor and made visible in the main()
method.main()
Method:
main()
method is necessary for any standalone Java application. It creates an instance of CustomerForm
(a JFrame
), sets its size, and makes it visible.Form Layout:
GridLayout
is used for the form with labels and text fields (Customer Name, Number, Age, and Address), while BorderLayout
positions the form components in the frame.Closing Behavior:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
ensures the application exits when the frame is closed.Now, you can run this program as a standard Java application without the need for a browser or applet viewer. Simply compile and run it in your preferred IDE or terminal:
javac CustomerForm.java
java CustomerForm
The Customer Form Application demonstrates the use of Java Swing to create a simple GUI form. It is a foundational project that showcases form input handling and basic layout management using JFrame
and GridLayout
. Future expansions can include more advanced features like form validation, data storage, and enhanced UI designs.
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India