1) Write a program using swing components to multiply two numbers.
Use text fields
for inputs and output. Your program should display the result when the user press a
button
Solution
import [Link].*;
import [Link].*;
import [Link];
import [Link];
class Multiplication extends JFrame implements ActionListener //implement
listener interface
JLabel l1, l2;
JTextField t1, t2, t3;
JButton b1;
public Multiplication()
l1 = new JLabel("First Number:");
[Link](20, 10, 100, 20); //x, y, width, height
t1 = new JTextField(10);
[Link](120, 10, 100, 20);
l2 = new JLabel("Second Number:");
[Link](20, 40, 100, 20);
t2 = new JTextField(10);
[Link](120, 40, 100, 20);
b1 = new JButton("Product");
[Link](20, 70, 80, 20);
t3 = new JTextField(10);
[Link](120, 70, 100, 120);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(t3);
[Link](this); //Registering event
setSize(400,300);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@Override
public void actionPerformed(ActionEvent e) //Handle Event
{
if([Link]()==b1){
int num1 = [Link]([Link]());
int num2 = [Link]([Link]());
int product = num1 * num2;
[Link]([Link](product));
}
public static void main(String args[])
{
new Multiplication();
}
2) discuss border layout with suitable example.
Ans.
BorderLayout is the default layout for the window objects such as JFrame, JWindow,
JDialog, JInternalFrame etc. BorderLayout arranges the components in the five
regions. Four sides are referred to as north, south, east, and west. The middle part is
called the center. Each region can contain only one component and is identified by a
corresponding constant as NORTH, SOUTH, EAST, WEST, and CENTER.
Constructors:
BorderLayout(): It will construct a new borderlayout with no gaps between the
components.
BorderLayout(int, int): It will constructs a border layout with the specified gaps
between the components.
Commonly Used Methods:
toString(): Returns a string which is the representation of the state of border layout.
getLayoutAlignmentX(Container parent): Returns the layout alignment along the X-
axis.
getLayoutAlignmentY(Container parent): It will return the layout alignment along the
Y-axis.
removeLayoutComponent(Component comp): This method is used to remove the
specified component from the borderlayout.
getVgap(): Return the vertical gap between the components.
getHgap(): Returns the Horizontal gap between the components.
setHgap(int hgap): It is used to set the horizontal gap between the components.
setVgap(int vgap): It is used to set the vertical gap between the components.
Below Programs will illustrate the BorderLayout class:
Program 1: This program will show how to pass the arguments in BorderLayout. Set
the background color by using setBackground() method. We create 5 JButton
components named “btn1“, “btn2“, “btn3“, “btn4“, “btn5“, and then add them to the
JFrame by using add() method. We set the title, size, and visibility of the frame by
using setTitle(), setSize() and setVisible() methods respectively. The layout is set by
the method setLayout().
// Java program to illustrate the BorderLayout
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
// class extends JFrame
public class BorderDemo extends JFrame {
// Constructor of BorderDemo class.
public BorderDemo()
// set the layout
setLayout(new BorderLayout());
// set the background
setBackground([Link]);
// creates Button (btn1)
Button btn1 = new Button("Geeks");
// creates Button (btn2)
Button btn2 = new Button("GFG");
// creates Button (btn3)
Button btn3 = new Button("Sudo Placement");
// creates Button (btn4)
// Java program to illustrate the BorderLayout
import [Link].*;
import [Link].*;
import [Link].*;
// class extends JFrame
class BoderLayoutDemo extends JFrame {
BoderLayoutDemo()
// Creating Object of Jpanel class
JPanel pa = new JPanel();
// set the layout
[Link](new BorderLayout());
// add a new JButton with name "wel" and it is
// lie top of the container
[Link](new JButton("WelCome"), [Link]);
// add a new JButton with name "come" and it is
// lie button of the container
[Link](new JButton("Geeks"), [Link]);
// add a new JButton with name "Layout" and it is
// lie left of the container
[Link](new JButton("Layout"), [Link]);
// add a new JButton with name "Border" and it is
// lie right of the container
[Link](new JButton("Border"), [Link]);
// add a new JButton with name "hello everybody" and it is
// lie center of the container
[Link](new JButton("GeeksforGeeks"), [Link]);
// add the pa object which refer to the Jpanel
add(pa);
// Function to close the operation of JFrame.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Function to set size of JFrame.
setSize(300, 300);
// Function to set visible status of JFrame.
setVisible(true);
class MainFrame {
// Driver code
public static void main(String[] args)
// calling the constructor
new BoderLayoutDemo();
}
}
3) discuss the role of event listener to handle events with suitable example.
Ans.
The Event listener represent the interfaces responsible to handle events. Java provides
us various Event listener classes but we will discuss those which are more frequently
used. Every method of an event listener method has a single argument as an object
which is subclass of EventObject class. For example, mouse event listener methods
will accept instance of MouseEvent, where MouseEvent derives from EventObject.
An event listener in Java is designed to process some kind of event — it "listens" for
an event, such as a user's mouse click or a key press, and then it responds accordingly.
An event listener must be connected to an event object that defines the event.
4) what is layout management. discuss any three layout class with example of each..
Ans.
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in
GUI forms. LayoutManager is an interface that is implemented by all the classes of
layout managers. There are the following classes that represent the layout managers:
1) [Link]
2) [Link]
3) [Link]
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north,
south, east, west, and center. Each region (area) may contain one component
only. It is the default layout of a frame or window. The BorderLayout provides
five constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
Example:
import [Link].*;
import [Link].*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as
NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as
SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as
CENTER
[Link](b1, [Link]); // b1 will be placed in the North Direction
[Link](b2, [Link]); // b2 will be placed in the South Direction
[Link](b3, [Link]); // b2 will be placed in the East Direction
[Link](b4, [Link]); // b2 will be placed in the West Direction
[Link](b5, [Link]); // b2 will be placed in the Center
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new Border();
}
}
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular
grid. One component is displayed in each rectangle.
Example:
import [Link].*;
import [Link].*;
public class GridLayoutExample
{
JFrame frameObj;
// constructor
GridLayoutExample()
{
frameObj = new JFrame();
// creating 9 buttons
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
// adding buttons to the frame
// since, we are using the parameterless constructor, therfore;
// the number of columns is equal to the number of buttons we
// are adding to the frame. The row count remains one.
[Link](btn1); [Link](btn2); [Link](btn3);
[Link](btn4); [Link](btn5); [Link](btn6);
[Link](btn7); [Link](btn8); [Link](btn9);
// setting the grid layout using the parameterless constructor
[Link](new GridLayout());
[Link](300, 300);
[Link](true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after
another (in a flow). It is the default layout of the applet or panel.
Example:
import [Link].*;
import [Link].*;
public class FlowLayoutExample
{
JFrame frameObj;
// constructor
FlowLayoutExample()
{
// creating a frame object
frameObj = new JFrame();
// creating the buttons
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("10");
// adding the buttons to frame
[Link](b1); [Link](b2); [Link](b3); [Link](b4);
[Link](b5); [Link](b6); [Link](b7); [Link](b8);
[Link](b9); [Link](b10);
// parameter less constructor is used
// therefore, alignment is center
// horizontal as well as the vertical gap is 5 units.
[Link](new FlowLayout());
[Link](300, 300);
[Link](true);
}
// main method
public static void main(String argvs[])
{
new FlowLayoutExample();
}
}
5) Discuss GridLayout with example.
Ans. Above.
6) What is the benefits of using swing components.? Explain.
.Ans.
Swing components can use a different look and feel. Swing components use the
Model-View-Controller paradigm (MVC) and thus can provide a much more
flexible UI. Swing components are lightweight (are less resource-intensive than
AWT). Swing provides built-in double buffering.
7) Why do we need layout management? What is GridBag layout.
A layout manager is an object that implements the LayoutManager interface*
and determines the size and position of the components within a container.
Although components can provide size and alignment hints, a container's layout
manager has the final say on the size and position of the components within the
container.
GridBagLayout is one of the most flexible — and complex — layout managers
the Java platform provides. A GridBagLayout places components in a grid of
rows and columns, allowing specified components to span multiple rows or
columns. Not all rows necessarily have the same height.