Вы находитесь на странице: 1из 7

Question 1.

What is use of setDefaultCloseOperation method and what are


possible values?

Answer 1. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) is used to close


your J-Frame as well as Java process. Test it with a JFrame sample program without the
setDefaultCloseOperaton. Close your J-frame by clicking the windows close. Check your task
manager/top command, you will find the java process will be still running.

JFrame.EXIT_ON_CLOSE — Exit the application.


JFrame.HIDE_ON_CLOSE — Hide the frame, but keep the application running.
JFrame.DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the application running.
JFrame.DO_NOTHING_ON_CLOSE — Ignore the click.
If you forget to call setDefaultCloseOperation() you will get JFrame.HIDE_ON_CLOSE by
default. This can be frustrating, because it looks like you have "killed" the program, but it keeps on
running, and you see no frame.

Question 2. What is a Layout Manager and what are the different Layout
Managers available in java.awt?

Answer 2. Java LayoutManagers


The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an
interface that is implemented by all the classes of layout managers. There are following classes
that represents the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.

1. 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 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
2. Java FlowLayout
The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the
default layout of applet or panel.
Fields of FlowLayout class
public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
3. Java GridLayout
The GridLayout is used to arrange the components in rectangular grid. One component is
displayed in each rectangle.
GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no
gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and
columns alongwith given horizontal and vertical gaps.
4. Java CardLayout
The CardLayout class manages the components in such a manner that only one component is
visible at a time. It treats each component as a card that is why it is known as CardLayout.
Constructors of CardLayout class
CardLayout(): creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.
5. Java GridBagLayout
The Java GridBagLayout class is used to align components vertically, horizontally or along their
baseline.
The components may not be of same size. Each GridBagLayout object maintains a dynamic,
rectangular grid of cells. Each component occupies one or more cells known as its display area.
Each component associates an instance of GridBagConstraints. With the help of constraints object
we arrange component's display area on the grid. The GridBagLayout manages each component's
minimum and preferred sizes in order to determine component's size.
6. Java BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally. For this
purpose, BoxLayout provides four constants. They are as follows:
Fields of BoxLayout class
public static final int X_AXIS
public static final int Y_AXIS
public static final int LINE_AXIS
public static final int PAGE_AXIS
7. GroupLayout
GroupLayout groups its components and places them in a Container hierarchically. The grouping
is done by instances of the Group class.
Group is an abstract class and two concrete classes which implement this Group class are
SequentialGroup and ParallelGroup.
SequentialGroup positions its child sequentially one after another where as ParallelGroup aligns
its child on top of each other.
The GroupLayout class provides methods such as createParallelGroup() and
createSequentialGroup() to create groups.
GroupLayout treats each axis independently. That is, there is a group representing the horizontal
axis, and a group representing the vertical axis. Each component must exists in both a horizontal
and vertical group, otherwise an IllegalStateException is thrown during layout, or when the
minimum, preferred or maximum size is requested.

8. ScrollPaneLayout
The layout manager used by JScrollPane. JScrollPaneLayout is responsible for nine components: a
viewport, two scrollbars, a row header, a column header, and four "corner" components.

9. Java SpringLayout
A SpringLayout arranges the children of its associated container according to a set of
constraints.Constraints are nothing but horizontal and vertical distance between two component
edges. Every constrains are represented by a SpringLayout.Constraint object.
Each child of a SpringLayout container, as well as the container itself, has exactly one set of
constraints associated with them.
Each edge position is dependent on the position of the other edge. If a constraint is added to create
new edge than the previous binding is discarded. SpringLayout doesn't automatically set the
location of the components it manages.
Question 3. Develop a simple client-server application in java

CLASS

package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class AddC extends UnicastRemoteObject implements AddI{

public AddC()throws RemoteException{


super();
}

public int add(int a, int b) throws RemoteException {

// TODO Auto-generated method stub


return a+b;
}
@Override
public int sub(int a, int b) throws Exception {
// TODO Auto-generated method stub
return a-b;
}
@Override
public int mul(int a, int b) throws Exception {
// TODO Auto-generated method stub
return a*b;
}
@Override
public int div(int a, int b) throws Exception {
// TODO Auto-generated method stub
return a/b;
}

}
INTERFACE

package rmi;

import java.rmi.Remote;

public interface AddI extends Remote{


public int add(int a,int b)throws Exception;
public int sub(int a,int b)throws Exception;
public int mul(int a,int b)throws Exception;
public int div(int a,int b)throws Exception;

CLIENT

package rmi;

import java.rmi.Naming;
import java.util.Scanner;

import org.omg.CosNaming.NamingContextExtHolder;

public class Client {

public static void main(String[] args)throws Exception {


// TODO Auto-generated method stub
Scanner scn=new Scanner(System.in);
try {
int x,y;

AddI c=(AddI)Naming.lookup("//localhost/calc");
c.

int choice =0;


System.out.println(" 1 for add \n 2 for sub \n 3 for mul \n 4 for divide");
choice=scn.nextInt();
System.out.println("enter a and b");
x=scn.nextInt();
y=scn.nextInt();
int ans=0;
switch (choice) {
case 1:
ans=c.add(x, y);
System.out.println("sum="+ans);

break;
case 2:
ans=c.sub(x, y);
System.out.println("sub="+ans);
break;
case 3:

System.out.println("mul="+c.mul(x, y));
break;

case 4:
System.out.println("div="+c.div(x, y));
break;
default:
break;

}
}
catch (Exception e) {
// TODO: handle exception
}
}

SERVER

package rmi;

import java.rmi.Naming;
import java.rmi.registry.Registry;

public class server {


public static void main(String arg[])
{
try {
Registry r=java.rmi.registry.LocateRegistry.createRegistry(1099);
r.rebind("calc", new AddC());
System.out.println("server is connected");

r.unbind("calc");
}catch(Exception e)
{
System.out.println("server not connected"+e);
}

}
}

Вам также может понравиться