Tuesday, September 26, 2023

BCA and B.Sc Programs - Programming in Java Lab - Bharathiar University - Practical Program 8 - Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address

 

Bharathiar University

Programming Java Lab
Java Program 8
Write a Java Program to create a frame with three text fields for name, age and qualification and a text field for multiple line for address
 



Source Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserForm {
    public static void main(String[] args) {
        // Create the main frame
        JFrame frame = new JFrame("User Profile Form");
        frame.setSize(320, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT,20,25));

        // Create labels and text fields
        JLabel nameLabel = new JLabel("Name:");
        JTextField name = new JTextField(20);

        JLabel ageLabel = new JLabel("Age:");
        JTextField age = new JTextField(20);

        JLabel qualificationLabel = new JLabel("Qualification:");
        JTextField qualification = new JTextField(20);

        JLabel addressLabel = new JLabel("Address:");
        JTextArea address = new JTextArea(5, 20);

        // Create a button to trigger the action
        JButton submit = new JButton("Submit");

        // Add components to the frame
        frame.add(nameLabel);
        frame.add(name);
        frame.add(ageLabel);
        frame.add(age);
        frame.add(qualificationLabel);
        frame.add(qualification);
        frame.add(addressLabel);
        frame.add(new JScrollPane(address));
        frame.add(submit);

        // Create ActionListener for the button
        submit.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e) {
                
                // Display all values in a dialog box
                String message = "Name: " + name.getText() + "\nAge: " + age.getText() + "\nQualification: " + qualification.getText() + "\nAddress:\n" + address.getText();
                JOptionPane.showMessageDialog(frame, message);
            }
        });

        // Set frame visibility
        frame.setVisible(true);
    }
}

OUTPUT






Dialog box

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...