JPR Manual Answers Experiment 14
JPR Manual Answers Experiment 14
: 14
Source Code:
import javax.swing.*;
import java.awt.event.*;
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);
}
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.*;
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);
}
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 ButtonDemo() {
setTitle("Button Demo");
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
add(okButton);
add(resetButton);
add(cancelButton);
setVisible(true);
}
Output:
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"
};
add(cityList, BorderLayout.CENTER);
setVisible(true);
}
Output:
Source Code:
import java.awt.*;
import java.awt.event.*;
newspaperList.addItemListener(this);
add(newspaperList);
setVisible(true);
}
Output: