JAVA Unit 5 Question Bank Answersd

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

UNIT–V

GUI PROGRAMMING, EVENT HANLDING, APPLETS

Part – A (Short Answer Questions)

1. What are the limitations of AWT?

The AWT (Abstract Window Toolkit) is a set of Java classes that provide a basic
user interface (UI) for Java applications. However, the AWT has a number of
limitations, including:

 It is not very customizable.

 It is not very platform-independent.

 It is not very efficient.

2. Why do applet classes need to be declared as public?

Applet classes need to be declared as public because they are intended to be


embedded in web pages. Web pages are public documents, so applet classes must
also be public.

3. What are the various classes used in creating a swing menu?

The following classes are used in creating a swing menu:

 JMenuBar: This class represents the menu bar.

 JMenu: This class represents a menu.

 JMenuItem: This class represents a menu item.

 JSeparator: This class represents a separator between menu items.

4. What are the differences between JToggle buttion and Radio buttion?

The main difference between JToggleButton and RadioButton is that JToggleButton


can be either on or off, while RadioButton can only be on or off. This is because
JToggleButton is a toggle button, while RadioButton is a radio button.
5. What is Swing in Java? How it differs from Applet.

Swing is a graphical user interface (GUI) toolkit for Java. It is a more advanced GUI
toolkit than AWT, and it provides a number of features that are not available in AWT.
For example, Swing is more customizable, more platform-independent, and more
efficient than AWT.

Applets are small, self-contained Java programs that are embedded in web pages.
They are typically used to provide interactive content on web pages. Applets are not
as powerful as standalone Java applications, but they are a convenient way to add
interactivity to web pages.

6. How do applets differ from application program?

Applets and application programs are both Java programs, but they have a number
of differences. Applets are embedded in web pages, while application programs are
standalone programs. Applets are subject to the security restrictions of the web
browser, while application programs are not. Applets are typically smaller and
simpler than application programs.

7. What is Delegation event model?

The delegation event model is a design pattern that is used in Swing to handle
events. In the delegation event model, the event source (the object that generates
the event) delegates the handling of the event to an event listener. The event listener
is responsible for processing the event.

8. What are the sources for item event?

The sources for item events are JComboBox, JList, and JMenu. Item events are
generated when the user selects an item in a JComboBox, JList, or JMenu.

9. Give the hierarchy for swing components

The hierarchy for swing components is as follows:

 JComponent

o Container
 JFrame

 JDialog

 JApplet

o JPanel

 JButton

 JLabel

 JTextField

 JTextArea

 JList

 JComboBox

 JMenuBar

 JMenu

 JMenuItem

10. Why swing components are preferred over AWT components?

Swing components are preferred over AWT components for a number of reasons,
including:

 They are more customizable.

 They are more platform-independent.

 They are more efficient.

 They are easier to use.


Part – B (Long Answer Questions)

11 a) Explain the handling of mouse and keyboards events.

Sure, here is an explanation of the handling of mouse and keyboard events in Java:

Mouse events

Mouse events are generated when the user interacts with a mouse. The following
are some of the most common mouse events:

 MouseEvent.MOUSE_PRESSED: This event is generated when the user


presses the mouse button.
 MouseEvent.MOUSE_RELEASED: This event is generated when the user
releases the mouse button.
 MouseEvent.MOUSE_MOVED: This event is generated when the user moves
the mouse cursor.
 MouseEvent.MOUSE_ENTERED: This event is generated when the mouse
cursor enters a component.
 MouseEvent.MOUSE_EXITED: This event is generated when the mouse
cursor exits a component.
Keyboard events

Keyboard events are generated when the user interacts with the keyboard. The
following are some of the most common keyboard events:

 KeyEvent.KEY_PRESSED: This event is generated when the user presses a


key.
 KeyEvent.KEY_RELEASED: This event is generated when the user releases
a key.
 KeyEvent.KEY_TYPED: This event is generated when the user types a
character.
Handling mouse and keyboard events

To handle mouse and keyboard events, you need to register a listener with the
component that you want to listen to events for. The listener will then be notified
when an event is generated.
b) Discuss about the JButton, JCheck Box and JTabbedPane?

The JButton, JCheckBox, and JTabbedPane are all Swing components that are used
to create graphical user interfaces (GUIs) in Java.

 JButton is a simple button that can be used to perform an action when it is


clicked. It can be used to start a program, open a file, or perform any other
task that you want to associate with a button click.
 JCheckBox is a check box that can be used to select or deselect an option. It
is often used to allow users to choose their preferences, such as whether they
want to receive email notifications or not.
 JTabbedPane is a container that can be used to organize multiple panels into
a single window. This allows users to switch between different panels without
having to open and close multiple windows.

Here is a table that summarizes the key features of these three components:

Component Feature

JButton Can be used to perform an action when clicked

JCheckBox Can be used to select or deselect an option

JTabbedPane Can be used to organize multiple panels into a single window

Here are some examples of how these components can be used in a GUI:

 A JButton can be used to start a program. For example, you could create a
