0% found this document useful (0 votes)
57 views13 pages

38 Hardikparmar Exp13

The document describes an experiment to design a graphical user interface (GUI) using Java Swing components. It includes definitions of common Swing components like buttons, labels, checkboxes, lists, tables, and menus. The program section implements a registration form with these components, including text fields, radio buttons, and action listeners. When submitted, the form data is displayed in the output text area and a confirmation message is shown. The reset button clears all the form fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views13 pages

38 Hardikparmar Exp13

The document describes an experiment to design a graphical user interface (GUI) using Java Swing components. It includes definitions of common Swing components like buttons, labels, checkboxes, lists, tables, and menus. The program section implements a registration form with these components, including text fields, radio buttons, and action listeners. When submitted, the form data is displayed in the output text area and a confirmation message is shown. The reset button clears all the form fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Experiment No:13

Date of 02/12/21
Performance:

Date of 12/12/21
Submission:

Program formation/
Execution/ Documentatio Timely B Viva Experiment Teacher Signature
ethical n Submission answers Marks (15) with date
practices (07) (02) (03) (03)

Experiment No.13
Aim:- Study of Swing.
Lab Outcome:- 2.ITL304.5 Design and develop Graphical User Interface using
Abstract Window Toolkit and Swings along with response to the events.
Problem Statement:-
Write a Java program to implement Swing components namely Buttons,
JLabels, Checkboxes, Radio Buttons, JScrollPane, JList, JComboBox, Trees,
Tables Scroll pan Menus and Toolbars to design interactive GUI.
Theory:-
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to
create window-based applications. It is built on the top of AWT (Abstract
Windowing Toolkit) API and entirely written in java.
1. JButton: JButton class is used to create a push-button on the UI. The
button can contain some display text or image. It generates an event when
clicked and double-clicked. A JButton can be implemented in the application by
calling one of its constructors.
2. JLabel: JLabel class is used to render a read-only text label or images on
the UI. It does not generate any event.
3. JTextField: JTextField renders an editable single-line text box. A user can
input non-formatted text in the box. To initialize the text field, call its
constructor and pass an optional integer parameter to it.
4. JCheckBox: JCheckBox renders a check-box with a label. The check-box
has two states – on/off. When selected, the state is on and a small tick is
displayed in the box.
5. JRadioButton: JRadioButton is used to render a group of radio buttons in
the UI. A user can select one choice from the group.
6. JList: JList component renders a scrollable list of elements. A user can
select a value or multiple values from the list. This select behavior is defined in
the code by the developer.
7. JComboBox: JComboBox class is used to render a dropdown of the list of
options.
8. JScrollPane: Creates an empty scroll pane (no viewPort). It can have
both vertical and horizontal scrollbars when needed.
9. JTable: In Java, Swing toolkit contains a JTable Class. It is under package
javax.swing.JTable class. It used to draw a table to display data.
10. JFrame: JFrame descends from the Frame class. The components added
to the Frame are called contents of the Frame.
11. JTree: The JTree class is used to display the tree structured data or
hierarchical data. JTree is a complex component. It has a 'root node' at the top
most which is a parent for all nodes in the tree. It inherits JComponent class.
12. JToolBar: JToolBar container allows us to group other components,
usually buttons with icons in a row or column. JToolBar provides a component
which is useful for displaying commonly used actions or controls.
13. Program:- import javax.swing.*; import java.awt.*; import
java.awt.event.*;

class MyFrame extends


