0% found this document useful (0 votes)
97 views8 pages

Chegg 1

This document contains the code for a Java Swing registration form application. It includes code to: 1. Create a JFrame window and set its properties like title, size etc. 2. Add labels, text fields, radio buttons, combo boxes and other components to collect registration details like name, gender, date of birth etc. 3. Add an action listener to a submit button to retrieve the entered values and display them in an output text area when clicked. 4. Format the components using fonts and layouts and add them to the container. The code thus creates a simple GUI registration form to collect and display user details.

Uploaded by

Satishvaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views8 pages

Chegg 1

This document contains the code for a Java Swing registration form application. It includes code to: 1. Create a JFrame window and set its properties like title, size etc. 2. Add labels, text fields, radio buttons, combo boxes and other components to collect registration details like name, gender, date of birth etc. 3. Add an action listener to a submit button to retrieve the entered values and display them in an output text area when clicked. 4. Format the components using fonts and layouts and add them to the container. The code thus creates a simple GUI registration form to collect and display user details.

Uploaded by

Satishvaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Lable:

import [Link].*;

import [Link].*;

class RegForm{

public static void main(String args[])

// Step 1 : Creating a frame using JFrame class

JFrame frame=new JFrame("Registration Form Example"); // Setting title of the


JFrame

[Link](true); // Setting visibility of the JFrame

[Link](200,100,700,600 ); // Setting location and size of the JFrame

[Link](JFrame.EXIT_ON_CLOSE); // Setting default close


operation of JFrame

// Step 2 : setting background color of Frame.

Container c=[Link](); // Creating an object of the Container class

[Link](null); // Setting layout as null

[Link]([Link]); // Setting background color as Yellow

// step 3 : creating JLabel for Heading

Font f=new Font("Arial",[Link],20); // Creating object of the Font class.

JLabel heading_lbl=new JLabel();

heading_lbl.setBounds(250,5,200,40);

heading_lbl.setText("<html><font><u><b>Registration Form</b></u></html>");

// applying font on JLabel

heading_lbl.setFont(f);

// adding heading label to the container

[Link](heading_lbl);

Registration Form:

import [Link].*;

import [Link].*;
import [Link].*; // importing event package for event listener

class RegForm{

//Creating Static variables

static JTextField name_txt ;

static JTextField fname_txt;

static JRadioButton male;

static JRadioButton female;

static JComboBox day;

static JComboBox month;

static JComboBox year;

static JTextArea add_txtArea;

static JTextField phone_txt;

static JTextField email_txt;

static JCheckBox chkbox;

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("Registration Form Example");

[Link](true);

[Link](200,100,700,600 );

[Link](JFrame.EXIT_ON_CLOSE);

// Step 2 : setting background color of Frame.

Container c=[Link]();

[Link](null);

[Link]([Link]);

/*---------------------------------- Creating JLabel for Heading Text


------------------------------------------- */

Font f=new Font("Arial",[Link],20); // Creating font style and size for heading

// step 3 : creating JLabel for Heading

JLabel heading_lbl=new JLabel();

heading_lbl.setBounds(250,5,200,40);

heading_lbl.setText("<html><font><u><b>Registration Form</b></u></html>");

// applying font on heading Label

heading_lbl.setFont(f);

/* ----------------------------------- Creating Global Font style for all


components ------------------------------ */

Font f1=new Font("Arial",[Link],14);

/* ----------------------------------- Creating components for Registration details


---------------------------------- */

// Step 4 : Creating JLabel for Name

JLabel name_lbl=new JLabel("Name : ");

name_lbl.setBounds(50,80,100,30);

// Creating JTextField for Name

name_txt=new JTextField();

name_txt.setBounds(180,80,180,30);

// Step 5 : Creating JLabel for Father's Name

JLabel fname_lbl=new JLabel("Father's Name : ");

fname_lbl.setBounds(50,120,150,30);

// Creating JTextField for Father's name

fname_txt=new JTextField();

fname_txt.setBounds(180,120,180,30);

// Step 6 : Creating JLabel for Gender

JLabel gender_lbl=new JLabel("Gender : ");

gender_lbl.setBounds(50,160,150,30);

// Setting Cursor for components

Cursor cur=new Cursor(Cursor.HAND_CURSOR);

// Creating JRadioButton for the Male


male=new JRadioButton("Male");

[Link](180,160,70,30);

[Link]([Link]);

[Link](cur);

// Creating JRadioButton for the Female

female=new JRadioButton("Female");

[Link](280,160,80,30);

[Link]([Link]);

[Link](cur);

// Creating ButtonGroup for the JRadioButtons

ButtonGroup gender_grp=new ButtonGroup();

gender_grp.add(male); // adding male radio button in the ButtonGroup

gender_grp.add(female); // adding female radio button in the ButtonGroup

// Step 7 : Creating JLabel for Date of Birth

JLabel dob_lbl=new JLabel("Date of Birth : ");

dob_lbl.setBounds(50,200,100,30);

// Creating JComboBox for the day

String day_arr[]=new String[31];

for(int i=1;i<=31;i++)

day_arr[i-1]=[Link](i);

day=new JComboBox(day_arr);

[Link](180,200,40,30);

// Creating JComboBox for the month

String
month_arr[]={"Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","No
v","Dec" };

month=new JComboBox(month_arr);

[Link](230,200,60,30);

// Creating JComboBox for the year

String year_arr[]=new String[70];


for(int i=1951;i<=2020;i++)

year_arr[i-1951]=[Link](i);

year=new JComboBox(year_arr);

[Link](300,200,60,30);

// Step 8 : Creating JLabel for the Address

JLabel add_lbl=new JLabel("Address : ");

add_lbl.setBounds(50,240,100,30);

// Creating JTextArea for the address

add_txtArea= new JTextArea();

add_txtArea.setBounds(180,240,180,100);

// Step 9 : Creating JLabel for the phone

JLabel phone_lbl=new JLabel("Phone No. : ");

phone_lbl.setBounds(50,350,100,30);

// Creating JTextField for the phone

phone_txt=new JTextField();

phone_txt.setBounds(180,350,180,30);

// Step 10 : Creating JLabel for the Email

JLabel email_lbl=new JLabel("Email : ");

email_lbl.setBounds(50,390,100,30);

// Creating JTextField for the Email

email_txt=new JTextField();

email_txt.setBounds(180,390,180,30);

// Step 11 : Creating JCheckBox for the license agreement

chkbox=new JCheckBox("I accept the terms and conditions");

[Link](50,430,300,30);

[Link]([Link]);

// Step 12 : Creating JButton for submit the details

submit_btn=new JButton("Submit");

submit_btn.setBounds(180,500,120,40);

submit_btn.setCursor(cur); // Applying hand cursor on the button


// Step 18 : Adding ActionListener on submit button

submit_btn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

submit_action(event);

});

// Step 17 : Creating JTextArea for output

output_txtArea=new JTextArea();

output_txtArea.setBounds(380,80,260,320);

// Step 13 : Applying Global Font on all the JLabels

name_lbl.setFont(f1);

fname_lbl.setFont(f1);

gender_lbl.setFont(f1);

dob_lbl.setFont(f1);

add_lbl.setFont(f1);

phone_lbl.setFont(f1);

email_lbl.setFont(f1);

// Step 14 : Applying Font on all JTextFields, JRadioButtons, JComboBox and


JTextArea

name_txt.setFont(f1);

fname_txt.setFont(f1);

[Link](f1);

[Link](f1);

add_txtArea.setFont(f1);

phone_txt.setFont(f1);

email_txt.setFont(f1);

[Link](f1);

submit_btn.setFont(f1);

output_txtArea.setFont(f1);

// Step 15 : Adding label components to the container


[Link](heading_lbl);

[Link](name_lbl);

[Link](fname_lbl);

[Link](gender_lbl);

[Link](male);

[Link](female);

[Link](dob_lbl);

[Link](add_lbl);

[Link](phone_lbl);

[Link](email_lbl);

// Step 16 : Adding JTextField, JTextArea, JComboBox, JCheckBox, JRadioButton to


the container

[Link](name_txt);

[Link](name_txt);

[Link](fname_txt);

[Link](day);

[Link](month);

[Link](year);

[Link](add_txtArea);

[Link](phone_txt);

[Link](email_txt);

[Link](chkbox);

[Link](submit_btn);

[Link](output_txtArea);

// Step 19 : Reading value from the Registration Form

public static void submit_action(ActionEvent event){

if([Link]()==true)

String name=name_txt.getText();
String fname=fname_txt.getText();

String gender="Male";

if([Link]()==true)

gender="Female";

String day_name=(String)[Link]();

String month_name=(String)[Link]();

String year_name=(String)[Link]();

String add=add_txtArea.getText();

String phone=phone_txt.getText();

String email=email_txt.getText();

// displaying value in the JTextArea

output_txtArea.setText(" Name : " +name + "\n Father's Name : " +fname + "\n Gender
: "+gender +

"\n Date of Birth : "+day_name + " "+month_name + " " +year_name +

"\n Address : "+add + " \n Phone no : "+phone +

"\n Email : "+email + "\n ");

else

output_txtArea.setText("Please accept the terms and condition");

You might also like