JButton with the text "Start" that would start your program when it is clicked.

 A JCheckBox can be used to allow users to choose their preferences. For


example, you could create a JCheckBox with the text "Receive email
notifications" that would allow users to choose whether they want to receive
email notifications or not.

 A JTabbedPane can be used to organize multiple panels into a single window.


For example, you could create a JTabbedPane with three panels: one for your
home page, one for your blog, and one for your contact information.
These are just a few examples of how these components can be used in a GUI.
There are many other ways that you can use them to create your own custom GUIs.

12 a) Write short notes on Flow Layout and Boarder Layout.

Sure, here are short notes on Flow Layout and Border Layout:

Flow Layout
 FlowLayout is a simple layout manager that arranges components in a single
row or column, depending on the container's orientation.

 Components are added to the container in the order in which they are added,
and they are resized to fit the available space.

 If there is not enough space to fit all of the components in a single row or
column, the components will wrap to the next row or column.

 FlowLayout is the default layout manager for JPanels.

Border Layout
 BorderLayout is a more complex layout manager that divides the container
into five regions: north, south, east, west, and center.

 Each region can contain only one component, and the components are
arranged in the order in which they are added to the container.

 The center region is the default region for components, and it will expand to fill
any empty space in the container.

 BorderLayout is often used for dialog boxes and other windows that have a
fixed layout.

Here is a table that summarizes the key differences between Flow Layout and
Border Layout:

Feature Flow Layout Border Layout

Number of regions 1 5

Default region Center Center


How components are In a single row or
In five regions
arranged column

To fit the available To fill the region they are


How components are resized
space in

When to use it For simple layouts For more complex layouts

b) Write an applet code to demonstrate parameter passing to applet.

import java.applet.Applet;

import java.awt.Graphics;

public class ParamDemo extends Applet {

private String message;

public void init() {

// Get the parameter value from the HTML page.

message = getParameter("msg");

}
public void paint(Graphics g) {

// Draw the message on the applet.

g.drawString(message, 50, 50);

13 a) List the event classes and Listener Interfaces.

Sure, here are some of the event classes and listener interfaces in Java:

Event Classes
 ActionEvent is generated when a button is clicked, a menu item is selected, or
a text field is entered.
 MouseEvent is generated when the mouse is moved, clicked, or dragged.
 KeyEvent is generated when a key is pressed or released.
 FocusEvent is generated when a component gains or loses focus.
 WindowEvent is generated when a window is opened, closed, iconified, or
deiconified.
Listener Interfaces
 ActionListener is used to handle action events.
 MouseListener and MouseMotionListener are used to handle mouse events.
 KeyListener is used to handle keyboard events.
 FocusListener is used to handle focus events.
 WindowListener is used to handle window events.

There are many other event classes and listener interfaces in Java. These are just a
few of the most common ones.

b) Write the step wise procedure to create and run an applet.

Creating and running an applet involves several steps. Applets are small Java
programs that are embedded within webpages and run in a web browser. Below is
the step-by-step procedure to create and run a basic applet:
1. Write the Applet Code: Open a text editor (e.g., Notepad, Visual Studio
Code) and write the Java applet code. The applet code should be placed inside
a class that extends java.applet.Applet. Below is a simple example of a basic
applet that displays "Hello, World!" on the screen:
}
2. Compile the Applet Code: Save the applet code with a .java extension (e.g.,
HelloWorldApplet.java). Open a command prompt or terminal and navigate to
the directory containing the applet code. Compile the code using the javac
compiler:
If there are no errors in the code, this will generate a compiled .class file.
3. Create the HTML File: To run the applet in a web browser, you need to
create an HTML file that embeds the applet. Create a new text file and name it
HelloWorldApplet.html (use the same name as the applet class). In the HTML
file, you should include the <applet> tag, specifying the width, height, and
codebase (location of the applet class) attributes. Here's an example of the
HTML file:
Note: The applet's .class file and HTML file should be in the same directory.
4. Run the Applet: Open a web browser and navigate to the location of the
HTML file. The applet should be displayed in the browser, and you should see
"Hello, World!" on the screen.
Alternatively, you can run the applet using the appletviewer command from
the command prompt
14 a) What is the difference between init( ) and start ( ) methods in an Applet? When will
each be executed?

The init() and start() methods are two of the most important methods in an
applet. They are called at different times during the life cycle of an applet, and they
serve different purposes.

The init() method is called when the applet is first loaded. It is used to initialize the
applet's components and other resources. This method is called only once, when the
applet is first loaded.

The start() method is called after the init() method. It is used to start the applet
running. This method is called whenever the applet is displayed in the browser
window.

Here is a table that summarizes the differences between the init() and start()
methods:
Method Purpose When is it called?
Initializes the applet's components and
init() Once, when the applet is first loaded.
other resources.

Whenever the applet is displayed in the


start() Starts the applet running.
browser window.

b) What are the various components of Swing? Explain.

Swing is a graphical user interface (GUI) toolkit for Java. It is a set of reusable
components that can be used to create graphical user interfaces. Swing components
are platform-independent, which means that they look the same on all platforms.

The various components of Swing include:

 Containers: Containers are components that can hold other components.


They are used to organize the layout of the GUI. Some examples of
containers include JFrame, JPanel, and JDialog.
 Basic Components: Basic components are the most basic components in
Swing. They are used to display text, images, and other simple elements.
Some examples of basic components include JLabel, JTextField, and
JButton.
 Advanced Components: Advanced components are more complex
components that provide more functionality. Some examples of advanced
components include JTable, JTree, and JList.
 Layout Managers: Layout managers are used to control the layout of the
components in a container. Some examples of layout managers include
FlowLayout, BorderLayout, and GridLayout.

15 a) What is a Layout manager? Explain different types of Layout managers.

A layout manager is an object that is responsible for arranging the components in a


container. It defines how the components are positioned and sized within the
container.
There are many different types of layout managers available in Java, each with its
own advantages and disadvantages. Some of the most common layout managers
include:

 FlowLayout: This is the simplest layout manager. It arranges components in a


single row or column, depending on the container's orientation.

 BorderLayout: This layout manager divides the container into five regions:
north, south, east, west, and center. Each region can contain only one
component, and the components are arranged in the order in which they are
added to the container.

 GridLayout: This layout manager arranges components in a grid. The number


of rows and columns in the grid can be specified by the programmer.

 CardLayout: This layout manager displays a single component at a time. The


components are arranged in a stack, and the user can switch between
components by clicking on tabs or buttons.

 GridBagLayout: This layout manager is the most flexible layout manager. It


allows the programmer to specify the exact position and size of each
component.

The choice of layout manager depends on the specific requirements of the GUI. For
example, if you need to create a simple GUI with a few components, then
FlowLayout might be a good choice. If you need to create a more complex GUI with
multiple components, then BorderLayout or GridLayout might be a better choice.

b) Write a program to create a frame window that responds to key clicks.

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

public class KeyClickFrame extends Frame implements KeyListener {


private String message;

public KeyClickFrame(String title) {

super(title);

addKeyListener(this);

public void keyPressed(KeyEvent e) {

message = "Key pressed: " + e.getKeyChar();

repaint();

public void keyReleased(KeyEvent e) {

message = "Key released: " + e.getKeyChar();

repaint();

public void keyTyped(KeyEvent e) {


message = "Key typed: " + e.getKeyChar();

repaint();

public void paint(Graphics g) {

g.drawString(message, 50, 50);

public static void main(String[] args) {

KeyClickFrame frame = new KeyClickFrame("Key Click Frame");

frame.setSize(300, 300);

frame.setVisible(true);

16 a) What is an applet? Explain the life cycle of Applet with a neat sketch.

An applet is a small Java program that is embedded in a web page. Applets can be
used to display dynamic content in web pages, such as images, animations, and
interactive games.

The life cycle of an applet is divided into five phases:


1. Init: This phase is called when the applet is first loaded. In this phase, the
applet can initialize its components and other resources.
2. Start: This phase is called after the init phase. In this phase, the applet can
start running.
3. Paint: This phase is called whenever the applet needs to be repainted. In this
phase, the applet can draw its components on the screen.
4. Stop: This phase is called when the applet is no longer visible. In this phase,
the applet can release any resources that it is using.
5. Destroy: This phase is called when the applet is being removed from the web
page. In this phase, the applet can clean up any of its remaining resources.

b) Write the applets to draw the Cube and Cylinder shapes.

code for an applet to draw a cube:

import java.applet.Applet;

import java.awt.Graphics;
public class Cube extends Applet {

private int side = 100;

public void paint(Graphics g) {

// Draw the sides of the cube.

for (int i = 0; i < 6; i++) {

int x1 = side * (i % 2);

int y1 = side * (i / 2);

int x2 = side * (i % 2 + 1);

int y2 = side * (i / 2 + 1);

g.drawLine(x1, y1, x2, y1);

g.drawLine(x2, y1, x2, y2);

g.drawLine(x2, y2, x1, y2);

g.drawLine(x1, y2, x1, y1);

The code for an applet to draw a cylinder is similar:

import java.applet.Applet;

import java.awt.Graphics;

public class Cylinder extends Applet {

private int radius = 50;


private int height = 100;

public void paint(Graphics g) {

// Draw the base of the cylinder.

int[] xPoints = {-radius, radius, radius, -radius};

int[] yPoints = {0, 0, height, height};

g.drawPolygon(xPoints, yPoints, 4);

// Draw the sides of the cylinder.

for (int i = 0; i < 360; i += 30) {

int x1 = radius * Math.cos(i * Math.PI / 180);

int y1 = radius * Math.sin(i * Math.PI / 180);

int x2 = radius * Math.cos((i + 30) * Math.PI / 180);

int y2 = radius * Math.sin((i + 30) * Math.PI / 180);

g.drawLine(x1, y1, x2, y2);

You might also like