JFrame implements
ActionListener {

// Components of the Form


private Container c; private
JLabel title; private JLabel
name; private JTextField
tname; private JLabel mno;
private JTextField tmno;
private JLabel gender;
private JRadioButton male;
private JRadioButton female;
private ButtonGroup gengp;
private JLabel dob; private
JComboBox date; private
JComboBox month; private
JComboBox year; private
JLabel add; private
JTextArea tadd; private
JCheckBox term; private
JButton sub; private
JButton reset; private
JTextArea tout; private
JLabel res; private
JTextArea resadd;

private String dates[]


= { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30",
"31" };
private String months[]
= { "Jan", "feb", "Mar", "Apr",
"May", "Jun", "July", "Aug",
"Sup", "Oct", "Nov", "Dec" }; private
String years[]
= { "1995", "1996", "1997", "1998",
"1999", "2000", "2001", "2002",
"2003", "2004", "2005", "2006",
"2007", "2008", "2009", "2010",
"2011", "2012", "2013", "2014", "2015",
"2016", "2017", "2018",
"2019" };
public MyFrame()
{
setTitle("Registration Form");
setBounds(300, 90, 900, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);

c = getContentPane();
c.setLayout(null);

title = new JLabel("Registration Form");


title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setSize(300, 30); title.setLocation(300, 30);
c.add(title);

name = new JLabel("Name");


name.setFont(new Font("Arial", Font.PLAIN, 20));
name.setSize(100, 20); name.setLocation(100,
100);
c.add(name);

tname = new JTextField();


tname.setFont(new Font("Arial", Font.PLAIN, 15));
tname.setSize(190, 20); tname.setLocation(200,
100);
c.add(tname);
mno = new JLabel("Mobile");
mno.setFont(new Font("Arial", Font.PLAIN, 20));
mno.setSize(100, 20); mno.setLocation(100,
150);
c.add(mno);

tmno = new JTextField();


tmno.setFont(new Font("Arial", Font.PLAIN, 15));
tmno.setSize(150, 20); tmno.setLocation(200,
150);
c.add(tmno);

gender = new JLabel("Gender");


gender.setFont(new Font("Arial", Font.PLAIN, 20));
gender.setSize(100, 20); gender.setLocation(100,
200);
c.add(gender);

male = new JRadioButton("Male");


male.setFont(new Font("Arial", Font.PLAIN, 15));
male.setSelected(true); male.setSize(75, 20);
male.setLocation(200, 200);
c.add(male);
female = new JRadioButton("Female");
female.setFont(new Font("Arial", Font.PLAIN, 15));
female.setSelected(false); female.setSize(80, 20);
female.setLocation(275, 200);
c.add(female);

gengp = new ButtonGroup();


gengp.add(male); gengp.add(female);

dob = new JLabel("DOB");


dob.setFont(new Font("Arial", Font.PLAIN, 20)); dob.setSize(100,
20);

dob.setLocation(100, 250);
c.add(dob);

date = new JComboBox(dates);


date.setFont(new Font("Arial", Font.PLAIN, 15));
date.setSize(50, 20); date.setLocation(200,
250);
c.add(date);

month = new JComboBox(months);


month.setFont(new Font("Arial", Font.PLAIN, 15));
month.setSize(60, 20); month.setLocation(250,
250);
c.add(month);
year = new JComboBox(years);
year.setFont(new Font("Arial", Font.PLAIN, 15));
year.setSize(60, 20); year.setLocation(320, 250);
c.add(year);

add = new JLabel("Address");


add.setFont(new Font("Arial", Font.PLAIN, 20));
add.setSize(100, 20); add.setLocation(100, 300);
c.add(add);

tadd = new JTextArea();


tadd.setFont(new Font("Arial", Font.PLAIN, 15));
tadd.setSize(200, 75); tadd.setLocation(200,
300); tadd.setLineWrap(true);
c.add(tadd);

term = new JCheckBox("Accept Terms And Conditions.");


term.setFont(new Font("Arial", Font.PLAIN, 15));
term.setSize(250, 20); term.setLocation(150, 400);
c.add(term);

sub = new JButton("Submit");


sub.setFont(new Font("Arial", Font.PLAIN, 15));
sub.setSize(100, 20); sub.setLocation(150,
450); sub.addActionListener(this);
c.add(sub);

reset = new JButton("Reset");


reset.setFont(new Font("Arial", Font.PLAIN, 15));
reset.setSize(100, 20); reset.setLocation(270,
450); reset.addActionListener(this);

c.add(reset);

tout = new JTextArea();


tout.setFont(new Font("Arial", Font.PLAIN, 15));
tout.setSize(300, 400); tout.setLocation(500,
100); tout.setLineWrap(true);
tout.setEditable(false);
c.add(tout);

res = new JLabel("");


res.setFont(new Font("Arial", Font.PLAIN, 20));
res.setSize(500, 25); res.setLocation(100, 500);
c.add(res);

resadd = new JTextArea();


resadd.setFont(new Font("Arial", Font.PLAIN, 15));
resadd.setSize(200, 75); resadd.setLocation(580,
175); resadd.setLineWrap(true);
c.add(resadd);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sub) {
if (term.isSelected()) {
String data1;
String data
= "Name : "
+ tname.getText() + "\n"
+ "Mobile : "
+ tmno.getText() + "\n";
if (male.isSelected())
data1 = "Gender : Male"
+ "\n";
else
data1 = "Gender : Female"
+ "\n";
String data2
= "DOB : "
+ (String)date.getSelectedItem()
+ "/" + (String)month.getSelectedItem()
+ "/" + (String)year.getSelectedItem()
+ "\n";
String data3 = "Address : " + tadd.getText();
tout.setText(data + data1 + data2 + data3);
tout.setEditable(false);
res.setText("Registration Successfully..");
} else
{ tout.setText("");
resadd.setText("");
res.setText("Please accept the"
+ " terms & conditions..");
}
}
else if (e.getSource() == reset) {
String def = "";
tname.setText(def);
tadd.setText(def);
tmno.setText(def);
res.setText(def); tout.setText(def);
term.setSelected(false);
date.setSelectedIndex(0);
month.setSelectedIndex(0);
year.setSelectedIndex(0);
resadd.setText(def);
}
}
}
class Registration {
public static void main(String[] args) throws Exception
{
MyFrame f = new MyFrame();
}
}
Output:-

Conclusion:-
It is the framework that is used for building windows based applications for
Java. It was developed to solve the issues that are in AWT. It provides more
components to work and uses extensible components to develop the
applications. There are many components in the swing package or library to
perform and define the look and feel of the project or application.

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