0% found this document useful (0 votes)
6 views3 pages

Java 2 Prac

The document describes a Java program that creates a form with fields for name, gender, and marital status. It uses labels, text fields, checkboxes, and buttons. When the submit button is clicked, it prints the entered form values.

Uploaded by

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

Java 2 Prac

The document describes a Java program that creates a form with fields for name, gender, and marital status. It uses labels, text fields, checkboxes, and buttons. When the submit button is clicked, it prints the entered form values.

Uploaded by

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

marriedCheckbox = new Checkbox("Married",

Program : statusGroup, false);


import java.awt.*;

import java.awt.event.*; submitButton = new Button("Submit");

submitButton.addActionListener(this);
public class FormExample extends Frame implements
ActionListener, ItemListener {
add(nameLabel);

add(nameField);
private TextField nameField;
add(genderLabel);
private Choice genderChoice;
add(genderChoice);
private CheckboxGroup statusGroup;
add(statusLabel);
private Checkbox singleCheckbox,
marriedCheckbox; add(singleCheckbox);

private Button submitButton; add(new Label());

add(marriedCheckbox);

public FormExample() { add(new Label());

setTitle("Form Example"); add(submitButton);

setSize(500, 500); }

setLayout(new GridLayout(5, 2));

addWindowListener(new WindowAdapter() { public void actionPerformed(ActionEvent e) {

public void windowClosing(WindowEvent e) {

System.exit(0); if (e.getSource() == submitButton) {

}); String name = nameField.getText();

String gender =
genderChoice.getSelectedItem();
Label nameLabel = new Label("Name:");
String status = singleCheckbox.getState() ?
nameField = new TextField(20); "Single" : "Married";

Label genderLabel = new Label("Gender:"); System.out.println("Name: " + name);


genderChoice = new Choice(); System.out.println("Gender: " + gender);
genderChoice.add("Male"); System.out.println("Marital Status: " +
status);
genderChoice.add("Female");
}
genderChoice.addItemListener(this);
}

Label statusLabel = new Label("Marital


Status:"); public void itemStateChanged(ItemEvent e) {
statusGroup = new CheckboxGroup();

singleCheckbox = new Checkbox("Single",


statusGroup, true);

Practical 2
String selectedGender =
genderChoice.getSelectedItem();

System.out.println("Selected Gender: " +


selectedGender);

public static void main(String[] args) {

FormExample form = new FormExample();

form.setVisible(true);

Practical 2
Output :

Conclusion :
In this Practical we learnt that how to design a form and
handle various events related to each control.
K S P A T

You might also like