Java Applet Programs
Q.1. Design an applet to demonstrate the use of radio button and checkbox.
Below is the Java program demonstrating radio buttons and checkboxes:
import javax.swing.*;
public class SmallDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Demo");
JCheckBox cb1 = new JCheckBox("Option 1");
JCheckBox cb2 = new JCheckBox("Option 2");
JRadioButton rb1 = new JRadioButton("Radio 1");
JRadioButton rb2 = new JRadioButton("Radio 2");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1); bg.add(rb2);
JLabel label = new JLabel("Select options");
cb1.addActionListener(e -> label.setText(cb1.isSelected() ? "Option 1" : "Unchecked"));
rb1.addActionListener(e -> label.setText(rb1.isSelected() ? "Radio 1" : "Radio 2"));
frame.add(cb1); frame.add(cb2); frame.add(rb1); frame.add(rb2); frame.add(label);
frame.setLayout(new java.awt.FlowLayout());
frame.setSize(200, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
Q.2. Design an applet to create form using Text field, Text area, Button, and Label.
Below is a simple Java applet for creating a form:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class SmallFormApplet extends Applet implements ActionListener {
TextField nameField;
Button submitButton;
Label output;
public void init() {
add(new Label("Name:"));
nameField = new TextField(15);
add(nameField);
submitButton = new Button("Submit");
add(submitButton);
output = new Label("");
add(output);
submitButton.addActionListener(this);
public void actionPerformed(ActionEvent e) {
output.setText("Hello, " + nameField.getText());
/*
<applet code="SmallFormApplet" width=300 height=150>
</applet>
*/