Applet – Program-Based Questions
1. Write an applet program to display "Hello Applet" on the screen.
2. Write an applet program to draw a rectangle, oval, and line on the screen.
3. Create an applet that changes the background color when a button is clicked.
4. Write an applet program to display your name in different fonts and colors.
5. Create an applet that takes input from a TextField and displays it when a button is clicked.
Applet – Program Answers
1. Display "Hello Applet"
import [Link];
import [Link];
public class HelloApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello Applet", 50, 50);
}
}
2. Draw rectangle, oval, and line
import [Link];
import [Link];
public class DrawShapes extends Applet {
public void paint(Graphics g) {
[Link](20, 20, 100, 50); // rectangle
[Link](150, 20, 100, 50); // oval
[Link](20, 100, 200, 150); // line
}
}
3. Change background color on button click
import [Link];
import [Link].*;
import [Link].*;
public class ColorApplet extends Applet implements ActionListener {
Button b;
public void init() {
b = new Button("Change Color");
add(b);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
setBackground([Link]);
repaint();
}
}
4. Display name in different fonts/colors
import [Link];
import [Link].*;
public class NameApplet extends Applet {
public void paint(Graphics g) {
[Link]([Link]);
[Link](new Font("Arial", [Link], 20));
[Link]("Your Name", 50, 50);
[Link]([Link]);
[Link](new Font("TimesRoman", [Link], 25));
[Link]("Your Name", 50, 100);
}
}
5. TextField input display
import [Link];
import [Link].*;
import [Link].*;
public class InputApplet extends Applet implements ActionListener {
TextField tf;
Button b;
String msg = "";
public void init() {
tf = new TextField(20);
b = new Button("Display");
add(tf); add(b);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
msg = "You entered: " + [Link]();
repaint();
}
public void paint(Graphics g) {
[Link](msg, 50, 100);
}
}
🔹 AWT – Program-Based Questions
1. Write a program in AWT to create a Frame with a Button.
2. Create a simple calculator using AWT with buttons for addition, subtraction, multiplication,
and division.
3. Write a program to create a login form using AWT (Labels, TextFields, Button).
4. Write an AWT program to handle mouse events (display coordinates when mouse is clicked).
5. Create a program using AWT Layout Managers: FlowLayout, BorderLayout, or GridLayout.
AWT – Program Answers
1. Frame with Button
import [Link].*;
public class AWTButtonExample {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Button b = new Button("Click Me");
[Link](50, 100, 80, 30);
[Link](b);
[Link](300, 300);
[Link](null);
[Link](true);
2. Simple Calculator (Addition)
import [Link].*;
import [Link].*;
public class SimpleCalc extends Frame implements ActionListener {
TextField tf1, tf2, tf3;
Button b;
SimpleCalc() {
tf1 = new TextField();
tf2 = new TextField();
tf3 = new TextField();
b = new Button("Add");
[Link](50,50,100,30);
[Link](50,100,100,30);
[Link](50,150,50,30);
[Link](50,200,100,30);
add(tf1); add(tf2); add(b); add(tf3);
[Link](this);
setSize(300,300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e) {
int a = [Link]([Link]());
int b1 = [Link]([Link]());
[Link]([Link](a+b1));
public static void main(String[] args) {
new SimpleCalc();
3. Login Form
import [Link].*;
import [Link].*;
public class LoginForm extends Frame implements ActionListener {
TextField user, pass;
Button login;
LoginForm() {
Label u = new Label("Username:"); [Link](50,50,80,30);
Label p = new Label("Password:"); [Link](50,100,80,30);
user = new TextField(); [Link](150,50,100,30);
pass = new TextField(); [Link](150,100,100,30); [Link]('*');
login = new Button("Login"); [Link](150,150,80,30);
add(u); add(p); add(user); add(pass); add(login);
[Link](this);
setSize(400,400); setLayout(null); setVisible(true);
public void actionPerformed(ActionEvent e) {
[Link]("Username: " + [Link]());
[Link]("Password: " + [Link]());
public static void main(String[] args) {
new LoginForm();
}
🔹 Swing – Program-Based Questions
1. Write a program to create a JFrame with a JButton. When the button is clicked, display
"Button Clicked".
2. Create a JFrame with JTextField and JButton; when the button is pressed, display the text
entered.
3. Write a program to create a JTable with sample data (like name, age, department).
4. Create a JTabbedPane with at least 3 tabs, each containing some components.
5. Write a Swing program using JPanel and JScrollPane to display a large text area with
scrollbars.
Swing – Program Answers
1. JFrame with JButton
import [Link].*;
import [Link].*;
public class SwingButtonExample {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click Me");
[Link](50,100,95,30);
[Link](b);
[Link](e -> [Link](f,"Button Clicked"));
[Link](300,300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
2. JTextField + JButton Display
import [Link].*;
import [Link].*;
public class TextFieldExample {
public static void main(String[] args) {
JFrame f = new JFrame("Input Example");
JTextField tf = new JTextField();
[Link](50,50,150,20);
JButton b = new JButton("Show");
[Link](50,100,80,30);
JLabel l = new JLabel();
[Link](50,150,200,30);
[Link](tf); [Link](b); [Link](l);
[Link](e -> [Link]("You entered: "+[Link]()));
[Link](400,400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
3. JTable Example
import [Link].*;
public class JTableExample {
public static void main(String[] args) {
JFrame f = new JFrame();
String data[][] = {{"101","Alice"}, {"102","Bob"}, {"103","Charlie"}};
String column[] = {"ID","Name"};
JTable t = new JTable(data,column);
JScrollPane sp = new JScrollPane(t);
[Link](sp);
[Link](300,200);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
4. JTabbedPane Example
import [Link].*;
public class TabExample {
public static void main(String[] args) {
JFrame f = new JFrame("JTabbedPane Example");
JTabbedPane tp = new JTabbedPane();
JPanel p1 = new JPanel(); [Link](new JLabel("Tab 1 Content"));
JPanel p2 = new JPanel(); [Link](new JLabel("Tab 2 Content"));
JPanel p3 = new JPanel(); [Link](new JLabel("Tab 3 Content"));
[Link]("Tab 1",p1);
[Link]("Tab 2",p2);
[Link]("Tab 3",p3);
[Link](tp);
[Link](400,400);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
5. JTextArea with JScrollPane
import [Link].*;
public class TextAreaScroll {
public static void main(String[] args) {
JFrame f = new JFrame("TextArea Example");
JTextArea ta = new JTextArea(20,20);
JScrollPane sp = new JScrollPane(ta);
[Link](sp);
[Link](300,300);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);