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

Java 5 Prac

The document shows code for a Java Swing application that creates a form with fields for name, gender, and marital status. It uses labels, text fields, radio buttons, and check boxes to collect the information, and a submit button to output the results.

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)
10 views3 pages

Java 5 Prac

The document shows code for a Java Swing application that creates a form with fields for name, gender, and marital status. It uses labels, text fields, radio buttons, and check boxes to collect the information, and a submit button to output the results.

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

String name = nameField.

getText();
Program :
String gender = maleRadio.isSelected() ?
import java.awt.*; "Male" : "Female";
import java.awt.event.ActionEvent; String maritalStatus =
singleCheckBox.isSelected() ? "Single" : "Married";
import java.awt.event.ActionListener;

import javax.swing.*;
System.out.println("Name: " + name);

System.out.println("Gender: " + gender);


public class SwingFormExample {
System.out.println("Marital Status: " +
public static void main(String[] args) {
maritalStatus);
JFrame frame = new JFrame("Swing Form
}
Example");
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
frame.add(nameLabel);
frame.setSize(500, 600);
frame.add(nameField);
frame.setLayout(new GridLayout(4, 2));
frame.add(genderLabel);

frame.add(maleRadio);
JLabel nameLabel = new JLabel("Name:");
frame.add(new JLabel()); // Empty label for
JTextField nameField = new JTextField(); alignment

frame.add(femaleRadio);
JLabel genderLabel = new JLabel("Gender:"); frame.add(statusLabel);
JRadioButton maleRadio = new frame.add(singleCheckBox);
JRadioButton("Male");
frame.add(new JLabel()); // Empty label for
JRadioButton femaleRadio = new alignment
JRadioButton("Female");
frame.add(marriedCheckBox);
ButtonGroup genderGroup = new
ButtonGroup(); frame.add(new JLabel()); // Empty label for
alignment
genderGroup.add(maleRadio);
frame.add(submitButton);
genderGroup.add(femaleRadio);

frame.setVisible(true);
JLabel statusLabel = new JLabel("Marital
Status:"); }

JCheckBox singleCheckBox = new }


JCheckBox("Single");

JCheckBox marriedCheckBox = new


JCheckBox("Married");

JButton submitButton = new


JButton("Submit");

submitButton.addActionListener(new
ActionListener() {

public void actionPerformed(ActionEvent e) {

Practical 5
Output :
Conclusion :
In this Practical we understand that how to design a form
using basic swing components.

K S P A T

You might also like