0% found this document useful (0 votes)
14 views

Rest of Java

Uploaded by

manglamdubey2011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Rest of Java

Uploaded by

manglamdubey2011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INDEX:

1. Define socket. Explain any of one TCP IP server socket or

TCP IP client socket.

2. Explain different types of layout managers in java.

3. What is event handling in Java? Explain components of it.

4. Explain panel, frame, canvas, windows and container.


1. Define socket. Explain any of one TCP IP server socket or TCP IP client
socket.
ANS- A socket is an endpoint for communication between two machines. It is a
combination of an IP address and a port number. Sockets are used to establish a
connection between a client and a server to facilitate data transfer

TCP/IP Server Socket


A TCP/IP server socket listens for incoming client connections and provides services
to the connected clients. Steps to create a simple TCP/IP server socket in Java:
1. Create a ServerSocket object to listen for connections on a specific port.
2. Accept client connections using the accept method of the ServerSocket class.
3. Communicate with the client using input and output streams.
Example Code:
import java.io.*;
import java.net.*;

public class TCPServer {


public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(5000)) {
System.out.println("Server is listening on port 5000");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");

new ServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ServerThread extends Thread {


private Socket socket;

public ServerThread(Socket socket) {


this.socket = socket;
}

public void run() {


try (InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(input));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true)) {

String text;
while ((text = reader.readLine()) != null) {
System.out.println("Received from client: " + text);
writer.println("Echo: " + text);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
TCP/IP Client Socket
A TCP/IP client socket connects to a server socket to communicate with the server.
Steps to create a simple TCP/IP client socket in Java:
1. Create a Socket object to connect to the server on a specific IP address and
port.
2. Communicate with the server using input and output streams.
Example Code:
import java.io.*;
import java.net.*;

public class TCPClient {


public static void main(String[] args) {
String hostname = "localhost";
int port = 5000;

try (Socket socket = new Socket(hostname, port)) {


OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);

InputStream input = socket.getInputStream();


BufferedReader reader = new
BufferedReader(newInputStreamReader(input));
BufferedReader consoleReader = new BufferedReader(new
InputStreamReader(System.in));
String text;

System.out.println("Connected to the server. Type a message and press


Enter to send:");

while ((text = consoleReader.readLine()) != null) {


writer.println(text);
String response = reader.readLine();
System.out.println("Server response: " + response);
}
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
2. Explain different types of layout managers in java.
ANS- Layout managers are used to arrange the components in a container. They
define the way components are organized and displayed within the container. Java
provides several types of layout managers:
1. FlowLayout: Arranges components in a left-to-right flow, much like lines of text
in a paragraph. When one line is full, it moves to the next line.
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
2. BorderLayout: Divides the container into five regions: NORTH, SOUTH, EAST,
WEST, and CENTER. Each region can contain only one component.
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("South"), BorderLayout.SOUTH);
frame.add(new JButton("East"), BorderLayout.EAST);
frame.add(new JButton("West"), BorderLayout.WEST);
frame.add(new JButton("Center"), BorderLayout.CENTER);

3. GridLayout: Arranges components in a grid with equal-sized cells. Components


are added in a left-to-right, top-to-bottom order.
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3)); // 2 rows and 3 columns
4. BoxLayout: Arranges components either vertically or horizontally. It's often used
with Box containers.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
5. CardLayout: Stacks components like a deck of cards, showing only one at a time.
It allows switching between components.
JPanel panel = new JPanel();
CardLayout cardLayout = new CardLayout();
panel.setLayout(cardLayout);
panel.add(new JButton("Card 1"), "Card 1");
panel.add(new JButton("Card 2"), "Card 2");
cardLayout.show(panel, "Card 1");
3. What is event handling in Java? Explain components of it.
ANS- Event handling is a key concept in building interactive applications in Java. It
involves the detection and processing of events such as button clicks, mouse
movements, key presses, and other user interactions. The main components of event
handling in Java are:
1. Event Classes
Event classes represent the different types of events that can occur in a Java
application. These classes are part of the java.awt.event package. Some common
event classes include:
• ActionEvent: Represents an action event, such as a button click.
• MouseEvent: Represents mouse events, including mouse clicks, mouse
movements, and mouse button presses.
• KeyEvent: Represents keyboard events, such as key presses and releases.
• WindowEvent: Represents window events, such as opening, closing, and
iconifying a window.
• ItemEvent: Represents events generated by item selection, like from a
checkbox or choice.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonClickExample {


public static void main(String[] args) {
Button button = new Button("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!"); } }); } }
2. Event Listeners
Event listeners are interfaces in the java.awt.event package that receive and process
events. An event listener must be registered with an event source to handle events of
a particular type. Some common event listener interfaces include:
• ActionListener: Listens for action events.
• MouseListener: Listens for mouse events.
• KeyListener: Listens for key events.
• WindowListener: Listens for window events.
• ItemListener: Listens for item events.
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ButtonClickExample {


public static void main(String[] args) {
Frame frame = new Frame("Button Click Example");
Button button = new Button("Click Me");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
});
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
3. Delegation Event Model
The delegation event model in Java involves three components: event source
(generates events), event object (describes events), and event listener (handles
events). The process: the event source generates an event, creates an event object,
and sends it to the event listener, which then processes the event.
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonClickExample {


public static void main(String[] args) {
Frame frame = new Frame("Button Click Example");
Button button = new Button("Click Me");
// Event Listener
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button was clicked!");
}
};

// Registering the listener with the event source


button.addActionListener(listener);

frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
4. Explain panel, frame, canvas, windows and container.
ANS-
Panel
A lightweight container used to group other components within a window.
Often used for organizing components in an applet or frame.
Panel panel = new Panel();
panel.add(new Button("Click Me"));
Frame
Description: A top-level window with a title and border, capable of containing
other components.
Usage: Commonly used as the main window for a standalone application.
Frame frame = new Frame("My Frame");
frame.setSize(300, 300);
frame.setVisible(true);
Canvas
Description: A blank rectangular area where custom drawing can be done and
images can be displayed.
Usage: Useful for creating custom graphics or for drawing shapes.
Canvas canvas = new Canvas() {
public void paint(Graphics g) {
g.drawString("Hello, Canvas!", 20, 20);
}
};
Window
Description: A top-level window without borders or menubar, usually used as a
pop-up or secondary window.
Usage: Can serve as a dialog or an additional window to the main application
frame.
Window window = new Window(frame);
window.setSize(200, 200);
window.setVisible(true);
Container
Description: A generic component that can hold other AWT components.
Usage: Acts as a base class for components like Panel and Frame that contain other
components.
Container container = frame.getContentPane();
container.add(new Button("Click Me"));

You might also like