0% found this document useful (0 votes)
9 views2 pages

JPR Manual Answers Experiment 21

The document provides a Java program that creates a simple graphical user interface (GUI) form using Swing. It includes fields for entering a name and comments, along with a submit button. The layout is managed using GridBagLayout for proper alignment and spacing of components.

Uploaded by

Riya Patil
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)
9 views2 pages

JPR Manual Answers Experiment 21

The document provides a Java program that creates a simple graphical user interface (GUI) form using Swing. It includes fields for entering a name and comments, along with a submit button. The layout is managed using GridBagLayout for proper alignment and spacing of components.

Uploaded by

Riya Patil
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/ 2

Practical No.

: 21

Program Statement 1: Write Java Program to display following output:

Source Code:

import javax.swing.*;
import java.awt.*;

public class FormExample extends JFrame {

public FormExample() {
setTitle("Form Example");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();


gbc.insets = new Insets(5, 5, 5, 5);

JLabel nameLabel = new JLabel("Name");


JTextField nameTextField = new JTextField(20);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(nameLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(nameTextField, gbc);

JLabel commentsLabel = new JLabel("Comments");


JTextArea commentsTextArea = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(commentsTextArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALW
AYS);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR
_NEVER);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
add(commentsLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(scrollPane, gbc);

JButton submitButton = new JButton("Submit");


submitButton.setBackground(Color.LIGHT_GRAY);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.anchor = GridBagConstraints.WEST;
add(submitButton, gbc);

setVisible(true);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new FormExample());
}
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy