Professional Documents
Culture Documents
JAVA Unit 5 Question Bank Answersd
JAVA Unit 5 Question Bank Answersd
JAVA Unit 5 Question Bank Answersd
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:
4. What are the differences between JToggle buttion and Radio buttion?
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.
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.
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.
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.
JComponent
o Container
JFrame
JDialog
JApplet
o JPanel
JButton
JLabel
JTextField
JTextArea
JList
JComboBox
JMenuBar
JMenu
JMenuItem
Swing components are preferred over AWT components for a number of reasons,
including:
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:
Keyboard events are generated when the user interacts with the keyboard. The
following are some of the most common 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.
Here is a table that summarizes the key features of these three components:
Component Feature
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.
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.
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:
Number of regions 1 5
import java.applet.Applet;
import java.awt.Graphics;
message = getParameter("msg");
}
public void paint(Graphics g) {
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.
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.
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.
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.
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.
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
super(title);
addKeyListener(this);
repaint();
repaint();
repaint();
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.
import java.applet.Applet;
import java.awt.Graphics;
public class Cube extends Applet {
import java.applet.Applet;
import java.awt.Graphics;