0% found this document useful (0 votes)
7 views3 pages

Java Unit4 Important Questions Answers

The document covers key concepts related to Java Applets, Event Handling, and AWT, including the applet lifecycle, parameter passing, and the Delegation Event Model. It provides example programs for displaying strings using applets, handling button clicks with ActionListener, creating frames, and using menus in AWT. Additionally, it discusses various AWT controls and layout managers.

Uploaded by

joshihitesh1104
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)
7 views3 pages

Java Unit4 Important Questions Answers

The document covers key concepts related to Java Applets, Event Handling, and AWT, including the applet lifecycle, parameter passing, and the Delegation Event Model. It provides example programs for displaying strings using applets, handling button clicks with ActionListener, creating frames, and using menus in AWT. Additionally, it discusses various AWT controls and layout managers.

Uploaded by

joshihitesh1104
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

Java: Applets, Event Handling and AWT (Unit 4)

Answers to Important Questions - Unit 4

1. Explain the lifecycle of an Applet.


- The applet lifecycle consists of the following methods:
* init(): Called once when the applet is first loaded.
* start(): Called every time the applet is started.
* stop(): Called when the browser moves away from the applet.
* destroy(): Called when the applet is removed from memory.

2. Write a program to display string using Applet.


import java.applet.*;
import java.awt.*;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello from Applet", 50, 50);
}
}
// HTML: <applet code="HelloApplet.class" width=300 height=300></applet>

3. How to pass parameters to an Applet? Write example.


HTML:
<applet code="ParamApplet.class" width=300 height=300>
<param name="msg" value="Hello Param">
</applet>
Java:
public class ParamApplet extends Applet {
public void paint(Graphics g) {
String msg = getParameter("msg");
g.drawString(msg, 50, 50);
}
}

4. Explain the Delegation Event Model.


- It is the model used for event handling in Java.
- Event source generates an event and sends it to listeners.
- Listeners implement specific interfaces to handle events.

5. Differentiate between Event Class and Listener Interface.


- Event Class: Encapsulates information about events (e.g., ActionEvent).
- Listener Interface: Defines methods that respond to events (e.g., ActionListener).
Java: Applets, Event Handling and AWT (Unit 4)

6. Write a program to handle button click using ActionListener.


import java.awt.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
Frame f = new Frame("Button Example");
Button b = new Button("Click Me");
b.setBounds(50, 100, 80, 30);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked!");
}
});
f.add(b);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
}

7. Explain different AWT controls and layout managers with examples.


Controls: Button, Label, TextField, Checkbox, List etc.
Layout Managers:
- FlowLayout: Left to right.
- BorderLayout: North, South, East, West, Center.
- GridLayout: Grid of cells.

8. Write a program to create a Frame and add components using AWT.


import java.awt.*;
public class AWTFrameExample {
public static void main(String[] args) {
Frame f = new Frame("AWT Frame");
Label l = new Label("Name:");
l.setBounds(20, 80, 80, 30);
TextField tf = new TextField();
tf.setBounds(100, 80, 100, 30);
f.add(l); f.add(tf);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
Java: Applets, Event Handling and AWT (Unit 4)

}
}

9. What are Menus in AWT? Explain with code.


- Menus are UI components to group commands.
Example:
Frame f = new Frame("Menu Example");
MenuBar mb = new MenuBar();
Menu m = new Menu("File");
MenuItem mi1 = new MenuItem("Open");
MenuItem mi2 = new MenuItem("Save");
m.add(mi1); m.add(mi2);
mb.add(m);
f.setMenuBar(mb);
f.setSize(300, 300);
f.setVisible(true);

You might also like