Ajp QB

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

AJP Question Bank for Weekly Test

Each question carries 5 marks

1. Explain layout managers in awt and write program on Gridbag layout


manager.

In the Abstract Window Toolkit (AWT), layout managers are responsible for arranging and
resizing components (like buttons, labels, text fields, etc.) in a container (like a frame or
panel). Each layout manager follows a different strategy for placing the components in the
container.Here are some common AWT layout managers:

1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
5. GridBagLayout

Program of GridBagLayout:
import java.awt.*;
import javax.swing.*;

public class SimpleGridBagLayoutExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Simple GridBagLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Add Button 1 at position (0, 0)
gbc.gridx = 0;
gbc.gridy = 0;
JButton button1 = new JButton("Button 1");
frame.add(button1, gbc);
// Add Button 2 at position (1, 0)
gbc.gridx = 1;
gbc.gridy = 0;
JButton button2 = new JButton("Button 2");
frame.add(button2, gbc);
// Add Button 3 at position (0, 1) spanning 2 columns
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 2;
JButton button3 = new JButton("Button 3");
frame.add(button3, gbc);
frame.pack(); // Adjusts the frame to fit the preferred sizes of the components
frame.setVisible(true);
}
}
2) WAP to create following menu bar.
import javax.swing.*;
import java.awt.event.*;
public class MenuBarExample {
public static void main(String[] args) {
// Create a new frame
JFrame frame = new JFrame("Menu Bar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
// Create the menu bar
JMenuBar menuBar = new JMenuBar();
// Create the "File" menu
JMenu fileMenu = new JMenu("File");
// Add menu items to the "File" menu
JMenuItem openItem = new JMenuItem("Open");
JMenuItem newItem = new JMenuItem("New");
JMenuItem saveItem = new JMenuItem("Save");
// Add menu items to the "File" menu
fileMenu.add(openItem);
fileMenu.add(newItem);
fileMenu.add(saveItem);
// Add the "File" menu to the menu bar
menuBar.add(fileMenu);
// Create the "Scan" menu
JMenu scanMenu = new JMenu("Scan");
menuBar.add(scanMenu);
// Create the "View" menu
JMenu viewMenu = new JMenu("View");
menuBar.add(viewMenu);
// Create the "Help" menu
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
// Set the menu bar for the frame
frame.setJMenuBar(menuBar);
// Display the frame
frame.setVisible(true);
}
}
3) WAP using Button, Label, List, Checkbox, Choice components in AWT.
import java.awt.*;

public class BasicAWTComponentExample {


public static void main(String[] args) {
// Create a new frame
Frame frame = new Frame("Basic AWT Component Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());

// Create a label
Label label = new Label("Select your options:");

// Create a list with multiple choices


List list = new List(4, true); // 4 visible rows, multiple selections allowed
list.add("Option 1");
list.add("Option 2");
list.add("Option 3");
list.add("Option 4");

// Create a checkbox
Checkbox checkbox = new Checkbox("Agree to terms and conditions");

// Create a choice dropdown


Choice choice = new Choice();
choice.add("Choice 1");
choice.add("Choice 2");
choice.add("Choice 3");

// Create a button
Button button = new Button("Submit");

// Add components to the frame


frame.add(label);
frame.add(list);
frame.add(checkbox);
frame.add(choice);
frame.add(button);

// Set frame visibility


frame.setVisible(true);
}
}
4) Differentiate between AWT and Swing, explain about MVC
architecture.

Sr AWT SWING
No.
1 AWT components Java swing components
are platform-dependent. are platform-independent.
2 AWT components Swing components
are heavyweight. are lightweight.
3 AWT provides less Swing provides more powerful
components than Swing. components such as tables,
lists, scrollpanes, colorchooser,
tabbedpane etc.
4 MVC pattern is not supported by MVC pattern is supported by Swing.
AWT.
5 AWT components require java.awt Swing components requires javax.swing
package package

MVC (Model-View-Controller) is a pattern in software design commonly used to implement


user interfaces, data, and controlling logic. It emphasizes a separation between the software's
business logic and display. This "separation of concerns" provides for a better division of
labor and improved maintenance

5) WAP using Tree component to print following output.


Tpoly
-co
-fy
-sy
-ty
-if
-fy
-sy
-ty

import java.awt.*;

public class TreeStructureExample {


public static void main(String[] args) {
// Create a new frame
Frame frame = new Frame("Tree Structure Example");
frame.setSize(300, 300);
frame.setLayout(new FlowLayout(FlowLayout.LEFT));

// Create panels for hierarchical structure


Panel tpolyPanel = new Panel(new GridLayout(4, 1));
Label tpolyLabel = new Label("tpoly");
tpolyPanel.add(tpolyLabel);

// Create "co" and "if" panels inside "tpoly"


Panel coPanel = new Panel(new GridLayout(4, 1, 10, 0));
Label coLabel = new Label(" co");
coPanel.add(coLabel);
Panel ifPanel = new Panel(new GridLayout(4, 1, 10, 0));
Label ifLabel = new Label(" if");
ifPanel.add(ifLabel);

// Create "fy", "sy", "ty" labels inside "co"


Label fyCoLabel = new Label(" fy");
Label syCoLabel = new Label(" sy");
Label tyCoLabel = new Label(" ty");
coPanel.add(fyCoLabel);
coPanel.add(syCoLabel);
coPanel.add(tyCoLabel);

// Create "fy", "sy", "ty" labels inside "if"


Label fyIfLabel = new Label(" fy");
Label syIfLabel = new Label(" sy");
Label tyIfLabel = new Label(" ty");
ifPanel.add(fyIfLabel);
ifPanel.add(syIfLabel);
ifPanel.add(tyIfLabel);

// Add "co" and "if" panels to "tpoly"


tpolyPanel.add(coPanel);
tpolyPanel.add(ifPanel);

// Add the tree structure to the frame


frame.add(tpolyPanel);

// Set frame visibility


frame.setVisible(true);
}
}
6) Explain delegation event model and write steps to handle events in java.
The Delegation Event Model is a programming pattern used in Java for handling events in
graphical user interfaces (GUIs). When a GUI component generates an event (such as a
button press), it is delegated to event listeners of that component. These listeners then handle
the event by executing the appropriate code.

Steps to Handle Events in Java

Here are the steps involved in handling events in Java:

1. Create an Event Source:


o This is the GUI component that will generate events. For example, a button
that generates an action event when clicked.
2. Implement an Event Listener:
o Create a class or anonymous inner class that implements the appropriate event
listener interface. This interface contains methods that need to be overridden
to handle the events.
3. Register the Event Listener with the Event Source:
o Attach the event listener to the event source so that the listener can receive and
handle events generated by the source.
4. Handle the Event:
o In the event listener's method, write the code that should execute when the
event occurs.

You might also like