Java 4 Mark Answers Ut2
Java 4 Mark Answers Ut2
2 marks
1.What is frame in swing. how to create frame.
Ans: A frame is a window that can hold component like button, textFiels,
label etc. that can part of the javax.swing package.
Frame is extending two ways.
1.By extending Frame class.
Syntax. Class ABC extends JFrame()
{
}
2.By creating object.
Syntax: JFrame f=new JFrame();
2. KeyTyped ()
Syntax:
public void keyTyped (KeyEvent e)
3. KeyReleased ()
Syntax:
public void keyReleased (KeyEvent e)
2) mouseEnterd()
Syntax:
public void mouseEnterd (MouseEvent e)
3) mouseClicked()
Syntax:
public void mouseClicked (MouseEvent e)
4) mouseExited()
Syntax:
public void mouseExited (MouseEvent e)
5)mouseReleased()
Syntax:
public void mouseReleased (MouseEvent e)
Ans.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public KeyEventDemo() {
label = new JLabel("Press any key", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 20));
add(label);
addKeyListener(this);
setSize(400, 200);
setTitle("Key Event Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
Q.2 Explain Delegation Event Model?
Ans.
Event Handling is the mechanism that controls the event and decides what should happen if
an event occurs. This mechanism have the code which is known as event handler that is
executed when an event occurs. Java Uses the Delegation Event Model to handle the events.
This model defines the standard mechanism to generate and handle the events. Let's have a
brief introduction to this model.
The Delegation Event Model has the following key participants namely:
Source - A source is an object that generates an event. This occurs when the internal state of
that object changes in some way. Sources may generate more than one type of event. A source
must register listeners in order for the listeners to receive notifications about a specific type
of event. Each type of event has its own registration method.
import java.awt.*;
import java.awt.event.*;
EventDemo() {
btn = new Button("Click Me");
btn.setBounds(50, 100, 80, 30);
btn.addActionListener(this);
add(btn);
setSize(300, 200);
setLayout(null);
setVisible(true);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public MouseMotionDemo() {
label = new JLabel("Move or Drag the Mouse");
label.setFont(new Font("Arial", Font.BOLD, 16));
add(label);
addMouseMotionListener(this);
setSize(400, 300);
setLayout(new FlowLayout());
setVisible(true);
}
Ans.
import javax.swing.*;
import javax.swing.tree.*;
public class b1
{
public static void main(String[] args)
{
Create frame JFrame frame = new JFrame("Border Layout Example");
frame.setSize(300, 200);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
import java.net.*;
import java.io.*;
getByName(String host)
getLocalHost()
getAllByName(String host)
Returns an InetAddress object for a specific hostname and byte array IP.
Example: InetAddress.getByAddress("localhost", new byte[]{127, 0, 0, 1});
Ans.
import java.net.*;
import java.util.Scanner;
try {
} catch (UnknownHostException e) {
System.out.println("Invalid hostname");
Q.11 Write a program to create a student table in database and insert a recors in student
table.
import java.sql.*;
public class St1{
try {
String createTable = "CREATE TABLE Student (Roll_no INT PRIMARY KEY, stu_name
VARCHAR(50), course_id INT, percentage FLOAT)";
stmt.executeUpdate(createTable);
String insertData = "INSERT INTO Student VALUES (1, 'John Doe', 101, 85.5)";
stmt.executeUpdate(insertData);
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
1. Client Tier (Presentation Layer) – This is the front end where users
interact with the application. It can be a Java GUI application
(Swing, JavaFX, or AWT) that takes user input and displays results.
2. Database Tier (Data Layer) – This is the back end where data is
stored. A database (like MySQL, Oracle, or PostgreSQL) stores and
manages the application's data. The client directly communicates
with the database to fetch or store data
How It Works:
The client application sends a request (e.g., fetching user details).
The request is sent to the database via JDBC (Java Database
Connectivity).
The database processes the request and sends back the result.
The client application displays the retrieved data.