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

JPR Manual Answers Experiment 14

The document contains multiple Java program statements demonstrating the creation of GUI applications using Swing components. It includes examples of using radio buttons, checkboxes, text fields, buttons, lists, and item listeners to create interactive user interfaces. Each program is accompanied by source code and showcases different functionalities such as form submission and item selection.

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)
6 views6 pages

JPR Manual Answers Experiment 14

The document contains multiple Java program statements demonstrating the creation of GUI applications using Swing components. It includes examples of using radio buttons, checkboxes, text fields, buttons, lists, and item listeners to create interactive user interfaces. Each program is accompanied by source code and showcases different functionalities such as form submission and item selection.

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/ 6

Practical No.

: 14

Program Statement 1: Design an applet/application to demonstrate the use of Radio Button


and Checkbox.

Source Code:

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

public class SimpleDemo extends JFrame implements ActionListener {


JLabel label1;
JRadioButton radio;
JCheckBox check;
JButton button;
JLabel label;

public SimpleDemo() {
label1 = new JLabel("Demonstration of Radio Button and Checkbox");
radio = new JRadioButton("Option");
check = new JCheckBox("Check");
button = new JButton("Go");
label = new JLabel("");

button.addActionListener(this);
add(label1);
add(radio);
add(check);
add(button);
add(label);

setLayout(new java.awt.FlowLayout());
setSize(200, 150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {


String r = radio.isSelected() ? "Selected" : "Not Selected";
String c = check.isSelected() ? "Checked" : "Unchecked";
label.setText("Radio: " + r + ", Check: " + c);
}

public static void main(String[] args) {


new SimpleDemo();
}
}
Output:

Program Statement 2: Design an applet/application to create form using Text Field, Text
Area, Button and Label.

Source Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleForm extends JFrame implements ActionListener {

JTextField textField;
JTextArea textArea;
JButton button;
JLabel label;

public SimpleForm() {
textField = new JTextField(20);
textArea = new JTextArea(5, 20);
button = new JButton("Submit");
label = new JLabel("");

button.addActionListener(this);

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
add(new JLabel("Name:"), gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
add(textField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0.0;
add(new JLabel("Address:"), gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
add(new JScrollPane(textArea), gbc);

gbc.gridx = 1;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER; // Center the button
gbc.weightx = 0.0;
gbc.weighty = 0.0;
add(button, gbc);

setSize(300, 250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {


String name = textField.getText();
String address = textArea.getText();
label.setText("Name: " + name + ", Address: " + address);
}

public static void main(String[] args) {


new SimpleForm();
}
}

Output:
Program Statement 3: Write a program to create three Buttons with captions OK,RESET
and CANCEL.

Source Code:
import javax.swing.*;
import java.awt.*;

public class ButtonDemo extends JFrame {

public ButtonDemo() {
setTitle("Button Demo");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

JButton okButton = new JButton("OK");


JButton resetButton = new JButton("RESET");
JButton cancelButton = new JButton("CANCEL");

add(okButton);
add(resetButton);
add(cancelButton);

setVisible(true);
}

public static void main(String[] args) {


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

Output:

Program Statement 4: Develop an applet/application using List components to add names


of 10 different cities.

Source Code:
import java.awt.*;
import javax.swing.*;
public class CityList extends JFrame {
public CityList() {
setTitle("City List");
setSize(200, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
String[] cities = {
"Sangli", "Mumbai", "Pune", "Delhi", "Bangalore",
"Chennai", "Hyderabad", "Kolkata", "Ahmedabad", "Jaipur"
};

List cityList = new List(5, false);


for (String city : cities) {
cityList.add(city);
}

add(cityList, BorderLayout.CENTER);
setVisible(true);
}

public static void main(String[] args) {


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

Output:

Program Statement 5: Develop applet/application to select multiple names of news papers.

Source Code:

import java.awt.*;
import java.awt.event.*;

public class NewspaperSelector extends Frame implements ItemListener {

private List newspaperList;


private Label resultLabel;
public NewspaperSelector() {
setTitle("Newspaper Selector");
setSize(300, 200);
setLayout(new FlowLayout());

newspaperList = new List(5, true);


newspaperList.add("The Times of India");
newspaperList.add("The Hindu");
newspaperList.add("Hindustan Times");
newspaperList.add("The Indian Express");
newspaperList.add("Deccan Chronicle");
newspaperList.add("The Telegraph");
newspaperList.add("The Tribune");
newspaperList.add("The Pioneer");
newspaperList.add("The Statesman");
newspaperList.add("Business Standard");

newspaperList.addItemListener(this);
add(newspaperList);

resultLabel = new Label("");


add(resultLabel);

setVisible(true);
}

public void itemStateChanged(ItemEvent e) {


String selectedNewspapers = "Selected Newspapers: ";
for (String newspaper : newspaperList.getSelectedItems()) {
selectedNewspapers += newspaper + ", ";
}
resultLabel.setText(selectedNewspapers);
}

public static void main(String[] args) {


new NewspaperSelector();
}
}

Output:

You might also like